Struct r3_core::closure::Closure

source ·
pub struct Closure { /* private fields */ }
Expand description

A light-weight closure, which is comprised of a function pointer and an environment parameter.

Implementations

Construct a Self from a function pointer and an associated pointer parameter.

Safety

Safe code that has access to the constructed Self will be able to execute func(env, _). A corollary is that, if func has additional safety requirements that are not covered by Closure, they are lost by this function, which means the resulting Closure mustn’t be exposed to safe code.

Construct a Self from the given closure at compile time.

The conversion may involve compile-time heap allocation (core::intrinsics::const_allocate). It’s illegal to call this function at runtime.

Examples
use r3_core::closure::Closure;

// Zero-sized
const C1: Closure = Closure::from_fn_const(|| {});

// CTFE-heap-allocated
const C2: Closure = {
    let x = 42;
    Closure::from_fn_const(move || assert_eq!(x, 42))
};

C1.call();
C2.call();

Don’t call it at runtime:

use r3_core::closure::Closure;
let x = [1, 2, 3];
Closure::from_fn_const(move || { let _x = x; });

Call the closure.

Get the function pointer.

Get the pojnter parameter.

Decompose self into raw components.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The default value.
Perform conversion to Closure, potentially using a compile-time heap. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.