pub struct DynamicFunction<'env> { /* private fields */ }
functions
only.Expand description
A dynamic representation of a function.
This type can be used to represent any callable that satisfies Fn
(or the reflection-based equivalent, ReflectFn
).
That is, any function or closure that does not mutably borrow data from its environment.
For functions that do need to capture their environment mutably (i.e. mutable closures),
see DynamicFunctionMut
.
See the module-level documentation for more information.
You will generally not need to construct this manually.
Instead, many functions and closures can be automatically converted using the IntoFunction
trait.
§Example
Most of the time, a DynamicFunction
can be created using the IntoFunction
trait:
fn add(a: i32, b: i32) -> i32 {
a + b
}
// Convert the function into a dynamic function using `IntoFunction::into_function`:
let mut func: DynamicFunction = add.into_function();
// Dynamically call it:
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let value = func.call(args).unwrap().unwrap_owned();
// Check the result:
assert_eq!(value.try_downcast_ref::<i32>(), Some(&100));
Implementations§
Source§impl<'env> DynamicFunction<'env>
impl<'env> DynamicFunction<'env>
Sourcepub fn new<F: for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + Send + Sync + 'env>(
func: F,
info: FunctionInfo,
) -> Self
pub fn new<F: for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + Send + Sync + 'env>( func: F, info: FunctionInfo, ) -> Self
Create a new DynamicFunction
.
The given function can be used to call out to any other callable, including functions, closures, or methods.
It’s important that the function signature matches the provided FunctionInfo
as this will be used to validate arguments when calling the function.
Sourcepub fn with_name(self, name: impl Into<Cow<'static, str>>) -> Self
pub fn with_name(self, name: impl Into<Cow<'static, str>>) -> Self
Set the name of the function.
For DynamicFunctions
created using IntoFunction
,
the default name will always be the full path to the function as returned by std::any::type_name
,
unless the function is a closure, anonymous function, or function pointer,
in which case the name will be None
.
Sourcepub fn call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>
pub fn call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>
Call the function with the given arguments.
§Example
let c = 23;
let add = |a: i32, b: i32| -> i32 {
a + b + c
};
let mut func = add.into_function().with_name("add");
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 123);
§Errors
This method will return an error if the number of arguments provided does not match
the number of arguments expected by the function’s FunctionInfo
.
The function itself may also return any errors it needs to.
Sourcepub fn info(&self) -> &FunctionInfo
pub fn info(&self) -> &FunctionInfo
Returns the function info.
Sourcepub fn name(&self) -> Option<&Cow<'static, str>>
pub fn name(&self) -> Option<&Cow<'static, str>>
The name of the function.
For DynamicFunctions
created using IntoFunction
,
the default name will always be the full path to the function as returned by std::any::type_name
,
unless the function is a closure, anonymous function, or function pointer,
in which case the name will be None
.
This can be overridden using with_name
.
Trait Implementations§
Source§impl<'env> Clone for DynamicFunction<'env>
impl<'env> Clone for DynamicFunction<'env>
Source§impl<'env> Debug for DynamicFunction<'env>
impl<'env> Debug for DynamicFunction<'env>
Outputs the function’s signature.
This takes the format: DynamicFunction(fn {name}({arg1}: {type1}, {arg2}: {type2}, ...) -> {return_type})
.
Names for arguments and the function itself are optional and will default to _
if not provided.
Source§impl<'env> From<DynamicFunction<'env>> for DynamicFunctionMut<'env>
impl<'env> From<DynamicFunction<'env>> for DynamicFunctionMut<'env>
Source§fn from(function: DynamicFunction<'env>) -> Self
fn from(function: DynamicFunction<'env>) -> Self
Source§impl Function for DynamicFunction<'static>
impl Function for DynamicFunction<'static>
Source§fn info(&self) -> &FunctionInfo
fn info(&self) -> &FunctionInfo
FunctionInfo
for this function.Source§fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>
Source§fn clone_dynamic(&self) -> DynamicFunction<'static>
fn clone_dynamic(&self) -> DynamicFunction<'static>
DynamicFunction
.Source§impl<'env> IntoFunction<'env, ()> for DynamicFunction<'env>
impl<'env> IntoFunction<'env, ()> for DynamicFunction<'env>
Source§fn into_function(self) -> DynamicFunction<'env>
fn into_function(self) -> DynamicFunction<'env>
Self
into a DynamicFunction
.Source§impl<'env> IntoFunctionMut<'env, ()> for DynamicFunction<'env>
impl<'env> IntoFunctionMut<'env, ()> for DynamicFunction<'env>
Source§fn into_function_mut(self) -> DynamicFunctionMut<'env>
fn into_function_mut(self) -> DynamicFunctionMut<'env>
Self
into a DynamicFunctionMut
.Source§impl PartialReflect for DynamicFunction<'static>
impl PartialReflect for DynamicFunction<'static>
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
Source§fn as_partial_reflect(&self) -> &dyn PartialReflect
fn as_partial_reflect(&self) -> &dyn PartialReflect
Source§fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
Source§fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Source§fn try_as_reflect(&self) -> Option<&dyn Reflect>
fn try_as_reflect(&self) -> Option<&dyn Reflect>
Source§fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
Source§fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn clone_value(&self) -> Box<dyn PartialReflect>
fn clone_value(&self) -> Box<dyn PartialReflect>
Reflect
trait object. Read moreSource§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool>
fn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool>
Source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§fn apply(&mut self, value: &dyn PartialReflect)
fn apply(&mut self, value: &dyn PartialReflect)
Source§impl<'env> TypePath for DynamicFunction<'env>
impl<'env> TypePath for DynamicFunction<'env>
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Auto Trait Implementations§
impl<'env> Freeze for DynamicFunction<'env>
impl<'env> !RefUnwindSafe for DynamicFunction<'env>
impl<'env> Send for DynamicFunction<'env>
impl<'env> Sync for DynamicFunction<'env>
impl<'env> Unpin for DynamicFunction<'env>
impl<'env> !UnwindSafe for DynamicFunction<'env>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.