Struct rhai::FnPtr

source ·
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§

source§

impl FnPtr

source

pub fn num_curried(&self) -> usize

👎Deprecated since 1.8.0: use <code>curry().len()</code> instead

Get the number of curried arguments.

Deprecated

This method is deprecated. Use curry().len() instead.

This method will be removed in the next major version.

source

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 <code>call_within_context</code> or <code>call_raw</code> instead

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.

Deprecated

This method is deprecated. Use call_within_context or call_raw instead.

This method will be removed in the next major version.

source§

impl FnPtr

source

pub fn new(name: impl Into<ImmutableString>) -> Result<Self, Box<EvalAltResult>>

Create a new function pointer.

source

pub fn fn_name(&self) -> &str

Get the name of the function.

source

pub fn curry(&self) -> &[Dynamic]

Get the curried arguments.

source

pub fn iter_curry(&self) -> impl Iterator<Item = &Dynamic>

Iterate the curried arguments.

source

pub fn iter_curry_mut(&mut self) -> impl Iterator<Item = &mut Dynamic>

Mutably-iterate the curried arguments.

source

pub fn add_curry(&mut self, value: Dynamic) -> &mut Self

Add a new curried argument.

source

pub fn set_curry( &mut self, values: impl IntoIterator<Item = Dynamic> ) -> &mut Self

Set curried arguments to the function pointer.

source

pub fn is_curried(&self) -> bool

Is the function pointer curried?

source

pub fn is_anonymous(&self) -> bool

Does the function pointer refer to an anonymous function?

Not available under no_function.

source

pub fn call<T: Variant + Clone>( &self, engine: &Engine, ast: &AST, args: impl FuncArgs ) -> Result<T, 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 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);
source

pub fn call_within_context<T: Variant + Clone>( &self, context: &NativeCallContext<'_>, args: impl FuncArgs ) -> Result<T, 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.

source

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.

source

pub fn call_raw_with_extra_args<const N: usize, const E: usize>( &self, fn_name: &str, ctx: &NativeCallContext<'_>, this_ptr: Option<&mut Dynamic>, args: [Dynamic; N], extras: [Dynamic; E], move_this_ptr_to_args: Option<usize> ) -> Result<Dynamic, Box<EvalAltResult>>

(internals) Make a call to a function pointer with either a specified number of arguments, or with extra arguments attached. Exported under the internals feature only.

If this_ptr is provided, it is first provided to script-defined functions bound to this.

When an appropriate function is not found and move_this_ptr_to_args is Some, this_ptr is removed and inserted as the appropriate parameter number.

This is useful for calling predicate closures within an iteration loop where the extra argument is the current element’s index.

If the function pointer is linked to a scripted function definition, use the appropriate number of arguments to call it directly (one version attaches extra arguments).

Trait Implementations§

source§

impl Clone for FnPtr

source§

fn clone(&self) -> FnPtr

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FnPtr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for FnPtr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<FnPtr> for Dynamic

source§

fn from(value: FnPtr) -> Self

Converts to this type from the input type.
source§

impl<T: Into<Shared<ScriptFnDef>>> From<T> for FnPtr

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl Hash for FnPtr

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Index<usize> for FnPtr

§

type Output = Dynamic

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<usize> for FnPtr

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl TryFrom<ImmutableString> for FnPtr

§

type Error = Box<EvalAltResult, Global>

The type returned in the event of a conversion error.
source§

fn try_from(value: ImmutableString) -> Result<Self, Box<EvalAltResult>>

Performs the conversion.

Auto Trait Implementations§

§

impl !RefUnwindSafe for FnPtr

§

impl !Send for FnPtr

§

impl !Sync for FnPtr

§

impl Unpin for FnPtr

§

impl !UnwindSafe for FnPtr

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.