Fix sync.

This commit is contained in:
Stephen Chung 2022-08-22 14:02:24 +08:00
parent 799dd9d9d1
commit db865d7538
2 changed files with 40 additions and 24 deletions

View File

@ -292,7 +292,7 @@ impl Engine {
.set_custom_type_raw(fully_qualified_type_path, name); .set_custom_type_raw(fully_qualified_type_path, name);
self 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. /// This is an advanced API.
#[inline(always)] #[inline(always)]
pub fn register_iterator<T>(&mut self) -> &mut Self pub fn register_iterator<T>(&mut self) -> &mut Self
@ -303,6 +303,17 @@ impl Engine {
self.global_namespace_mut().set_iterable::<T>(); self.global_namespace_mut().set_iterable::<T>();
self 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<T, X>(&mut self) -> &mut Self
where
T: Variant + Clone + IntoIterator<Item = RhaiResultOf<X>>,
X: Variant + Clone,
{
self.global_namespace_mut().set_iterable_result::<T, X>();
self
}
/// Register a getter function for a member of a registered type with the [`Engine`]. /// 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`. /// The function signature must start with `&mut self` and not `&self`.

View File

@ -2250,12 +2250,11 @@ impl Module {
} }
/// Set a type iterator into the [`Module`]. /// Set a type iterator into the [`Module`].
#[cfg(not(feature = "sync"))]
#[inline(always)] #[inline(always)]
pub fn set_iter( pub fn set_iter(
&mut self, &mut self,
type_id: TypeId, type_id: TypeId,
func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static, func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.set_iter_result(type_id, move |x| { self.set_iter_result(type_id, move |x| {
Box::new(func(x).map(Ok)) as Box<dyn Iterator<Item = RhaiResultOf<Dynamic>>> Box::new(func(x).map(Ok)) as Box<dyn Iterator<Item = RhaiResultOf<Dynamic>>>
@ -2263,12 +2262,11 @@ impl Module {
} }
/// Set a fallible type iterator into the [`Module`]. /// Set a fallible type iterator into the [`Module`].
#[cfg(not(feature = "sync"))]
#[inline] #[inline]
pub fn set_iter_result( pub fn set_iter_result(
&mut self, &mut self,
type_id: TypeId, type_id: TypeId,
func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = RhaiResultOf<Dynamic>>> + 'static, func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = RhaiResultOf<Dynamic>>> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
let func = Shared::new(func); let func = Shared::new(func);
if self.indexed { if self.indexed {
@ -2280,24 +2278,7 @@ impl Module {
} }
/// Set a type iterator into the [`Module`]. /// Set a type iterator into the [`Module`].
#[cfg(feature = "sync")] #[inline(always)]
#[inline]
pub fn set_iter(
&mut self,
type_id: TypeId,
func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 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]
pub fn set_iterable<T>(&mut self) -> &mut Self pub fn set_iterable<T>(&mut self) -> &mut Self
where where
T: Variant + Clone + IntoIterator, 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<T, X>(&mut self) -> &mut Self
where
T: Variant + Clone + IntoIterator<Item = RhaiResultOf<X>>,
X: Variant + Clone,
{
self.set_iter_result(TypeId::of::<T>(), |obj: Dynamic| {
Box::new(obj.cast::<T>().into_iter().map(|v| v.map(Dynamic::from)))
})
}
/// Set an iterator type into the [`Module`] as a type iterator. /// Set an iterator type into the [`Module`] as a type iterator.
#[inline] #[inline(always)]
pub fn set_iterator<T>(&mut self) -> &mut Self pub fn set_iterator<T>(&mut self) -> &mut Self
where where
T: Variant + Clone + Iterator, 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<T, X>(&mut self) -> &mut Self
where
T: Variant + Clone + Iterator<Item = RhaiResultOf<X>>,
X: Variant + Clone,
{
self.set_iter_result(TypeId::of::<T>(), |obj: Dynamic| {
Box::new(obj.cast::<T>().map(|v| v.map(Dynamic::from)))
})
}
/// Get the specified type iterator. /// Get the specified type iterator.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline] #[inline]