#[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

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.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.