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
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: