bevy_reflect::func

Struct DynamicFunction

Source
pub struct DynamicFunction<'env> { /* private fields */ }
Available on crate feature 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>

Source

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.

Source

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.

Source

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.

Source

pub fn info(&self) -> &FunctionInfo

Returns the function info.

Source

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>

Source§

fn clone(&self) -> Self

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<'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§

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

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

impl<'env> From<DynamicFunction<'env>> for DynamicFunctionMut<'env>

Source§

fn from(function: DynamicFunction<'env>) -> Self

Converts to this type from the input type.
Source§

impl Function for DynamicFunction<'static>

Source§

fn info(&self) -> &FunctionInfo

The FunctionInfo for this function.
Source§

fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>

Call this function with the given arguments.
Source§

fn clone_dynamic(&self) -> DynamicFunction<'static>

Clone this function into a DynamicFunction.
Source§

fn name(&self) -> Option<&Cow<'static, str>>

The name of the function, if any. Read more
Source§

fn arg_count(&self) -> usize

The number of arguments this function accepts.
Source§

impl<'env> IntoFunction<'env, ()> for DynamicFunction<'env>

Source§

fn into_function(self) -> DynamicFunction<'env>

Converts Self into a DynamicFunction.
Source§

impl<'env> IntoFunctionMut<'env, ()> for DynamicFunction<'env>

Source§

impl PartialReflect for DynamicFunction<'static>

Source§

fn get_represented_type_info(&self) -> Option<&'static TypeInfo>

Returns the TypeInfo of the type represented by this value. Read more
Source§

fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>

Casts this type to a boxed, reflected value. Read more
Source§

fn as_partial_reflect(&self) -> &dyn PartialReflect

Casts this type to a reflected value. Read more
Source§

fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect

Casts this type to a mutable, reflected value. Read more
Source§

fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>

Attempts to cast this type to a boxed, fully-reflected value.
Source§

fn try_as_reflect(&self) -> Option<&dyn Reflect>

Attempts to cast this type to a fully-reflected value.
Source§

fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>

Attempts to cast this type to a mutable, fully-reflected value.
Source§

fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>

Tries to apply a reflected value to this value. Read more
Source§

fn reflect_kind(&self) -> ReflectKind

Returns a zero-sized enumeration of “kinds” of type. Read more
Source§

fn reflect_ref(&self) -> ReflectRef<'_>

Returns an immutable enumeration of “kinds” of type. Read more
Source§

fn reflect_mut(&mut self) -> ReflectMut<'_>

Returns a mutable enumeration of “kinds” of type. Read more
Source§

fn reflect_owned(self: Box<Self>) -> ReflectOwned

Returns an owned enumeration of “kinds” of type. Read more
Source§

fn clone_value(&self) -> Box<dyn PartialReflect>

Clones the value as a Reflect trait object. Read more
Source§

fn reflect_hash(&self) -> Option<u64>

Returns a hash of the value (which includes the type). Read more
Source§

fn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool>

Returns a “partial equality” comparison result. Read more
Source§

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

Debug formatter for the value. Read more
Source§

fn serializable(&self) -> Option<Serializable<'_>>

Returns a serializable version of the value. Read more
Source§

fn is_dynamic(&self) -> bool

Indicates whether or not this type is a dynamic type. Read more
Source§

fn apply(&mut self, value: &dyn PartialReflect)

Applies a reflected value to this value. Read more
Source§

impl<'env> TypePath for DynamicFunction<'env>
where DynamicFunction<'env>: Any + Send + Sync,

Source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
Source§

fn short_type_path() -> &'static str

Returns a short, pretty-print enabled path to the type. Read more
Source§

fn type_ident() -> Option<&'static str>

Returns the name of the type, or None if it is anonymous. Read more
Source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
Source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynamicTypePath for T
where T: TypePath,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

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 T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,