pub trait ReflectFn<'env, Marker>: ReflectFnMut<'env, Marker> {
// Required method
fn reflect_call<'a>(
&self,
args: ArgList<'a>,
) -> Result<Return<'a>, FunctionError>;
}
Expand description
A reflection-based version of the Fn
trait.
This allows functions to be called dynamically through reflection.
§Blanket Implementation
This trait has a blanket implementation that covers:
- Functions and methods defined with the
fn
keyword - Anonymous functions
- Function pointers
- Closures that capture immutable references to their environment
- Closures that take ownership of captured variables
For each of the above cases, the function signature may only have up to 15 arguments,
not including an optional receiver argument (often &self
or &mut self
).
This optional receiver argument may be either a mutable or immutable reference to a type.
If the return type is also a reference, its lifetime will be bound to the lifetime of this receiver.
See the module-level documentation for more information on valid signatures.
To handle functions that capture mutable references to their environment,
see the ReflectFnMut
trait instead.
Arguments are expected to implement FromArg
, and the return type is expected to implement IntoReturn
.
Both of these traits are automatically implemented when using the Reflect
derive macro.
§Example
fn add(a: i32, b: i32) -> i32 {
a + b
}
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let value = add.reflect_call(args).unwrap().unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 100);
§Trait Parameters
This trait has a Marker
type parameter that is used to get around issues with
unconstrained type parameters when defining impls with generic arguments or return types.
This Marker
can be any type, provided it doesn’t conflict with other implementations.
Additionally, it has a lifetime parameter, 'env
, that is used to bound the lifetime of the function.
For named functions and some closures, this will end up just being 'static
,
however, closures that borrow from their environment will have a lifetime bound to that environment.
Required Methods§
Sourcefn reflect_call<'a>(
&self,
args: ArgList<'a>,
) -> Result<Return<'a>, FunctionError>
fn reflect_call<'a>( &self, args: ArgList<'a>, ) -> Result<Return<'a>, FunctionError>
Call the function with the given arguments and return the result.