Tracking Issue for lazy_cell · Issue #109736 · rust-lang/rust (original) (raw)

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; 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; 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

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html