Partial stabilization of once_cell by tgross35 · Pull Request #105587 · rust-lang/rust (original) (raw)

This PR aims to stabilize a portion of the once_cell feature:

This will leave LazyCell and LazyLock unstabilized, which have been moved to the lazy_cell feature flag.

Tracking issue: #74465 (does not fully close, but it may make sense to move to a new issue)

Future steps for separate PRs:

To be stabilized API summary

// core::cell (in core/cell/once.rs)

pub struct OnceCell { .. }

impl OnceCell { pub const fn new() -> OnceCell; pub fn get(&self) -> Option<&T>; pub fn get_mut(&mut self) -> Option<&mut T>; pub fn set(&self, value: T) -> Result<(), T>; pub fn get_or_init(&self, f: F) -> &T where F: FnOnce() -> T; pub fn into_inner(self) -> Option; pub fn take(&mut self) -> Option; }

impl<T: Clone> Clone for OnceCell; impl<T: Debug> Debug for OnceCell impl Default for OnceCell; impl From for OnceCell; impl<T: PartialEq> PartialEq for OnceCell; impl<T: Eq> Eq for OnceCell;

// std::sync (in std/sync/once_lock.rs)

impl OnceLock { pub const fn new() -> OnceLock; pub fn get(&self) -> Option<&T>; pub fn get_mut(&mut self) -> Option<&mut T>; pub fn set(&self, value: T) -> Result<(), T>; pub fn get_or_init(&self, f: F) -> &T where F: FnOnce() -> T; pub fn into_inner(self) -> Option; pub fn take(&mut self) -> Option; }

impl<T: Clone> Clone for OnceLock; impl<T: Debug> Debug for OnceLock; impl Default for OnceLock; impl<#[may_dangle] T> Drop for OnceLock; impl From for OnceLock; impl<T: PartialEq> PartialEq for OnceLock impl<T: Eq> Eq for OnceLock; impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock; unsafe impl<T: Send> Send for OnceLock; unsafe impl<T: Sync + Send> Sync for OnceLock; impl<T: UnwindSafe> UnwindSafe for OnceLock;

No longer planned as part of this PR, and moved to the rust_cell_try feature gate:

impl OnceCell { pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>; }

impl OnceLock { pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>; }

I am new to this process so would appreciate mentorship wherever needed.

Post-merge update

With this merge, LazyCell and LazyLock have been moved under the gate lazy_cell (see #109736) and the get_or_try_init functions are under once_cell_try (see #109737)