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
Unresolved Questions
Note:
lazy_cell_consumeis now tracked at #125623This supercedes #74465 after a portion of
once_cellwas stabilized with #105587Feature gate:
#![feature(lazy_cell)]This is a tracking issue for the
LazyCellandLazyLocktypes, which are designed to support convenient one-time initialization. One of the main goals is to be able to replace thelazy_staticcrate, as well asonce_cell::{sync, unsync}::Lazy.Public API
Steps / History
LazyCellandLazyLock#121377Unresolved Questions
Lazy<T, F>covariant inFmatklad/once_cell#167)F = fn() -> Tin type signature (See Tracking Issue forlazy_cell#109736 (comment))Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩