Re: [PATCH v14 1/7] rust: sync: add `OnceLock`
From: Alice Ryhl
Date: Wed Jul 02 2025 - 11:35:49 EST
On Wed, Jul 2, 2025 at 5:07 PM Benno Lossin <lossin@xxxxxxxxxx> wrote:
>
> On Wed Jul 2, 2025 at 3:18 PM CEST, Andreas Hindborg wrote:
> > +impl<T: Copy> OnceLock<T> {
> > + /// Get a copy of the contained object.
> > + ///
> > + /// Returns [`None`] if the [`OnceLock`] is empty.
> > + pub fn copy(&self) -> Option<T> {
> > + if self.init.load(Acquire) == 2 {
> > + // SAFETY: As determined by the load above, the object is ready for shared access.
> > + Some(unsafe { *self.value.get() })
> > + } else {
> > + None
> > + }
>
> The impl can just be:
>
> self.as_ref().copied()
>
> Would it make sense for this function to take `self` instead & we make
> the `OnceLock` also `Copy` if `T: Copy`? Maybe not...
Atomics are not Copy.
Alice