| 1 | //! Tools for making it easier to use function pointers. |
| 2 | |
| 3 | /// The `void*` equivalent for a function pointer, for when you need to handle "some fn". |
| 4 | /// |
| 5 | /// Some platforms (WASM, AVR) have non-uniform representations for "code" and "data" pointers. |
| 6 | /// Such platforms are referred to as "Harvard Architectures". |
| 7 | /// |
| 8 | /// Rust does not have a good way for talking about "some" function pointer, so it's pretty |
| 9 | /// common to cast to a usize or a raw pointer to make it easier. The entire point of strict |
| 10 | /// provenance is to make you not do usize casts, so obviously we're not a fan of that. But the |
| 11 | /// other approach *also* isn't great because |
| 12 | /// [Oxford Casts Are A Mess](https://github.com/rust-lang/rust/issues/95489). |
| 13 | /// |
| 14 | /// So really you *want* to stay in "definitely doing function pointers", which means you want |
| 15 | /// a proper opaque function pointer type. This type *attempts* to be that but is honestly |
| 16 | /// not very good at it, because it immediately runs into the exact problem it's trying to solve: |
| 17 | /// Rust makes it really hard to talk about "some" function pointer, so we can't actually |
| 18 | /// describe its interface! |
| 19 | /// |
| 20 | /// This really needs proper language support. |
| 21 | /// |
| 22 | /// (In the meantime, `func as usize` and `usize as func` are genuinely the less evil casts |
| 23 | /// here! Don't do Oxford Casts if you want your code to be maximally portable!) |
| 24 | #[repr(transparent)] |
| 25 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] |
| 26 | pub struct OpaqueFnPtr(fn() -> ()); |
| 27 | |
| 28 | impl OpaqueFnPtr { |
| 29 | /// Create an OpaqueFnPtr from some `fn`. |
| 30 | /// |
| 31 | /// Rust doesn't have a good way to express, so this just takes "anything" and it's |
| 32 | /// up to you to make sure you're actually feeding in a function pointer. |
| 33 | /// |
| 34 | /// **If you feed in anything else, it is Undefined Behaviour.** |
| 35 | #[inline] |
| 36 | #[must_use] |
| 37 | pub unsafe fn from_fn<T>(func: T) -> Self { |
| 38 | assert_eq!( |
| 39 | core::mem::size_of::<T>(), |
| 40 | core::mem::size_of::<OpaqueFnPtr>() |
| 41 | ); |
| 42 | assert_eq!( |
| 43 | core::mem::align_of::<T>(), |
| 44 | core::mem::align_of::<OpaqueFnPtr>() |
| 45 | ); |
| 46 | |
| 47 | OpaqueFnPtr(core::mem::transmute_copy(&func)) |
| 48 | } |
| 49 | |
| 50 | /// Create a `fn` from an OpaqueFnPtr. |
| 51 | /// |
| 52 | /// Rust doesn't have a good way to express, so this just takes "anything" and it's |
| 53 | /// up to you to make sure you're actually feeding in a function pointer type. |
| 54 | /// |
| 55 | /// **If you feed in anything else, it is Undefined Behaviour.** |
| 56 | #[inline] |
| 57 | #[must_use] |
| 58 | pub unsafe fn to_fn<T>(self) -> T { |
| 59 | assert_eq!( |
| 60 | core::mem::size_of::<T>(), |
| 61 | core::mem::size_of::<OpaqueFnPtr>() |
| 62 | ); |
| 63 | assert_eq!( |
| 64 | core::mem::align_of::<T>(), |
| 65 | core::mem::align_of::<OpaqueFnPtr>() |
| 66 | ); |
| 67 | |
| 68 | core::mem::transmute_copy(&self.0) |
| 69 | } |
| 70 | |
| 71 | /// Get the address of the function pointer. |
| 72 | /// |
| 73 | /// Note that while you *can* compare this to a data pointer, the result will |
| 74 | /// almost certainly be meaningless, especially on platforms like WASM and AVR |
| 75 | /// where function pointers are in a separate address-space from data pointers. |
| 76 | /// |
| 77 | /// See [`pointer::addr`][crate::Strict::addr] for details. |
| 78 | #[inline] |
| 79 | #[must_use] |
| 80 | pub fn addr(self) -> usize { |
| 81 | self.0 as usize |
| 82 | } |
| 83 | } |