1 use crate::cell::UnsafeCell;
3 use crate::marker::PhantomData;
4 use crate::mem::MaybeUninit;
5 use crate::panic::{RefUnwindSafe, UnwindSafe};
8 /// A synchronization primitive which can be written to only once.
10 /// This type is a thread-safe `OnceCell`.
15 /// #![feature(once_cell)]
17 /// use std::sync::OnceLock;
19 /// static CELL: OnceLock<String> = OnceLock::new();
20 /// assert!(CELL.get().is_none());
22 /// std::thread::spawn(|| {
23 /// let value: &String = CELL.get_or_init(|| {
24 /// "Hello, World!".to_string()
26 /// assert_eq!(value, "Hello, World!");
27 /// }).join().unwrap();
29 /// let value: Option<&String> = CELL.get();
30 /// assert!(value.is_some());
31 /// assert_eq!(value.unwrap().as_str(), "Hello, World!");
33 #[unstable(feature = "once_cell", issue = "74465")]
34 pub struct OnceLock<T> {
36 // Whether or not the value is initialized is tracked by `state_and_queue`.
37 value: UnsafeCell<MaybeUninit<T>>,
38 /// `PhantomData` to make sure dropck understands we're dropping T in our Drop impl.
40 /// ```compile_fail,E0597
41 /// #![feature(once_cell)]
43 /// use std::sync::OnceLock;
45 /// struct A<'a>(&'a str);
47 /// impl<'a> Drop for A<'a> {
48 /// fn drop(&mut self) {}
51 /// let cell = OnceLock::new();
53 /// let s = String::new();
54 /// let _ = cell.set(A(&s));
57 _marker: PhantomData<T>,
61 /// Creates a new empty cell.
62 #[unstable(feature = "once_cell", issue = "74465")]
64 pub const fn new() -> OnceLock<T> {
67 value: UnsafeCell::new(MaybeUninit::uninit()),
72 /// Gets the reference to the underlying value.
74 /// Returns `None` if the cell is empty, or being initialized. This
75 /// method never blocks.
76 #[unstable(feature = "once_cell", issue = "74465")]
77 pub fn get(&self) -> Option<&T> {
78 if self.is_initialized() {
79 // Safe b/c checked is_initialized
80 Some(unsafe { self.get_unchecked() })
86 /// Gets the mutable reference to the underlying value.
88 /// Returns `None` if the cell is empty. This method never blocks.
89 #[unstable(feature = "once_cell", issue = "74465")]
90 pub fn get_mut(&mut self) -> Option<&mut T> {
91 if self.is_initialized() {
92 // Safe b/c checked is_initialized and we have a unique access
93 Some(unsafe { self.get_unchecked_mut() })
99 /// Sets the contents of this cell to `value`.
101 /// May block if another thread is currently attempting to initialize the cell. The cell is
102 /// guaranteed to contain a value when set returns, though not necessarily the one provided.
104 /// Returns `Ok(())` if the cell's value was set by this call.
109 /// #![feature(once_cell)]
111 /// use std::sync::OnceLock;
113 /// static CELL: OnceLock<i32> = OnceLock::new();
116 /// assert!(CELL.get().is_none());
118 /// std::thread::spawn(|| {
119 /// assert_eq!(CELL.set(92), Ok(()));
120 /// }).join().unwrap();
122 /// assert_eq!(CELL.set(62), Err(62));
123 /// assert_eq!(CELL.get(), Some(&92));
126 #[unstable(feature = "once_cell", issue = "74465")]
127 pub fn set(&self, value: T) -> Result<(), T> {
128 let mut value = Some(value);
129 self.get_or_init(|| value.take().unwrap());
132 Some(value) => Err(value),
136 /// Gets the contents of the cell, initializing it with `f` if the cell
139 /// Many threads may call `get_or_init` concurrently with different
140 /// initializing functions, but it is guaranteed that only one function
141 /// will be executed.
145 /// If `f` panics, the panic is propagated to the caller, and the cell
146 /// remains uninitialized.
148 /// It is an error to reentrantly initialize the cell from `f`. The
149 /// exact outcome is unspecified. Current implementation deadlocks, but
150 /// this may be changed to a panic in the future.
155 /// #![feature(once_cell)]
157 /// use std::sync::OnceLock;
159 /// let cell = OnceLock::new();
160 /// let value = cell.get_or_init(|| 92);
161 /// assert_eq!(value, &92);
162 /// let value = cell.get_or_init(|| unreachable!());
163 /// assert_eq!(value, &92);
165 #[unstable(feature = "once_cell", issue = "74465")]
166 pub fn get_or_init<F>(&self, f: F) -> &T
170 match self.get_or_try_init(|| Ok::<T, !>(f())) {
175 /// Gets the contents of the cell, initializing it with `f` if
176 /// the cell was empty. If the cell was empty and `f` failed, an
177 /// error is returned.
181 /// If `f` panics, the panic is propagated to the caller, and
182 /// the cell remains uninitialized.
184 /// It is an error to reentrantly initialize the cell from `f`.
185 /// The exact outcome is unspecified. Current implementation
186 /// deadlocks, but this may be changed to a panic in the future.
191 /// #![feature(once_cell)]
193 /// use std::sync::OnceLock;
195 /// let cell = OnceLock::new();
196 /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
197 /// assert!(cell.get().is_none());
198 /// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
201 /// assert_eq!(value, Ok(&92));
202 /// assert_eq!(cell.get(), Some(&92))
204 #[unstable(feature = "once_cell", issue = "74465")]
205 pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
207 F: FnOnce() -> Result<T, E>,
210 // NOTE: We need to perform an acquire on the state in this method
211 // in order to correctly synchronize `LazyLock::force`. This is
212 // currently done by calling `self.get()`, which in turn calls
213 // `self.is_initialized()`, which in turn performs the acquire.
214 if let Some(value) = self.get() {
219 debug_assert!(self.is_initialized());
221 // SAFETY: The inner value has been initialized
222 Ok(unsafe { self.get_unchecked() })
225 /// Consumes the `OnceLock`, returning the wrapped value. Returns
226 /// `None` if the cell was empty.
231 /// #![feature(once_cell)]
233 /// use std::sync::OnceLock;
235 /// let cell: OnceLock<String> = OnceLock::new();
236 /// assert_eq!(cell.into_inner(), None);
238 /// let cell = OnceLock::new();
239 /// cell.set("hello".to_string()).unwrap();
240 /// assert_eq!(cell.into_inner(), Some("hello".to_string()));
242 #[unstable(feature = "once_cell", issue = "74465")]
243 pub fn into_inner(mut self) -> Option<T> {
247 /// Takes the value out of this `OnceLock`, moving it back to an uninitialized state.
249 /// Has no effect and returns `None` if the `OnceLock` hasn't been initialized.
251 /// Safety is guaranteed by requiring a mutable reference.
256 /// #![feature(once_cell)]
258 /// use std::sync::OnceLock;
260 /// let mut cell: OnceLock<String> = OnceLock::new();
261 /// assert_eq!(cell.take(), None);
263 /// let mut cell = OnceLock::new();
264 /// cell.set("hello".to_string()).unwrap();
265 /// assert_eq!(cell.take(), Some("hello".to_string()));
266 /// assert_eq!(cell.get(), None);
268 #[unstable(feature = "once_cell", issue = "74465")]
269 pub fn take(&mut self) -> Option<T> {
270 if self.is_initialized() {
271 self.once = Once::new();
272 // SAFETY: `self.value` is initialized and contains a valid `T`.
273 // `self.once` is reset, so `is_initialized()` will be false again
274 // which prevents the value from being read twice.
275 unsafe { Some((&mut *self.value.get()).assume_init_read()) }
282 fn is_initialized(&self) -> bool {
283 self.once.is_completed()
287 fn initialize<F, E>(&self, f: F) -> Result<(), E>
289 F: FnOnce() -> Result<T, E>,
291 let mut res: Result<(), E> = Ok(());
292 let slot = &self.value;
294 // Ignore poisoning from other threads
295 // If another thread panics, then we'll be able to run our closure
296 self.once.call_once_force(|p| {
299 unsafe { (&mut *slot.get()).write(value) };
304 // Treat the underlying `Once` as poisoned since we
305 // failed to initialize our value. Calls
315 /// The value must be initialized
316 unsafe fn get_unchecked(&self) -> &T {
317 debug_assert!(self.is_initialized());
318 (&*self.value.get()).assume_init_ref()
323 /// The value must be initialized
324 unsafe fn get_unchecked_mut(&mut self) -> &mut T {
325 debug_assert!(self.is_initialized());
326 (&mut *self.value.get()).assume_init_mut()
330 // Why do we need `T: Send`?
331 // Thread A creates a `OnceLock` and shares it with
332 // scoped thread B, which fills the cell, which is
333 // then destroyed by A. That is, destructor observes
335 #[unstable(feature = "once_cell", issue = "74465")]
336 unsafe impl<T: Sync + Send> Sync for OnceLock<T> {}
337 #[unstable(feature = "once_cell", issue = "74465")]
338 unsafe impl<T: Send> Send for OnceLock<T> {}
340 #[unstable(feature = "once_cell", issue = "74465")]
341 impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T> {}
342 #[unstable(feature = "once_cell", issue = "74465")]
343 impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
345 #[unstable(feature = "once_cell", issue = "74465")]
346 #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
347 impl<T> const Default for OnceLock<T> {
348 /// Creates a new empty cell.
353 /// #![feature(once_cell)]
355 /// use std::sync::OnceLock;
358 /// assert_eq!(OnceLock::<()>::new(), OnceLock::default());
361 fn default() -> OnceLock<T> {
366 #[unstable(feature = "once_cell", issue = "74465")]
367 impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
368 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
370 Some(v) => f.debug_tuple("Once").field(v).finish(),
371 None => f.write_str("Once(Uninit)"),
376 #[unstable(feature = "once_cell", issue = "74465")]
377 impl<T: Clone> Clone for OnceLock<T> {
378 fn clone(&self) -> OnceLock<T> {
379 let cell = Self::new();
380 if let Some(value) = self.get() {
381 match cell.set(value.clone()) {
383 Err(_) => unreachable!(),
390 #[unstable(feature = "once_cell", issue = "74465")]
391 impl<T> From<T> for OnceLock<T> {
392 /// Create a new cell with its contents set to `value`.
397 /// #![feature(once_cell)]
399 /// use std::sync::OnceLock;
401 /// # fn main() -> Result<(), i32> {
402 /// let a = OnceLock::from(3);
403 /// let b = OnceLock::new();
405 /// assert_eq!(a, b);
409 fn from(value: T) -> Self {
410 let cell = Self::new();
411 match cell.set(value) {
413 Err(_) => unreachable!(),
418 #[unstable(feature = "once_cell", issue = "74465")]
419 impl<T: PartialEq> PartialEq for OnceLock<T> {
420 fn eq(&self, other: &OnceLock<T>) -> bool {
421 self.get() == other.get()
425 #[unstable(feature = "once_cell", issue = "74465")]
426 impl<T: Eq> Eq for OnceLock<T> {}
428 #[unstable(feature = "once_cell", issue = "74465")]
429 unsafe impl<#[may_dangle] T> Drop for OnceLock<T> {
431 if self.is_initialized() {
432 // SAFETY: The cell is initialized and being dropped, so it can't
433 // be accessed again. We also don't touch the `T` other than
434 // dropping it, which validates our usage of #[may_dangle].
435 unsafe { (&mut *self.value.get()).assume_init_drop() };