-
Notifications
You must be signed in to change notification settings - Fork 20
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
ACP: Generic Atomic<T>
#443
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Comments
We discussed this in the libs-api meeting just now. Please go ahead with these changes, as long as none of the changes affect the stable public interface. |
We can probably just stabilize 128 bit atomics before we stablize |
This was referenced Sep 19, 2024
workingjubilee
added a commit
to workingjubilee/rustc
that referenced
this issue
Sep 19, 2024
Create `Atomic<T>` type alias and use it in core/alloc/std where possible, ignoring test files for now. This is step one, creating the alias from `Atomic<T>` to `AtomicT`. The next step of flipping this and aliasing `AtomicT` to `Atomic<T>` will have a bigger impact, since `AtomicT` imports can be dropped once `Atomic::new` is a usable name. First commit is the true change. Second commit is mostly mechanical replacement of `AtomicT` type names with `Atomic<T>`. See [how this was done for `NonZero`](rust-lang#120257) for the rough blueprint I'm following. - ACP: rust-lang/libs-team#443 (comment) - Tracking issue: rust-lang#130539
13 tasks
bors
added a commit
to rust-lang-ci/rust
that referenced
this issue
Feb 6, 2025
Create `Atomic<T>` type alias and use it in core/alloc/std where possible, ignoring test files for now. This is step one, creating the alias from `Atomic<T>` to `AtomicT`. The next step of flipping this and aliasing `AtomicT` to `Atomic<T>` will have a bigger impact, since `AtomicT` imports can be dropped once `Atomic::new` is a usable name. First commit is the true change. Second commit is mostly mechanical replacement of `AtomicT` type names with `Atomic<T>`. See [how this was done for `NonZero`](rust-lang#120257) for the rough blueprint I'm following. - ACP: rust-lang/libs-team#443 (comment) - Tracking issue: rust-lang#130539
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
The
std::sync::atomic
module contains 12 differentAtomic*
types with nearly identical API. However, there's no good way to associate a primitive type with its counterpart atomic cell, nor to use atomics with the various FFI integer type aliases.Like was done for
NonZero*
in 1.79, this can be cleanly addressed with a generic typeAtomic<T: AtomicPrimitive>
.This ACP does not propose to support
Atomic<T>
for any types that do not already have anAtomic*
type defined.Solution sketch
Implementation would proceed in a few steps:
Atomic<T>
as a type alias toAtomicT
.Atomic<T>
instead ofAtomicT
.AtomicT
is an alias of generic structAtomic<T>
.This ACP only proposes unifying the nonatomic methods initially. The other methods can and should be eventually unified, but this limits the initial scope and avoids trying to unify disparate stability annotations, or addressing
AtomicPtr
andAtomicBool
's slightly different API.128-bit atomics are interesting, since they are still unstable, but trait implementations cannot be unstable. To avoid premature stabilization of
Atomic<i128>
, eitherAtomicI128
can be left as a separately implemented type, orAtomic
can mention an unstable middleman marker type instead ofi128
directly as the generic.The result API would be about: (cfgs omitted)
I am volunteering to do this implementation work, if this is determined a desirable refactor.
Alternatives
This API plan only encodes the equivalent of
cfg(target_has_atomic_load_store = "N")
into the type system (asiN: AtomicPrimitive
). The other two atomic cfgs could also be encoded. Notably,target_has_atomic_equal_alignment
has a reasonably meaningful potential spelling ofT: AtomicPrimitive<AlignedForAtomic = T>
.This can be done in a 3rd party crate instead of std, and crates already exist to provide this or a more general
AtomicCell
. However, the target ideal end state (functions onAtomic<T>
and notAtomic*
) provides a cleaner API surface for Rust than the current apparent duplication.Additionally, using a generic enables future work that extends
Atomic<*mut T>
to also support*const T
,&T
, andNonNull<T>
, if so desired, as well as atomic double (fat) pointers.Links and related work
NonZero*
's migration to use a generic: Tracking Issue for genericNonZero
rust#120257radium
crate'sAtom<T>
works like this ACP'sAtomic<T>
. (The crate primarily exists to provide a unified API for atomics andCell
.)What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: