Re: [PATCH v14 1/7] rust: sync: add `OnceLock`

From: Benno Lossin
Date: Wed Jul 02 2025 - 11:44:59 EST


On Wed Jul 2, 2025 at 5:27 PM CEST, Alice Ryhl wrote:
> 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.

Ah right... Yeah it probably also isn't useful.

---
Cheers,
Benno