Skip to content

Commit

Permalink
Fix TypeBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 30, 2024
1 parent bdadfa4 commit 31b0760
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Other bug fixes
* Arrays in object maps now serialize to JSON correctly via `to_json()` when the `serde` feature is not enabled.
* `Engine::format_map_as_json` now serializes arrays correctly.
* `Engine::gen_fn_signatures(false)` now properly skips functions in the standard library.
* `TypeBuilder::with_name` now properly sets the display-name of the type for use in generating metadata.

Enhancements
------------
Expand Down
28 changes: 8 additions & 20 deletions src/api/build_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,27 @@ impl Engine {
///
/// To define a pretty-print name, call [`with_name`][`TypeBuilder::with_name`],
/// to use [`Engine::register_type_with_name`] instead.
pub struct TypeBuilder<'a, 's, T: Variant + Clone> {
pub struct TypeBuilder<'a, T: Variant + Clone> {
engine: &'a mut Engine,
name: Option<&'s str>,
_marker: PhantomData<T>,
}

impl<'a, T: Variant + Clone> TypeBuilder<'a, '_, T> {
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// Create a [`TypeBuilder`] linked to a particular [`Engine`] instance.
#[inline(always)]
fn new(engine: &'a mut Engine) -> Self {
Self {
engine,
name: None,
_marker: PhantomData,
}
}
}

impl<'s, T: Variant + Clone> TypeBuilder<'_, 's, T> {
impl<'s, T: Variant + Clone> TypeBuilder<'_, T> {
/// Set a pretty-print name for the `type_of` function.
#[inline(always)]
pub fn with_name(&mut self, name: &'s str) -> &mut Self {
self.name = Some(name.as_ref());
pub fn with_name(&mut self, name: &str) -> &mut Self {
self.engine.register_type_with_name::<T>(name);
self
}

Expand Down Expand Up @@ -156,7 +154,7 @@ impl<'s, T: Variant + Clone> TypeBuilder<'_, 's, T> {
}
}

impl<T> TypeBuilder<'_, '_, T>
impl<T> TypeBuilder<'_, T>
where
T: Variant + Clone + IntoIterator,
<T as IntoIterator>::Item: Variant + Clone,
Expand All @@ -171,7 +169,7 @@ where
}

#[cfg(not(feature = "no_object"))]
impl<T: Variant + Clone> TypeBuilder<'_, '_, T> {
impl<T: Variant + Clone> TypeBuilder<'_, T> {
/// Register a getter function.
///
/// The function signature must start with `&mut self` and not `&self`.
Expand Down Expand Up @@ -224,7 +222,7 @@ impl<T: Variant + Clone> TypeBuilder<'_, '_, T> {
}

#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
impl<T: Variant + Clone> TypeBuilder<'_, '_, T> {
impl<T: Variant + Clone> TypeBuilder<'_, T> {
/// Register an index getter.
///
/// The function signature must start with `&mut self` and not `&self`.
Expand Down Expand Up @@ -281,13 +279,3 @@ impl<T: Variant + Clone> TypeBuilder<'_, '_, T> {
self
}
}

impl<T: Variant + Clone> Drop for TypeBuilder<'_, '_, T> {
#[inline]
fn drop(&mut self) {
match self.name {
Some(ref name) => self.engine.register_type_with_name::<T>(name),
None => self.engine.register_type::<T>(),
};
}
}

0 comments on commit 31b0760

Please sign in to comment.