pub struct OpaqueFnPtr(/* private fields */);
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§
Source§impl 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§
Source§impl Clone for OpaqueFnPtr
impl Clone for OpaqueFnPtr
Source§fn clone(&self) -> OpaqueFnPtr
fn clone(&self) -> OpaqueFnPtr
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more