Description
Following the discussion in rust-lang/rust#51559 I am opening this to track the lack of atomic function pointers in the std
library (and the ecosystem in general).
We currently have std::sync::atomic::AtomicPtr
but it does not work with function pointers by design:
fn foo() {}
static Foo: AtomicPtr<fn()->()> = AtomicPtr::new(foo);
emits
error[E0308]: mismatched types
--> src/main.rs:5:50
|
5 | static Foo: AtomicPtr<fn()->()> = AtomicPtr::new(foo);
| ^^^ expected *-ptr, found fn item
|
= note: expected type `*mut fn()`
found type `fn() {foo}`
This is at the pure brainstorming stage, but enabling this use case is probably going to need an AtomicFnPtr
or similar type unless we retrofit AtomicPtr
to also work with fn
items.
EDIT: relevant threads/rfcs:
[0] pre-RFC: Extended atomic types proposes a generic atomic type that uses constraints to be generic. I don't know if that approach can be feasible extended to function pointer types.
[1] RFC 1543 (merged): Add more integer atomic types. Only mentions AtomicPtr
, but it does not mention pointers to functions.
[2] Function pointers can be stored in atomic usize
s just fine.