-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Atomic function pointers #2481
Comments
The main issue that I can see here is that we would need variadic generics to make this work for all possible argument lists for the inner function type. Also have you considered whether |
Variadic generics aren't even enough, since fn types can have higher rank lifetimes (e.g., |
Thinking of:
I think "null" is a perfectly valid value for a raw fn pointer, but I don't know if it is possible to have one in Rust: |
|
Then I think we need |
At this point you might as well just use my crate. |
Ping @gnzlbg; what's the state of this? ( You can do: |
Creating a null-valued fn pointer can't be UB, because it is safe:
|
@kazcw That's not a null function pointer, that's a null pointer to a function pointer. |
This would be very useful for defining global handlers in a safe way:
This can be implemented in Rust right now by either:
It would be nice to have a safe way to do this w/ |
@Amanieu It works for me, thanks! But I wonder why does it work while the std one does not? In other words, why std forbid "AtomicFnPtr", while your implementation can allow us to do that - what magic do you do ;) ? |
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:emits
This is at the pure brainstorming stage, but enabling this use case is probably going to need an
AtomicFnPtr
or similar type unless we retrofitAtomicPtr
to also work withfn
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.The text was updated successfully, but these errors were encountered: