diff --git a/src/api/register.rs b/src/api/register.rs index 427fd69e..5625773d 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -292,7 +292,7 @@ impl Engine { .set_custom_type_raw(fully_qualified_type_path, name); self } - /// Register an type iterator for an iterable type with the [`Engine`]. + /// Register a type iterator for an iterable type with the [`Engine`]. /// This is an advanced API. #[inline(always)] pub fn register_iterator(&mut self) -> &mut Self @@ -303,6 +303,17 @@ impl Engine { self.global_namespace_mut().set_iterable::(); self } + /// Register a fallible type iterator for an iterable type with the [`Engine`]. + /// This is an advanced API. + #[inline(always)] + pub fn register_iterator_result(&mut self) -> &mut Self + where + T: Variant + Clone + IntoIterator>, + X: Variant + Clone, + { + self.global_namespace_mut().set_iterable_result::(); + self + } /// Register a getter function for a member of a registered type with the [`Engine`]. /// /// The function signature must start with `&mut self` and not `&self`. diff --git a/src/module/mod.rs b/src/module/mod.rs index 79a43c64..143be2a5 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -2250,12 +2250,11 @@ impl Module { } /// Set a type iterator into the [`Module`]. - #[cfg(not(feature = "sync"))] #[inline(always)] pub fn set_iter( &mut self, type_id: TypeId, - func: impl Fn(Dynamic) -> Box> + 'static, + func: impl Fn(Dynamic) -> Box> + SendSync + 'static, ) -> &mut Self { self.set_iter_result(type_id, move |x| { Box::new(func(x).map(Ok)) as Box>> @@ -2263,12 +2262,11 @@ impl Module { } /// Set a fallible type iterator into the [`Module`]. - #[cfg(not(feature = "sync"))] #[inline] pub fn set_iter_result( &mut self, type_id: TypeId, - func: impl Fn(Dynamic) -> Box>> + 'static, + func: impl Fn(Dynamic) -> Box>> + SendSync + 'static, ) -> &mut Self { let func = Shared::new(func); if self.indexed { @@ -2280,24 +2278,7 @@ impl Module { } /// Set a type iterator into the [`Module`]. - #[cfg(feature = "sync")] - #[inline] - pub fn set_iter( - &mut self, - type_id: TypeId, - func: impl Fn(Dynamic) -> Box> + SendSync + 'static, - ) -> &mut Self { - let func = Shared::new(func); - if self.indexed { - self.all_type_iterators.insert(type_id, func.clone()); - self.contains_indexed_global_functions = true; - } - self.type_iterators.insert(type_id, func); - self - } - - /// Set a type iterator into the [`Module`]. - #[inline] + #[inline(always)] pub fn set_iterable(&mut self) -> &mut Self where T: Variant + Clone + IntoIterator, @@ -2308,8 +2289,20 @@ impl Module { }) } + /// Set a fallible type iterator into the [`Module`]. + #[inline(always)] + pub fn set_iterable_result(&mut self) -> &mut Self + where + T: Variant + Clone + IntoIterator>, + X: Variant + Clone, + { + self.set_iter_result(TypeId::of::(), |obj: Dynamic| { + Box::new(obj.cast::().into_iter().map(|v| v.map(Dynamic::from))) + }) + } + /// Set an iterator type into the [`Module`] as a type iterator. - #[inline] + #[inline(always)] pub fn set_iterator(&mut self) -> &mut Self where T: Variant + Clone + Iterator, @@ -2320,6 +2313,18 @@ impl Module { }) } + /// Set a iterator type into the [`Module`] as a fallible type iterator. + #[inline(always)] + pub fn set_iterator_result(&mut self) -> &mut Self + where + T: Variant + Clone + Iterator>, + X: Variant + Clone, + { + self.set_iter_result(TypeId::of::(), |obj: Dynamic| { + Box::new(obj.cast::().map(|v| v.map(Dynamic::from))) + }) + } + /// Get the specified type iterator. #[cfg(not(feature = "no_module"))] #[inline]