Explain LazyCell in core::cell overview · model-checking/verify-rust-std@a198721 (original) (raw)
`@@ -82,6 +82,20 @@
`
82
82
`//!
`
83
83
`` //! The corresponding [Sync
] version of OnceCell<T>
is [OnceLock<T>
].
``
84
84
`//!
`
``
85
`` +
//! ## LazyCell<T, F>
``
``
86
`+
//!
`
``
87
`+
//! A common pattern with OnceCell is, for a given OnceCell, to use the same function on every
`
``
88
`` +
//! call to [OnceCell::get_or_init
] with that cell. This is what is offered by [LazyCell
],
``
``
89
`` +
//! which pairs cells of T
with functions of F
, and always calls F
before it yields &T
.
``
``
90
`+
//! This happens implicitly by simply attempting to dereference the LazyCell to get its contents,
`
``
91
`+
//! so its use is much more transparent with a place which has been initialized by a constant.
`
``
92
`+
//!
`
``
93
`` +
//! More complicated patterns that don't fit this description can be built on OnceCell<T>
instead.
``
``
94
`+
//!
`
``
95
`` +
//! LazyCell
works by providing an implementation of impl Deref
that calls the function,
``
``
96
`` +
//! so you can just use it by dereference (e.g. *lazy_cell
or lazy_cell.deref()
).
``
``
97
`+
//!
`
``
98
`` +
//! The corresponding [Sync
] version of LazyCell<T, F>
is [LazyLock<T, F>
].
``
85
99
`//!
`
86
100
`//! # When to choose interior mutability
`
87
101
`//!
`
230
244
`` //! [RwLock<T>
]: ../../std/sync/struct.RwLock.html
``
231
245
`` //! [Mutex<T>
]: ../../std/sync/struct.Mutex.html
``
232
246
`` //! [OnceLock<T>
]: ../../std/sync/struct.OnceLock.html
``
``
247
`` +
//! [LazyLock<T, F>
]: ../../std/sync/struct.LazyLock.html
``
233
248
`` //! [Sync
]: ../../std/marker/trait.Sync.html
``
234
249
`` //! [atomic
]: crate::sync::atomic
``
235
250
``