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§

source§

impl Closure

source

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.

source

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; });
source

pub fn call(self)

Call the closure.

source

pub const fn func(self) -> unsafe extern "C" fn(_: ClosureEnv)

Get the function pointer.

source

pub const fn env(self) -> ClosureEnv

Get the pojnter parameter.

source

pub const fn as_raw_parts( self ) -> (unsafe extern "C" fn(_: ClosureEnv), ClosureEnv)

Decompose self into raw components.

Trait Implementations§

source§

impl Clone for Closure

source§

fn clone(&self) -> Closure

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 Debug for Closure

source§

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

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

impl Default for Closure

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Init for Closure

source§

const INIT: Closure = _

The default value.
source§

impl IntoClosureConst for Closure

source§

const fn into_closure_const(self) -> Closure

Perform conversion to Closure, potentially using a compile-time heap.
source§

impl Copy for Closure

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.