From 4137adeba301b41f4ff5d2c8b3e5dd8ce918d13b Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 10 Oct 2020 20:14:33 +0200 Subject: [PATCH] Return a Pin from SyncOnceCell::get_or_init_pin. --- library/std/src/lazy.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index 091c69ca083a..68f57958bb23 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -321,13 +321,15 @@ impl SyncOnceCell { /// It is an error to reentrantly initialize the cell from `f`. The exact /// outcome is unspecified. Current implementation deadlocks, but this may /// be changed to a panic in the future. - pub(crate) fn get_or_init_pin(self: Pin<&Self>, f: F, g: G) -> &T + pub(crate) fn get_or_init_pin(self: Pin<&Self>, f: F, g: G) -> Pin<&T> where F: FnOnce() -> T, G: FnOnce(Pin<&mut T>), { if let Some(value) = self.get_ref().get() { - return value; + // SAFETY: The inner value was already initialized, and will not be + // moved anymore. + return unsafe { Pin::new_unchecked(value) }; } let slot = &self.value; @@ -345,8 +347,9 @@ impl SyncOnceCell { g(unsafe { Pin::new_unchecked(value) }); }); - // SAFETY: The inner value has been initialized. - unsafe { self.get_ref().get_unchecked() } + // SAFETY: The inner value has been initialized, and will not be moved + // anymore. + unsafe { Pin::new_unchecked(self.get_ref().get_unchecked()) } } /// Consumes the `SyncOnceCell`, returning the wrapped value. Returns