Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4137ade

Browse files
m-ou-seMark-Simulacrum
authored andcommitted
Return a Pin from SyncOnceCell::get_or_init_pin.
1 parent 91cb628 commit 4137ade

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

library/std/src/lazy.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,15 @@ impl<T> SyncOnceCell<T> {
321321
/// It is an error to reentrantly initialize the cell from `f`. The exact
322322
/// outcome is unspecified. Current implementation deadlocks, but this may
323323
/// be changed to a panic in the future.
324-
pub(crate) fn get_or_init_pin<F, G>(self: Pin<&Self>, f: F, g: G) -> &T
324+
pub(crate) fn get_or_init_pin<F, G>(self: Pin<&Self>, f: F, g: G) -> Pin<&T>
325325
where
326326
F: FnOnce() -> T,
327327
G: FnOnce(Pin<&mut T>),
328328
{
329329
if let Some(value) = self.get_ref().get() {
330-
return value;
330+
// SAFETY: The inner value was already initialized, and will not be
331+
// moved anymore.
332+
return unsafe { Pin::new_unchecked(value) };
331333
}
332334

333335
let slot = &self.value;
@@ -345,8 +347,9 @@ impl<T> SyncOnceCell<T> {
345347
g(unsafe { Pin::new_unchecked(value) });
346348
});
347349

348-
// SAFETY: The inner value has been initialized.
349-
unsafe { self.get_ref().get_unchecked() }
350+
// SAFETY: The inner value has been initialized, and will not be moved
351+
// anymore.
352+
unsafe { Pin::new_unchecked(self.get_ref().get_unchecked()) }
350353
}
351354

352355
/// Consumes the `SyncOnceCell`, returning the wrapped value. Returns

0 commit comments

Comments
 (0)