| 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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 26 | pub struct OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#(fn() -> ()); |
| 27 | |
| 28 | impl OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/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_fnrust-analyzer cargo sptr 0.3.2 func/impl#[OpaqueFnPtr]from_fn().<T>(func: T) -> Selfrust-analyzer cargo sptr 0.3.2 func/impl#[OpaqueFnPtr] { |
| 38 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 39 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>(), |
| 40 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#>() |
| 41 | ); |
| 42 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 43 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>(), |
| 44 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#>() |
| 45 | ); |
| 46 | |
| 47 | OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/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_fnrust-analyzer cargo sptr 0.3.2 func/impl#[OpaqueFnPtr]to_fn().<T>(self) -> T { |
| 59 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 60 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>(), |
| 61 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#>() |
| 62 | ); |
| 63 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 64 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>(), |
| 65 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<OpaqueFnPtrrust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#>() |
| 66 | ); |
| 67 | |
| 68 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&self.0rust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#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 addrrust-analyzer cargo sptr 0.3.2 func/impl#[OpaqueFnPtr]addr().(self) -> usize { |
| 81 | self.0rust-analyzer cargo sptr 0.3.2 func/OpaqueFnPtr#0. as usize |
| 82 | } |
| 83 | } |