pub struct Closure { /* private fields */ }Expand description
A light-weight closure, which is comprised of a function pointer and an environment parameter.
Implementations§
source§impl Closure
impl Closure
sourcepub const unsafe fn from_raw_parts(
func: unsafe extern "C" fn(_: ClosureEnv),
env: ClosureEnv
) -> Self
pub const unsafe fn from_raw_parts( func: unsafe extern "C" fn(_: ClosureEnv), env: ClosureEnv ) -> Self
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.
sourcepub const fn from_fn_const<T: FnOnce() + Copy + Send + 'static>(func: T) -> Self
pub const fn from_fn_const<T: FnOnce() + Copy + Send + 'static>(func: T) -> Self
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; });sourcepub const fn func(self) -> unsafe extern "C" fn(_: ClosureEnv)
pub const fn func(self) -> unsafe extern "C" fn(_: ClosureEnv)
Get the function pointer.
sourcepub const fn env(self) -> ClosureEnv
pub const fn env(self) -> ClosureEnv
Get the pojnter parameter.
sourcepub const fn as_raw_parts(
self
) -> (unsafe extern "C" fn(_: ClosureEnv), ClosureEnv)
pub const fn as_raw_parts( self ) -> (unsafe extern "C" fn(_: ClosureEnv), ClosureEnv)
Decompose self into raw components.
Trait Implementations§
source§impl IntoClosureConst for Closure
impl IntoClosureConst for Closure
source§const fn into_closure_const(self) -> Closure
const fn into_closure_const(self) -> Closure
Closure, potentially using a compile-time
heap.