You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
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 AtomicFnPtr should allow a "null" value? In which case it would wrap a Option<fn()> rather than a raw fn().
Variadic generics aren't even enough, since fn types can have higher rank lifetimes (e.g., fn(&str) -> &str is actually a for<'a> ... binder). That issue isn't specific to atomics (e.g., it also affects trait impls for function pointers, except the magic compiler-generated ones), so there's motivation for some way to abstract over those HRLBs, but I don't know if that's even really possible and I certainly haven't heard of any proposals for how to add it to Rust.
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.
Thinking of: fn<'a, 'b, T, U>(x: &'a T, y: &'b U) -> &'a T vs fn<'a, T, U>(x: &'a T, y: &'a U) -> &'a T vs fn<'a, T, U>(x: &'a T, y: U) -> &'a T, would variadic generic be enough?
Also have you considered whether AtomicFnPtr should allow a "null" value?
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: let x: fn()->() = std::ptr::null(); fails to compile.
@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 ;) ?
Activity
Amanieu commentedon Jun 20, 2018
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
AtomicFnPtrshould allow a "null" value? In which case it would wrap aOption<fn()>rather than a rawfn().hanna-kruppe commentedon Jun 20, 2018
Variadic generics aren't even enough, since fn types can have higher rank lifetimes (e.g.,
fn(&str) -> &stris actually afor<'a> ...binder). That issue isn't specific to atomics (e.g., it also affects trait impls for function pointers, except the magic compiler-generated ones), so there's motivation for some way to abstract over those HRLBs, but I don't know if that's even really possible and I certainly haven't heard of any proposals for how to add it to Rust.gnzlbg commentedon Jun 20, 2018
Thinking of:
fn<'a, 'b, T, U>(x: &'a T, y: &'b U) -> &'a Tvsfn<'a, T, U>(x: &'a T, y: &'a U) -> &'a Tvsfn<'a, T, U>(x: &'a T, y: U) -> &'a T, would variadic generic be enough?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:
let x: fn()->() = std::ptr::null();fails to compile.hanna-kruppe commentedon Jun 20, 2018
fntypes are non-null. Unsafely type-punning a null pointer to afnis instant UB.gnzlbg commentedon Jun 20, 2018
Then I think we need
AtomicFnPtr<Option<fn()->()>>. . .Amanieu commentedon Jun 20, 2018
At this point you might as well just use my crate.
Centril commentedon Oct 14, 2018
Ping @gnzlbg; what's the state of this?
( You can do:
AtomicPtr::new(foo as *mut _))kazcw commentedon May 10, 2019
Creating a null-valued fn pointer can't be UB, because it is safe:
sfackler commentedon May 10, 2019
@kazcw That's not a null function pointer, that's a null pointer to a function pointer.
josephlr commentedon Apr 17, 2020
This would be very useful for defining global handlers in a safe way:
This can be implemented in Rust right now by either:
stdAtomicUsizeandtransmuteIt would be nice to have a safe way to do this w/
no_std.fzyzcjy commentedon Oct 2, 2021
@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 ;) ?