Struct FnPtr
pub struct FnPtr { /* private fields */ }
Expand description
A general function pointer, which may carry additional (i.e. curried) argument values to be passed onto a function during a call.
Implementations§
§impl FnPtr
impl FnPtr
pub fn num_curried(&self) -> usize
👎Deprecated since 1.8.0: use curry().len()
instead
pub fn num_curried(&self) -> usize
curry().len()
insteadGet the number of curried arguments.
§Deprecated
This method is deprecated.
Use curry().len()
instead.
This method will be removed in the next major version.
pub fn call_dynamic(
&self,
context: &NativeCallContext<'_>,
this_ptr: Option<&mut Dynamic>,
arg_values: impl AsMut<[Dynamic]>,
) -> Result<Dynamic, Box<EvalAltResult>>
👎Deprecated since 1.3.0: use call_within_context
or call_raw
instead
pub fn call_dynamic( &self, context: &NativeCallContext<'_>, this_ptr: Option<&mut Dynamic>, arg_values: impl AsMut<[Dynamic]>, ) -> Result<Dynamic, Box<EvalAltResult>>
call_within_context
or call_raw
insteadCall the function pointer with curried arguments (if any).
The function may be script-defined (not available under no_function
) or native Rust.
This method is intended for calling a function pointer that is passed into a native Rust
function as an argument. Therefore, the AST
is NOT evaluated before calling the
function.
§Deprecated
This method is deprecated.
Use call_within_context
or call_raw
instead.
This method will be removed in the next major version.
§impl FnPtr
impl FnPtr
pub fn new(
name: impl Into<ImmutableString>,
) -> Result<FnPtr, Box<EvalAltResult>>
pub fn new( name: impl Into<ImmutableString>, ) -> Result<FnPtr, Box<EvalAltResult>>
Create a new function pointer.
§Errors
Returns an error if the function name is not a valid identifier or is a reserved keyword.
pub fn from_fn(
name: impl Into<ImmutableString>,
func: impl Fn(NativeCallContext<'_>, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync + 'static,
) -> Result<FnPtr, Box<EvalAltResult>>
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.
pub fn from_fn( name: impl Into<ImmutableString>, func: impl Fn(NativeCallContext<'_>, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync + 'static, ) -> Result<FnPtr, Box<EvalAltResult>>
Create a new function pointer from a native Rust function.
§Errors
Returns an error if the function name is not a valid identifier or is a reserved keyword.
§WARNING - Unstable API
This API is volatile and may change in the future.
§Callback Function Signature
Fn(context: NativeCallContext, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
pub fn from_dyn_fn(
name: impl Into<ImmutableString>,
func: Box<dyn Fn(NativeCallContext<'_>, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync>,
) -> Result<FnPtr, Box<EvalAltResult>>
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.
pub fn from_dyn_fn( name: impl Into<ImmutableString>, func: Box<dyn Fn(NativeCallContext<'_>, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync>, ) -> Result<FnPtr, Box<EvalAltResult>>
Create a new function pointer from a native Rust function.
§Errors
Returns an error if the function name is not a valid identifier or is a reserved keyword.
§WARNING - Unstable API
This API is volatile and may change in the future.
§Callback Function Signature
Fn(context: NativeCallContext, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
pub fn iter_curry(&self) -> impl Iterator<Item = &Dynamic>
pub fn iter_curry(&self) -> impl Iterator<Item = &Dynamic>
Iterate the curried arguments.
pub fn iter_curry_mut(&mut self) -> impl Iterator<Item = &mut Dynamic>
pub fn iter_curry_mut(&mut self) -> impl Iterator<Item = &mut Dynamic>
Mutably-iterate the curried arguments.
pub fn set_curry(
&mut self,
values: impl IntoIterator<Item = Dynamic>,
) -> &mut FnPtr
pub fn set_curry( &mut self, values: impl IntoIterator<Item = Dynamic>, ) -> &mut FnPtr
Set curried arguments to the function pointer.
pub fn is_curried(&self) -> bool
pub fn is_curried(&self) -> bool
Is the function pointer curried?
pub fn call<T>(
&self,
engine: &Engine,
ast: &AST,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>>where
T: Variant + Clone,
pub fn call<T>(
&self,
engine: &Engine,
ast: &AST,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>>where
T: Variant + Clone,
Call the function pointer with curried arguments (if any).
The function may be script-defined (not available under no_function
) or native Rust.
This method is intended for calling a function pointer directly, possibly on another Engine
.
Therefore, the AST
is NOT evaluated before calling the function.
§Example
use rhai::{Engine, FnPtr};
let engine = Engine::new();
let ast = engine.compile("fn foo(x, y) { len(x) + y }")?;
let mut fn_ptr = FnPtr::new("foo")?;
// Curry values into the function pointer
fn_ptr.set_curry(vec!["abc".into()]);
// Values are only needed for non-curried parameters
let result: i64 = fn_ptr.call(&engine, &ast, ( 39_i64, ) )?;
assert_eq!(result, 42);
pub fn call_within_context<T>(
&self,
context: &NativeCallContext<'_>,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>>where
T: Variant + Clone,
pub fn call_within_context<T>(
&self,
context: &NativeCallContext<'_>,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>>where
T: Variant + Clone,
Call the function pointer with curried arguments (if any).
The function may be script-defined (not available under no_function
) or native Rust.
This method is intended for calling a function pointer that is passed into a native Rust
function as an argument. Therefore, the AST
is NOT evaluated before calling the
function.
pub fn call_raw(
&self,
context: &NativeCallContext<'_>,
this_ptr: Option<&mut Dynamic>,
arg_values: impl AsMut<[Dynamic]>,
) -> Result<Dynamic, Box<EvalAltResult>>
pub fn call_raw( &self, context: &NativeCallContext<'_>, this_ptr: Option<&mut Dynamic>, arg_values: impl AsMut<[Dynamic]>, ) -> Result<Dynamic, Box<EvalAltResult>>
Call the function pointer with curried arguments (if any).
The function may be script-defined (not available under no_function
) or native Rust.
This method is intended for calling a function pointer that is passed into a native Rust
function as an argument. Therefore, the AST
is NOT evaluated before calling the
function.
§WARNING - Low Level API
This function is very low level.
§Arguments
All the arguments are consumed, meaning that they’re replaced by ()
.
This is to avoid unnecessarily cloning the arguments.
Do not use the arguments after this call. If they are needed afterwards, clone them before calling this function.
Trait Implementations§
§impl Extend<Dynamic> for FnPtr
impl Extend<Dynamic> for FnPtr
§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = Dynamic>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = Dynamic>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)