FnPtr

Trait FnPtr 

Source
pub unsafe trait FnPtr:
    Sized
    + Copy
    + Send
    + Sync {
    type CC: Default + Copy + Send + Sync;
    type Args<'a, 'b, 'c>;
    type Ret<'a, 'b, 'c>;

    // Required methods
    unsafe fn call<'a, 'b, 'c>(
        self,
        args: Self::Args<'a, 'b, 'c>,
    ) -> Self::Ret<'a, 'b, 'c>;
    unsafe fn from_ptr(ptr: *const ()) -> Self;
    fn to_ptr(self) -> *const ();
    fn make_once_thunk<F>(fun: F) -> impl FnOnceThunk<Self>
       where F: for<'a, 'b, 'c> PackedFnOnce<'a, 'b, 'c, Self>;
    fn make_mut_thunk<F>(fun: F) -> impl FnMutThunk<Self>
       where F: for<'a, 'b, 'c> PackedFnMut<'a, 'b, 'c, Self>;
    fn make_thunk<F>(fun: F) -> impl FnThunk<Self>
       where F: for<'a, 'b, 'c> PackedFn<'a, 'b, 'c, Self>;
}
Expand description

Trait implemented by unsafe function pointer types of up to 12 arguments.

Allows introspection of the function’s calling convention, arguments, return type, and provides a call method for invoking the function.

§Limitations

The trait cannot be automatically implemented for higher-kinded (i.e. for <'a> fn) bare functions. For these, use the bare_hrtb! macro to create a transparent wrapper type which implements the trait. Furthermore, the trait cannot be implemented at all for higher-kinded bare functions which have more than 3 independent lifetimes.

§Safety

  • The trait must not be implemented on a type that is not #[repr(transparent)] with a function pointer, i.e. has a different size/alignment.

  • When implemented on a non-function pointer type that is #[repr(transparent)] to a function pointer, all associated types (CC, Args and Ret) must be consistent with the function pointer.

Required Associated Types§

Source

type CC: Default + Copy + Send + Sync

Marker type for the bare function’s calling convention.

This is required to be a ZST; in particular it must be safe to create an instance via core::mem::zeroed or a reference via core::ptr::dangling.

Source

type Args<'a, 'b, 'c>

The arguments of the function, as a tuple.

This is a GAT with 3 independent lifetimes to support most higher-kinded bare functions.

When the tuple_trait crate feature is enabled, this associated type has a core::marker::Tuple bound. Note that this also requires the tuple_trait nightly feature.

Source

type Ret<'a, 'b, 'c>

The return type of the function.

This is a GAT with 3 independent lifetimes to support most higher-kinded bare functions.

Required Methods§

Source

unsafe fn call<'a, 'b, 'c>( self, args: Self::Args<'a, 'b, 'c>, ) -> Self::Ret<'a, 'b, 'c>

Calls self.

§Safety

The same function-specific safety invariants must be upheld as when calling it directly.

Source

unsafe fn from_ptr(ptr: *const ()) -> Self

Creates Self from an untyped pointer.

§Safety

The untyped pointer must point to a valid instance of Self.

Source

fn to_ptr(self) -> *const ()

Casts self to an untyped pointer.

Source

fn make_once_thunk<F>(fun: F) -> impl FnOnceThunk<Self>
where F: for<'a, 'b, 'c> PackedFnOnce<'a, 'b, 'c, Self>,

Creates a FnOnceThunk implementation from a closure taking the same arguments as this function pointer.

Source

fn make_mut_thunk<F>(fun: F) -> impl FnMutThunk<Self>
where F: for<'a, 'b, 'c> PackedFnMut<'a, 'b, 'c, Self>,

Creates a FnMutThunk implementation from a closure taking the same arguments as this function pointer.

Source

fn make_thunk<F>(fun: F) -> impl FnThunk<Self>
where F: for<'a, 'b, 'c> PackedFn<'a, 'b, 'c, Self>,

Creates a FnThunk implementation from a closure taking the same arguments as this function pointer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§