Stabilize a portion of 'once_cell'

Move items not part of this stabilization to 'lazy_cell' or 'once_cell_try'
This commit is contained in:
Trevor Gross
2022-12-12 00:42:45 -05:00
committed by Trevor Gross
parent f98598c6cd
commit dc4ba57566
40 changed files with 87 additions and 128 deletions

View File

@@ -11,7 +11,7 @@ use crate::ops::Deref;
/// # Examples
///
/// ```
/// #![feature(once_cell)]
/// #![feature(lazy_cell)]
///
/// use std::cell::LazyCell;
///
@@ -29,7 +29,7 @@ use crate::ops::Deref;
/// // 92
/// // 92
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
pub struct LazyCell<T, F = fn() -> T> {
cell: OnceCell<T>,
init: Cell<Option<F>>,
@@ -41,7 +41,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
/// #![feature(lazy_cell)]
///
/// use std::cell::LazyCell;
///
@@ -52,7 +52,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
/// assert_eq!(&*lazy, "HELLO, WORLD!");
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
pub const fn new(init: F) -> LazyCell<T, F> {
LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
}
@@ -65,7 +65,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
/// #![feature(lazy_cell)]
///
/// use std::cell::LazyCell;
///
@@ -75,7 +75,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
/// assert_eq!(&*lazy, &92);
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
pub fn force(this: &LazyCell<T, F>) -> &T {
this.cell.get_or_init(|| match this.init.take() {
Some(f) => f(),
@@ -84,7 +84,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
type Target = T;
#[inline]
@@ -93,7 +93,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
impl<T: Default> Default for LazyCell<T> {
/// Creates a new lazy value using `Default` as the initializing function.
#[inline]
@@ -102,7 +102,7 @@ impl<T: Default> Default for LazyCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "lazy_cell", issue = "109736")]
impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()

View File

@@ -16,8 +16,6 @@ use crate::mem;
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::OnceCell;
///
/// let cell = OnceCell::new();
@@ -29,7 +27,7 @@ use crate::mem;
/// assert_eq!(value, "Hello, World!");
/// assert!(cell.get().is_some());
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub struct OnceCell<T> {
// Invariant: written to at most once.
inner: UnsafeCell<Option<T>>,
@@ -39,7 +37,8 @@ impl<T> OnceCell<T> {
/// Creates a new empty cell.
#[inline]
#[must_use]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub const fn new() -> OnceCell<T> {
OnceCell { inner: UnsafeCell::new(None) }
}
@@ -48,7 +47,7 @@ impl<T> OnceCell<T> {
///
/// Returns `None` if the cell is empty.
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn get(&self) -> Option<&T> {
// SAFETY: Safe due to `inner`'s invariant
unsafe { &*self.inner.get() }.as_ref()
@@ -58,7 +57,7 @@ impl<T> OnceCell<T> {
///
/// Returns `None` if the cell is empty.
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn get_mut(&mut self) -> Option<&mut T> {
self.inner.get_mut().as_mut()
}
@@ -73,8 +72,6 @@ impl<T> OnceCell<T> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::OnceCell;
///
/// let cell = OnceCell::new();
@@ -86,7 +83,7 @@ impl<T> OnceCell<T> {
/// assert!(cell.get().is_some());
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn set(&self, value: T) -> Result<(), T> {
// SAFETY: Safe because we cannot have overlapping mutable borrows
let slot = unsafe { &*self.inner.get() };
@@ -117,8 +114,6 @@ impl<T> OnceCell<T> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::OnceCell;
///
/// let cell = OnceCell::new();
@@ -128,7 +123,7 @@ impl<T> OnceCell<T> {
/// assert_eq!(value, &92);
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn get_or_init<F>(&self, f: F) -> &T
where
F: FnOnce() -> T,
@@ -153,7 +148,7 @@ impl<T> OnceCell<T> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
/// #![feature(once_cell_try)]
///
/// use std::cell::OnceCell;
///
@@ -166,7 +161,7 @@ impl<T> OnceCell<T> {
/// assert_eq!(value, Ok(&92));
/// assert_eq!(cell.get(), Some(&92))
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
#[unstable(feature = "once_cell_try", issue = "109737")]
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
where
F: FnOnce() -> Result<T, E>,
@@ -199,8 +194,6 @@ impl<T> OnceCell<T> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::OnceCell;
///
/// let cell: OnceCell<String> = OnceCell::new();
@@ -211,7 +204,7 @@ impl<T> OnceCell<T> {
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn into_inner(self) -> Option<T> {
// Because `into_inner` takes `self` by value, the compiler statically verifies
// that it is not currently borrowed. So it is safe to move out `Option<T>`.
@@ -227,8 +220,6 @@ impl<T> OnceCell<T> {
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::OnceCell;
///
/// let mut cell: OnceCell<String> = OnceCell::new();
@@ -240,13 +231,13 @@ impl<T> OnceCell<T> {
/// assert_eq!(cell.get(), None);
/// ```
#[inline]
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
pub fn take(&mut self) -> Option<T> {
mem::take(self).into_inner()
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for OnceCell<T> {
#[inline]
fn default() -> Self {
@@ -254,7 +245,7 @@ impl<T> Default for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.get() {
@@ -264,7 +255,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: Clone> Clone for OnceCell<T> {
#[inline]
fn clone(&self) -> OnceCell<T> {
@@ -279,7 +270,7 @@ impl<T: Clone> Clone for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: PartialEq> PartialEq for OnceCell<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
@@ -287,10 +278,11 @@ impl<T: PartialEq> PartialEq for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: Eq> Eq for OnceCell<T> {}
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T> const From<T> for OnceCell<T> {
/// Creates a new `OnceCell<T>` which already contains the given `value`.
#[inline]
@@ -300,5 +292,5 @@ impl<T> const From<T> for OnceCell<T> {
}
// Just like for `Cell<T>` this isn't needed, but results in nicer error messages.
#[unstable(feature = "once_cell", issue = "74465")]
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T> !Sync for OnceCell<T> {}