1 use crate::cell::{Cell, OnceCell};
5 /// A value which is initialized on the first access.
7 /// For a thread-safe version of this struct, see [`std::sync::LazyLock`].
9 /// [`std::sync::LazyLock`]: ../../std/sync/struct.LazyLock.html
14 /// #![feature(once_cell)]
16 /// use std::cell::LazyCell;
18 /// let lazy: LazyCell<i32> = LazyCell::new(|| {
19 /// println!("initializing");
22 /// println!("ready");
23 /// println!("{}", *lazy);
24 /// println!("{}", *lazy);
32 #[unstable(feature = "once_cell", issue = "74465")]
33 pub struct LazyCell<T, F = fn() -> T> {
35 init: Cell<Option<F>>,
38 impl<T, F> LazyCell<T, F> {
39 /// Creates a new lazy value with the given initializing function.
44 /// #![feature(once_cell)]
46 /// use std::cell::LazyCell;
48 /// let hello = "Hello, World!".to_string();
50 /// let lazy = LazyCell::new(|| hello.to_uppercase());
52 /// assert_eq!(&*lazy, "HELLO, WORLD!");
54 #[unstable(feature = "once_cell", issue = "74465")]
55 pub const fn new(init: F) -> LazyCell<T, F> {
56 LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
60 impl<T, F: FnOnce() -> T> LazyCell<T, F> {
61 /// Forces the evaluation of this lazy value and returns a reference to
64 /// This is equivalent to the `Deref` impl, but is explicit.
69 /// #![feature(once_cell)]
71 /// use std::cell::LazyCell;
73 /// let lazy = LazyCell::new(|| 92);
75 /// assert_eq!(LazyCell::force(&lazy), &92);
76 /// assert_eq!(&*lazy, &92);
78 #[unstable(feature = "once_cell", issue = "74465")]
79 pub fn force(this: &LazyCell<T, F>) -> &T {
80 this.cell.get_or_init(|| match this.init.take() {
82 None => panic!("`Lazy` instance has previously been poisoned"),
87 #[unstable(feature = "once_cell", issue = "74465")]
88 impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
90 fn deref(&self) -> &T {
95 #[unstable(feature = "once_cell", issue = "74465")]
96 impl<T: Default> Default for LazyCell<T> {
97 /// Creates a new lazy value using `Default` as the initializing function.
98 fn default() -> LazyCell<T> {
99 LazyCell::new(T::default)
103 #[unstable(feature = "once_cell", issue = "74465")]
104 impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()