Struct sptr::func::OpaqueFnPtr
source · [−]#[repr(transparent)]pub struct OpaqueFnPtr(_);
Expand description
The void*
equivalent for a function pointer, for when you need to handle “some fn”.
Some platforms (WASM, AVR) have non-uniform representations for “code” and “data” pointers. Such platforms are referred to as “Harvard Architectures”.
Rust does not have a good way for talking about “some” function pointer, so it’s pretty common to cast to a usize or a raw pointer to make it easier. The entire point of strict provenance is to make you not do usize casts, so obviously we’re not a fan of that. But the other approach also isn’t great because Oxford Casts Are A Mess.
So really you want to stay in “definitely doing function pointers”, which means you want a proper opaque function pointer type. This type attempts to be that but is honestly not very good at it, because it immediately runs into the exact problem it’s trying to solve: Rust makes it really hard to talk about “some” function pointer, so we can’t actually describe its interface!
This really needs proper language support.
(In the meantime, func as usize
and usize as func
are genuinely the less evil casts
here! Don’t do Oxford Casts if you want your code to be maximally portable!)
Implementations
sourceimpl OpaqueFnPtr
impl OpaqueFnPtr
sourcepub unsafe fn from_fn<T>(func: T) -> Self
pub unsafe fn from_fn<T>(func: T) -> Self
Create an OpaqueFnPtr from some fn
.
Rust doesn’t have a good way to express, so this just takes “anything” and it’s up to you to make sure you’re actually feeding in a function pointer.
If you feed in anything else, it is Undefined Behaviour.
sourcepub unsafe fn to_fn<T>(self) -> T
pub unsafe fn to_fn<T>(self) -> T
Create a fn
from an OpaqueFnPtr.
Rust doesn’t have a good way to express, so this just takes “anything” and it’s up to you to make sure you’re actually feeding in a function pointer type.
If you feed in anything else, it is Undefined Behaviour.
sourcepub fn addr(self) -> usize
pub fn addr(self) -> usize
Get the address of the function pointer.
Note that while you can compare this to a data pointer, the result will almost certainly be meaningless, especially on platforms like WASM and AVR where function pointers are in a separate address-space from data pointers.
See pointer::addr
for details.
Trait Implementations
sourceimpl Clone for OpaqueFnPtr
impl Clone for OpaqueFnPtr
sourcefn clone(&self) -> OpaqueFnPtr
fn clone(&self) -> OpaqueFnPtr
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for OpaqueFnPtr
impl Debug for OpaqueFnPtr
sourceimpl Hash for OpaqueFnPtr
impl Hash for OpaqueFnPtr
sourceimpl Ord for OpaqueFnPtr
impl Ord for OpaqueFnPtr
sourceimpl PartialEq<OpaqueFnPtr> for OpaqueFnPtr
impl PartialEq<OpaqueFnPtr> for OpaqueFnPtr
sourcefn eq(&self, other: &OpaqueFnPtr) -> bool
fn eq(&self, other: &OpaqueFnPtr) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &OpaqueFnPtr) -> bool
fn ne(&self, other: &OpaqueFnPtr) -> bool
This method tests for !=
.
sourceimpl PartialOrd<OpaqueFnPtr> for OpaqueFnPtr
impl PartialOrd<OpaqueFnPtr> for OpaqueFnPtr
sourcefn partial_cmp(&self, other: &OpaqueFnPtr) -> Option<Ordering>
fn partial_cmp(&self, other: &OpaqueFnPtr) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for OpaqueFnPtr
impl Eq for OpaqueFnPtr
impl StructuralEq for OpaqueFnPtr
impl StructuralPartialEq for OpaqueFnPtr
Auto Trait Implementations
impl RefUnwindSafe for OpaqueFnPtr
impl Send for OpaqueFnPtr
impl Sync for OpaqueFnPtr
impl Unpin for OpaqueFnPtr
impl UnwindSafe for OpaqueFnPtr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more