Skip to content

Commit 36aa3b2

Browse files
Merge pull request #11 from kkysen/const-fn-Atomic-new_from_primitive
Add `fn Atomic::from_impl`, a `const fn` way to create an `Atomic`, but from the underlying atomic impl directly
2 parents 3ac6418 + 97a43a3 commit 36aa3b2

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/impls.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
//! Traits for abstracting over `std` atomics. Basically implementation detail!
1+
//! Traits for abstracting over `std` atomics. Mostly hidden implementation detail.
22
//!
3-
//! This module only promises stability about the trait names and which types
4-
//! these traits are implemented by (though, new impls can be added at any
5-
//! time, of course). In particular, the traits' methods and other items are
6-
//! not part of the public API of `atomig`. Those items are also hidden in the
7-
//! documentation. And the traits are sealed anyway, so you can't implement
8-
//! them for your own types.
3+
//! Most items of these traits are hidden and not part of the public API of this library.
4+
//! You cannot implement these traits yourself.
95
106
use core::{num::Wrapping, sync::atomic::{self, Ordering}};
117
use super::{Atom, AtomLogic, AtomInteger};
@@ -28,7 +24,6 @@ mod sealed {
2824
/// the public API -- see the module docs.
2925
pub trait PrimitiveAtom: Sized + Copy + sealed::Sealed {
3026
/// The standard library type that is the atomic version of `Self`.
31-
#[doc(hidden)]
3227
type Impl;
3328

3429
#[doc(hidden)]

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,26 @@ impl<T: Atom> Atomic<T> {
341341
Self(T::Repr::into_impl(v.pack()))
342342
}
343343

344+
/// Creates a new atomic value from the underlying `Atomic*` type from `std`.
345+
///
346+
/// Since [`Atom`] is a `trait` and `const fn`s in `trait`s are not supported yet,
347+
/// the only way for this to be a `const fn` is
348+
/// to take the underyling atomic impl type directly.
349+
///
350+
/// This allows `static` `Atomic`s to be created.
351+
///
352+
/// # Examples
353+
///
354+
/// ```
355+
/// use atomig::Atomic;
356+
/// use std::sync::atomic::AtomicU32;
357+
///
358+
/// static X: Atomic<u32> = Atomic::from_impl(AtomicU32::new(7));
359+
/// ```
360+
pub const fn from_impl(v: <<T as Atom>::Repr as PrimitiveAtom>::Impl) -> Self {
361+
Self(v)
362+
}
363+
344364
/// Consumes the atomic and returns the contained value.
345365
///
346366
/// This is safe because passing `self` by value guarantees that no other

0 commit comments

Comments
 (0)