Closed
Description
Note: lazy_cell_consume
is now tracked at #125623
This supercedes #74465 after a portion of once_cell
was stabilized with #105587
Feature gate: #![feature(lazy_cell)]
This is a tracking issue for the LazyCell
and LazyLock
types, which are designed to support convenient one-time initialization. One of the main goals is to be able to replace the lazy_static
crate, as well as once_cell::{sync, unsync}::Lazy
.
Public API
// core::cell (in core/src/cell/lazy.rs)
pub struct LazyCell<T, F = fn() -> T> { /* ... */ }
impl<T, F: FnOnce() -> T> LazyCell<T, F> {
pub const fn new(init: F) -> LazyCell<T, F>;
pub fn force(this: &LazyCell<T, F>) -> &T;
}
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
type Target = T;
}
impl<T: Default> Default for LazyCell<T>;
impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F>;
// std::sync (in std/sync/lazy_lock.rs)
pub struct LazyLock<T, F = fn() -> T> { /* ... */ }
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
pub const fn new(f: F) -> LazyLock<T, F>;
pub fn force(this: &LazyLock<T, F>) -> &T;
}
impl<T, F> Drop for LazyLock<T, F>;
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
type Target = T;
}
impl<T: Default> Default for LazyLock<T>;
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F>;
// We never create a `&F` from a `&LazyLock<T, F>` so it is fine
// to not impl `Sync` for `F`
unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F>;
// auto-derived `Send` impl is OK.
impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F>;
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F>;
Steps / History
- Implementation: Add lazy initialization primitives to std #72414
- Final comment period (FCP)1
- Stabilization PR - Stabilize
LazyCell
andLazyLock
#121377
Unresolved Questions
- Is variance of Lazy correct? (See Feature request: Make
Lazy<T, F>
covariant inF
matklad/once_cell#167) - Default
F = fn() -> T
in type signature (See Tracking Issue forlazy_cell
#109736 (comment))