Fix generic parameters.

This commit is contained in:
Stephen Chung 2022-08-22 00:15:00 +08:00
parent 80a23ddaa2
commit 1deed8cd55
3 changed files with 59 additions and 13 deletions

View File

@ -8,6 +8,7 @@ Bug fixes
--------- ---------
* API for registering property getters/setters and indexers to an `Engine` now works with functions that take a first parameter of `NativeCallContext`. * API for registering property getters/setters and indexers to an `Engine` now works with functions that take a first parameter of `NativeCallContext`.
* Missing API function `Module::set_getter_setter_fn` is added.
Version 1.9.0 Version 1.9.0

View File

@ -549,11 +549,11 @@ impl Engine {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_get_set<T: Variant + Clone, V: Variant + Clone, S>( pub fn register_get_set<T: Variant + Clone, V: Variant + Clone, S1, S2>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S2> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_get(&name, get_fn).register_set(&name, set_fn) self.register_get(&name, get_fn).register_set(&name, set_fn)
} }
@ -924,11 +924,12 @@ impl Engine {
T: Variant + Clone, T: Variant + Clone,
X: Variant + Clone, X: Variant + Clone,
V: Variant + Clone, V: Variant + Clone,
S, S1,
S2,
>( >(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S2> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_indexer_get(get_fn) self.register_indexer_get(get_fn)
.register_indexer_set(set_fn) .register_indexer_set(set_fn)

View File

@ -1368,11 +1368,11 @@ impl Module {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn set_setter_fn<A, B, F, S>(&mut self, name: impl AsRef<str>, func: F) -> u64 pub fn set_setter_fn<A, T, F, S>(&mut self, name: impl AsRef<str>, func: F) -> u64
where where
A: Variant + Clone, A: Variant + Clone,
B: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, B), (), RhaiResultOf<S>> + SendSync + 'static, F: RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S>> + SendSync + 'static,
{ {
self.set_fn( self.set_fn(
crate::engine::make_setter(name.as_ref()).as_str(), crate::engine::make_setter(name.as_ref()).as_str(),
@ -1384,6 +1384,48 @@ impl Module {
) )
} }
/// Set a pair of Rust getter and setter functions into the [`Module`], returning both non-zero hash keys.
/// This is a short-hand for [`set_getter_fn`][Module::set_getter_fn] and [`set_setter_fn`][Module::set_setter_fn].
///
/// These function are automatically exposed to the global namespace.
///
/// If there are similar existing Rust functions, they are replaced.
///
/// # Function Metadata
///
/// No metadata for the function is registered.
/// Use [`update_fn_metadata`][Module::update_fn_metadata] to add metadata.
///
/// # Example
///
/// ```
/// use rhai::{Module, ImmutableString};
///
/// let mut module = Module::new();
/// let (hash_get, hash_set) = module.set_getter_setter_fn("value",
/// |x: &mut i64| { Ok(x.to_string().into()) },
/// |x: &mut i64, y: ImmutableString| {
/// *x = y.len() as i64;
/// Ok(())
/// }
/// );
/// assert!(module.contains_fn(hash_get));
/// assert!(module.contains_fn(hash_set));
/// ```
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn set_getter_setter_fn<A: Variant + Clone, T: Variant + Clone, S1, S2>(
&mut self,
name: impl AsRef<str>,
getter: impl RegisterNativeFunction<(Mut<A>,), T, RhaiResultOf<S1>> + SendSync + 'static,
setter: impl RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S2>> + SendSync + 'static,
) -> (u64, u64) {
(
self.set_getter_fn(name.as_ref(), getter),
self.set_setter_fn(name.as_ref(), setter),
)
}
/// Set a Rust index getter taking two parameters (the first one mutable) into the [`Module`], /// Set a Rust index getter taking two parameters (the first one mutable) into the [`Module`],
/// returning a non-zero hash key. /// returning a non-zero hash key.
/// This function is automatically exposed to the global namespace. /// This function is automatically exposed to the global namespace.
@ -1506,10 +1548,12 @@ impl Module {
) )
} }
/// Set a pair of Rust index getter and setter functions, returning both non-zero hash keys. /// Set a pair of Rust index getter and setter functions into the [`Module`], returning both non-zero hash keys.
/// This is a short-hand for [`set_indexer_get_fn`][Module::set_indexer_get_fn] and /// This is a short-hand for [`set_indexer_get_fn`][Module::set_indexer_get_fn] and
/// [`set_indexer_set_fn`][Module::set_indexer_set_fn]. /// [`set_indexer_set_fn`][Module::set_indexer_set_fn].
/// ///
/// These functions are automatically exposed to the global namespace.
///
/// If there are similar existing Rust functions, they are replaced. /// If there are similar existing Rust functions, they are replaced.
/// ///
/// # Panics /// # Panics
@ -1541,10 +1585,10 @@ impl Module {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)] #[inline(always)]
pub fn set_indexer_get_set_fn<A, B, T, S>( pub fn set_indexer_get_set_fn<A, B, T, S1, S2>(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S>> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S1>> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S>> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S2>> + SendSync + 'static,
) -> (u64, u64) ) -> (u64, u64)
where where
A: Variant + Clone, A: Variant + Clone,