From 1deed8cd5588640e5052a53efccc4821f9e6047f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 22 Aug 2022 00:15:00 +0800 Subject: [PATCH] Fix generic parameters. --- CHANGELOG.md | 1 + src/api/register.rs | 13 +++++----- src/module/mod.rs | 58 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddda6008..abcec383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. +* Missing API function `Module::set_getter_setter_fn` is added. Version 1.9.0 diff --git a/src/api/register.rs b/src/api/register.rs index a6ba5284..427fd69e 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -549,11 +549,11 @@ impl Engine { /// ``` #[cfg(not(feature = "no_object"))] #[inline(always)] - pub fn register_get_set( + pub fn register_get_set( &mut self, name: impl AsRef, - get_fn: impl RegisterNativeFunction<(Mut,), V, S> + SendSync + 'static, - set_fn: impl RegisterNativeFunction<(Mut, V), (), S> + SendSync + 'static, + get_fn: impl RegisterNativeFunction<(Mut,), V, S1> + SendSync + 'static, + set_fn: impl RegisterNativeFunction<(Mut, V), (), S2> + SendSync + 'static, ) -> &mut Self { self.register_get(&name, get_fn).register_set(&name, set_fn) } @@ -924,11 +924,12 @@ impl Engine { T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, - S, + S1, + S2, >( &mut self, - get_fn: impl RegisterNativeFunction<(Mut, X), V, S> + SendSync + 'static, - set_fn: impl RegisterNativeFunction<(Mut, X, V), (), S> + SendSync + 'static, + get_fn: impl RegisterNativeFunction<(Mut, X), V, S1> + SendSync + 'static, + set_fn: impl RegisterNativeFunction<(Mut, X, V), (), S2> + SendSync + 'static, ) -> &mut Self { self.register_indexer_get(get_fn) .register_indexer_set(set_fn) diff --git a/src/module/mod.rs b/src/module/mod.rs index 743d2e93..ef496427 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1368,11 +1368,11 @@ impl Module { /// ``` #[cfg(not(feature = "no_object"))] #[inline(always)] - pub fn set_setter_fn(&mut self, name: impl AsRef, func: F) -> u64 + pub fn set_setter_fn(&mut self, name: impl AsRef, func: F) -> u64 where A: Variant + Clone, - B: Variant + Clone, - F: RegisterNativeFunction<(Mut, B), (), RhaiResultOf> + SendSync + 'static, + T: Variant + Clone, + F: RegisterNativeFunction<(Mut, T), (), RhaiResultOf> + SendSync + 'static, { self.set_fn( 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( + &mut self, + name: impl AsRef, + getter: impl RegisterNativeFunction<(Mut,), T, RhaiResultOf> + SendSync + 'static, + setter: impl RegisterNativeFunction<(Mut, T), (), RhaiResultOf> + 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`], /// returning a non-zero hash key. /// 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 /// [`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. /// /// # Panics @@ -1541,10 +1585,10 @@ impl Module { /// ``` #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[inline(always)] - pub fn set_indexer_get_set_fn( + pub fn set_indexer_get_set_fn( &mut self, - get_fn: impl RegisterNativeFunction<(Mut, B), T, RhaiResultOf> + SendSync + 'static, - set_fn: impl RegisterNativeFunction<(Mut, B, T), (), RhaiResultOf> + SendSync + 'static, + get_fn: impl RegisterNativeFunction<(Mut, B), T, RhaiResultOf> + SendSync + 'static, + set_fn: impl RegisterNativeFunction<(Mut, B, T), (), RhaiResultOf> + SendSync + 'static, ) -> (u64, u64) where A: Variant + Clone,