pub trait ReflectFnMut<'env, Marker> {
// Required method
fn reflect_call_mut<'a>(
&mut self,
args: ArgList<'a>,
) -> Result<Return<'a>, FunctionError>;
}functions only.Expand description
A reflection-based version of the FnMut trait.
This allows functions to be called dynamically through reflection.
This is a supertrait of ReflectFn, and is used for functions that may mutate their environment,
such as closures that capture mutable references.
§Blanket Implementation
This trait has a blanket implementation that covers everything that ReflectFn does:
- Functions and methods defined with the
fnkeyword - Anonymous functions
- Function pointers
- Closures that capture immutable references to their environment
- Closures that take ownership of captured variables
But also allows for:
- Closures that capture mutable references to their environment
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.
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
let mut list: Vec<i32> = vec![1, 3];
// `insert` is a closure that captures a mutable reference to `list`
let mut insert = |index: usize, value: i32| {
list.insert(index, value);
};
let args = ArgList::new().with_owned(1_usize).with_owned(2_i32);
insert.reflect_call_mut(args).unwrap();
assert_eq!(list, vec![1, 2, 3]);§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_mut<'a>(
&mut self,
args: ArgList<'a>,
) -> Result<Return<'a>, FunctionError>
fn reflect_call_mut<'a>( &mut self, args: ArgList<'a>, ) -> Result<Return<'a>, FunctionError>
Call the function with the given arguments and return the result.