Rollup merge of #160079 - RalfJung:const-atomic, r=folkertdev make atomic operations const In a similar vein to https://github.com/rust-lang/rust/issues/159094, it makes sense to make these `const fn` so that code can be shared between compile-time and run-time even if the runtime version has to deal with concurrency. This constifies the following: ```rust // core::sync::atomic impl AtomicBool { pub const fn get_mut(&mut self) -> &mut bool; pub const fn from_mut(v: &mut bool) -> &mut Self; pub const fn get_mut_slice(this: &mut [Self]) -> &mut [bool]; pub const fn from_mut_slice(v: &mut [bool]) -> &mut [Self]; pub const fn load(&self, order: Ordering) -> bool; pub const fn store(&self, val: bool, order: Ordering); pub const fn swap(&self, val: bool, order: Ordering) -> bool; pub const fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool; pub const fn compare_exchange( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>; pub const fn compare_exchange_weak( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>; pub const fn fetch_and(&self, val: bool, order: Ordering) -> bool; pub const fn fetch_nand(&self, val: bool, order: Ordering) -> bool; pub const fn fetch_or(&self, val: bool, order: Ordering) -> bool; pub const fn fetch_xor(&self, val: bool, order: Ordering) -> bool; pub const fn fetch_not(&self, order: Ordering) -> bool; } impl AtomicPtr<T> { pub const fn get_mut(&mut self) -> &mut *mut T; pub const fn from_mut(v: &mut *mut T) -> &mut Self; pub const fn get_mut_slice(this: &mut [Self]) -> &mut [*mut T]; pub const fn from_mut_slice(v: &mut [*mut T]) -> &mut [Self]; pub const fn load(&self, order: Ordering) -> *mut T; pub const fn store(&self, ptr: *mut T, order: Ordering); pub const fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T; } impl Atomic$Int { pub const fn get_mut(&mut self) -> &mut $int_type; pub const fn from_mut(v: &mut $int_type) -> &mut Self; pub const fn get_mut_slice(this: &mut [Self]) -> &mut [$int_type]; pub const fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self]; pub const fn load(&self, order: Ordering) -> $int_type; pub const fn store(&self, val: $int_type, order: Ordering); pub const fn swap(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn compare_and_swap(&self, current: $int_type, new: $int_type, order: Ordering) -> $int_type; pub const fn compare_exchange(&self, current: $int_type, new: $int_type, success: Ordering, failure: Ordering) -> Result<$int_type, $int_type>; pub const fn compare_exchange_weak(&self, current: $int_type, new: $int_type, success: Ordering, failure: Ordering) -> Result<$int_type, $int_type>; pub const fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type; pub const fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type; } pub const fn fence(order: Ordering); pub const fn compiler_fence(order: Ordering); ``` However, this excludes the `fetch_*` and `compare_*` operations on `AtomicPtr`, as that kind of pointer arithmetic is not possible at compile time. Tracking issue: https://github.com/rust-lang/rust/issues/160078 Cc @rust-lang/wg-const-eval @rust-lang/opsem
Website | Getting started | Learn | Documentation | Contributing
This is the main source code repository for Rust. It contains the compiler, standard library, and documentation.
Performance: Fast and memory-efficient, suitable for critical services, embedded devices, and easily integrated with other languages.
Reliability: Our rich type system and ownership model ensure memory and thread safety, reducing bugs at compile-time.
Productivity: Comprehensive documentation, a compiler committed to providing great diagnostics, and advanced tooling including package manager and build tool (Cargo), auto-formatter (rustfmt), linter (Clippy) and editor support (rust-analyzer).
Read “Installation” from The Book.
If you really want to install from source (though this is not recommended), see INSTALL.md.
See https://www.rust-lang.org/community for a list of chat platforms and forums.
See CONTRIBUTING.md.
For a detailed explanation of the compiler's architecture and how to begin contributing, see the rustc-dev-guide.
Rust is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.
The Rust Foundation owns and protects the Rust and Cargo trademarks and logos (the “Rust Trademarks”).
If you want to use these names or brands, please read the Rust language trademark policy.
Third-party logos may be subject to third-party copyrights and trademarks. See Licenses for details.