From a1549bb3772b3631db7e7d85e6fa06266062b3f1 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 15 Mar 2021 22:37:50 +0800 Subject: [PATCH] Allow register_result to return any type. --- CHANGELOG.md | 5 +++++ src/engine_api.rs | 34 +++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9957e59e..9735f1b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ Breaking changes * `FnPtr::call_dynamic` now takes `&NativeCallContext` instead of consuming it. * All `Module::set_fn_XXX` methods are removed, in favor of `Module::set_native_fn`. +Enhancements +------------ + +* `Engine::register_result_fn` no longer requires the successful return type to be `Dynamic`. It can now be any type. + Version 0.19.14 =============== diff --git a/src/engine_api.rs b/src/engine_api.rs index 3ccf26bc..7654ef74 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -82,15 +82,15 @@ impl Engine { /// # Example /// /// ``` - /// use rhai::{Engine, Dynamic, EvalAltResult}; + /// use rhai::{Engine, EvalAltResult}; /// /// // Normal function - /// fn div(x: i64, y: i64) -> Result> { + /// fn div(x: i64, y: i64) -> Result> { /// if y == 0 { /// // '.into()' automatically converts to 'Box' /// Err("division by zero!".into()) /// } else { - /// Ok((x / y).into()) + /// Ok(x / y) /// } /// } /// @@ -102,9 +102,9 @@ impl Engine { /// .expect_err("expecting division by zero error!"); /// ``` #[inline] - pub fn register_result_fn(&mut self, name: &str, func: F) -> &mut Self + pub fn register_result_fn(&mut self, name: &str, func: F) -> &mut Self where - F: RegisterNativeFunction, + F: RegisterNativeFunction>>, { let param_types = F::param_types(); let mut param_type_names: StaticVec<_> = F::param_names() @@ -321,8 +321,8 @@ impl Engine { /// fn new() -> Self { Self { field: 1 } } /// /// // Even a getter must start with `&mut self` and not `&self`. - /// fn get_field(&mut self) -> Result> { - /// Ok(self.field.into()) + /// fn get_field(&mut self) -> Result> { + /// Ok(self.field) /// } /// } /// @@ -342,10 +342,10 @@ impl Engine { /// ``` #[cfg(not(feature = "no_object"))] #[inline(always)] - pub fn register_get_result( + pub fn register_get_result( &mut self, name: &str, - get_fn: impl Fn(&mut T) -> RhaiResult + SendSync + 'static, + get_fn: impl Fn(&mut T) -> Result> + SendSync + 'static, ) -> &mut Self { use crate::engine::make_getter; self.register_result_fn(&make_getter(name), get_fn) @@ -444,7 +444,7 @@ impl Engine { ) -> &mut Self { use crate::engine::make_setter; self.register_result_fn(&make_setter(name), move |obj: &mut T, value: U| { - set_fn(obj, value).map(Into::into) + set_fn(obj, value) }) } /// Short-hand for registering both getter and setter functions @@ -583,8 +583,8 @@ impl Engine { /// fn new() -> Self { Self { fields: vec![1, 2, 3, 4, 5] } } /// /// // Even a getter must start with `&mut self` and not `&self`. - /// fn get_field(&mut self, index: i64) -> Result> { - /// Ok(self.fields[index as usize].into()) + /// fn get_field(&mut self, index: i64) -> Result> { + /// Ok(self.fields[index as usize]) /// } /// } /// @@ -606,9 +606,13 @@ impl Engine { /// ``` #[cfg(not(feature = "no_index"))] #[inline(always)] - pub fn register_indexer_get_result( + pub fn register_indexer_get_result< + T: Variant + Clone, + X: Variant + Clone, + U: Variant + Clone, + >( &mut self, - get_fn: impl Fn(&mut T, X) -> RhaiResult + SendSync + 'static, + get_fn: impl Fn(&mut T, X) -> Result> + SendSync + 'static, ) -> &mut Self { if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); @@ -761,7 +765,7 @@ impl Engine { self.register_result_fn( crate::engine::FN_IDX_SET, - move |obj: &mut T, index: X, value: U| set_fn(obj, index, value).map(Into::into), + move |obj: &mut T, index: X, value: U| set_fn(obj, index, value), ) } /// Short-hand for register both index getter and setter functions for a custom type with the [`Engine`].