Remove _result registration methods.

This commit is contained in:
Stephen Chung
2022-08-22 22:16:26 +08:00
parent 4ce8d4609d
commit a9413dc570
9 changed files with 260 additions and 491 deletions

View File

@@ -88,64 +88,6 @@ impl Engine {
);
self
}
/// Register a custom fallible function with the [`Engine`].
///
/// # Example
///
/// ```
/// use rhai::{Engine, EvalAltResult};
///
/// // Normal function
/// fn div(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> {
/// if y == 0 {
/// // '.into()' automatically converts to 'Box<EvalAltResult::ErrorRuntime>'
/// Err("division by zero!".into())
/// } else {
/// Ok(x / y)
/// }
/// }
///
/// let mut engine = Engine::new();
///
/// engine.register_result_fn("div", div);
///
/// engine.eval::<i64>("div(42, 0)")
/// .expect_err("expecting division by zero error!");
/// ```
#[inline]
pub fn register_result_fn<N, A, F, R, S>(&mut self, name: N, func: F) -> &mut Self
where
N: AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, R, RhaiResultOf<S>>,
{
let param_types = F::param_types();
#[cfg(feature = "metadata")]
let param_type_names: crate::StaticVec<_> = F::param_names()
.iter()
.map(|ty| format!("_: {}", self.format_type_name(ty)))
.chain(Some(self.format_type_name(F::return_type_name()).into()))
.collect();
#[cfg(feature = "metadata")]
let param_type_names: crate::StaticVec<_> =
param_type_names.iter().map(String::as_str).collect();
#[cfg(feature = "metadata")]
let param_type_names = Some(param_type_names.as_ref());
#[cfg(not(feature = "metadata"))]
let param_type_names: Option<&[&str]> = None;
self.global_namespace_mut().set_fn(
name,
FnNamespace::Global,
FnAccess::Public,
param_type_names,
param_types,
func.into_callable_function(),
);
self
}
/// Register a function of the [`Engine`].
///
/// # WARNING - Low Level API
@@ -363,55 +305,6 @@ impl Engine {
) -> &mut Self {
self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
}
/// 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`.
///
/// Not available under `no_object`.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, EvalAltResult};
///
/// #[derive(Clone)]
/// struct TestStruct {
/// field: i64
/// }
///
/// impl TestStruct {
/// fn new() -> Self {
/// Self { field: 1 }
/// }
/// // Even a getter must start with `&mut self` and not `&self`.
/// fn get_field(&mut self) -> Result<i64, Box<EvalAltResult>> {
/// Ok(self.field)
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// // Register API for the custom type.
/// engine
/// .register_type::<TestStruct>()
/// .register_fn("new_ts", TestStruct::new)
/// // Register a getter on a property (notice it doesn't have to be the same name).
/// .register_get_result("xyz", TestStruct::get_field);
///
/// assert_eq!(engine.eval::<i64>("let a = new_ts(); a.xyz")?, 1);
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn register_get_result<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, RhaiResultOf<S>> + SendSync + 'static,
) -> &mut Self {
self.register_result_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
}
/// Register a setter function for a member of a registered type with the [`Engine`].
///
/// Not available under `no_object`.
@@ -462,57 +355,6 @@ impl Engine {
) -> &mut Self {
self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
}
/// Register a setter function for a member of a registered type with the [`Engine`].
///
/// Not available under `no_object`.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, EvalAltResult};
///
/// #[derive(Debug, Clone, Eq, PartialEq)]
/// struct TestStruct {
/// field: i64
/// }
///
/// impl TestStruct {
/// fn new() -> Self {
/// Self { field: 1 }
/// }
/// fn set_field(&mut self, new_val: i64) -> Result<(), Box<rhai::EvalAltResult>> {
/// self.field = new_val;
/// Ok(())
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// // Register API for the custom type.
/// engine
/// .register_type::<TestStruct>()
/// .register_fn("new_ts", TestStruct::new)
/// // Register a setter on a property (notice it doesn't have to be the same name)
/// .register_set_result("xyz", TestStruct::set_field);
///
/// // Notice that, with a getter, there is no way to get the property value
/// assert_eq!(
/// engine.eval::<TestStruct>("let a = new_ts(); a.xyz = 42; a")?,
/// TestStruct { field: 42 }
/// );
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), RhaiResultOf<S>> + SendSync + 'static,
) -> &mut Self {
self.register_result_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
}
/// Short-hand for registering both getter and setter functions
/// of a registered type with the [`Engine`].
///
@@ -643,86 +485,6 @@ impl Engine {
self.register_fn(crate::engine::FN_IDX_GET, get_fn)
}
/// Register an index getter for a custom type with the [`Engine`].
///
/// The function signature must start with `&mut self` and not `&self`.
///
/// Not available under both `no_index` and `no_object`.
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
/// Indexers for arrays, object maps, strings and integers cannot be registered.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, EvalAltResult};
///
/// #[derive(Clone)]
/// struct TestStruct {
/// fields: Vec<i64>
/// }
///
/// impl TestStruct {
/// 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<i64, Box<EvalAltResult>> {
/// Ok(self.fields[index as usize])
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// // Register API for the custom type.
/// # #[cfg(not(feature = "no_object"))]
/// engine.register_type::<TestStruct>();
///
/// engine
/// .register_fn("new_ts", TestStruct::new)
/// // Register an indexer.
/// .register_indexer_get_result(TestStruct::get_field);
///
/// # #[cfg(not(feature = "no_index"))]
/// assert_eq!(engine.eval::<i64>("let a = new_ts(); a[2]")?, 3);
/// # Ok(())
/// # }
/// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline]
pub fn register_indexer_get_result<
T: Variant + Clone,
X: Variant + Clone,
V: Variant + Clone,
S,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, RhaiResultOf<S>> + SendSync + 'static,
) -> &mut Self {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
panic!("Cannot register indexer for arrays.");
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
panic!("Cannot register indexer for object maps.");
}
if TypeId::of::<T>() == TypeId::of::<String>()
|| TypeId::of::<T>() == TypeId::of::<&str>()
|| TypeId::of::<T>() == TypeId::of::<crate::ImmutableString>()
{
panic!("Cannot register indexer for strings.");
}
if TypeId::of::<T>() == TypeId::of::<crate::INT>() {
panic!("Cannot register indexer for integers.");
}
self.register_result_fn(crate::engine::FN_IDX_GET, get_fn)
}
/// Register an index setter for a custom type with the [`Engine`].
///
/// Not available under both `no_index` and `no_object`.
@@ -798,87 +560,6 @@ impl Engine {
self.register_fn(crate::engine::FN_IDX_SET, set_fn)
}
/// Register an index setter for a custom type with the [`Engine`].
///
/// Not available under both `no_index` and `no_object`.
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
/// Indexers for arrays, object maps, strings and integers cannot be registered.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, EvalAltResult};
///
/// #[derive(Clone)]
/// struct TestStruct {
/// fields: Vec<i64>
/// }
///
/// impl TestStruct {
/// fn new() -> Self {
/// Self { fields: vec![1, 2, 3, 4, 5] }
/// }
/// fn set_field(&mut self, index: i64, value: i64) -> Result<(), Box<rhai::EvalAltResult>> {
/// self.fields[index as usize] = value;
/// Ok(())
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// // Register API for the custom type.
/// # #[cfg(not(feature = "no_object"))]
/// engine.register_type::<TestStruct>();
///
/// engine
/// .register_fn("new_ts", TestStruct::new)
/// // Register an indexer.
/// .register_indexer_set_result(TestStruct::set_field);
///
/// # #[cfg(not(feature = "no_index"))]
/// let result = engine.eval::<TestStruct>("let a = new_ts(); a[2] = 42; a")?;
///
/// # #[cfg(not(feature = "no_index"))]
/// assert_eq!(result.fields[2], 42);
/// # Ok(())
/// # }
/// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline]
pub fn register_indexer_set_result<
T: Variant + Clone,
X: Variant + Clone,
V: Variant + Clone,
S,
>(
&mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), RhaiResultOf<S>> + SendSync + 'static,
) -> &mut Self {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
panic!("Cannot register indexer for arrays.");
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
panic!("Cannot register indexer for object maps.");
}
if TypeId::of::<T>() == TypeId::of::<String>()
|| TypeId::of::<T>() == TypeId::of::<&str>()
|| TypeId::of::<T>() == TypeId::of::<crate::ImmutableString>()
{
panic!("Cannot register indexer for strings.");
}
if TypeId::of::<T>() == TypeId::of::<crate::INT>() {
panic!("Cannot register indexer for integers.");
}
self.register_result_fn(crate::engine::FN_IDX_SET, set_fn)
}
/// Short-hand for registering both index getter and setter functions for a custom type with the [`Engine`].
///
/// Not available under both `no_index` and `no_object`.