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:
committed by
Trevor Gross
parent
f98598c6cd
commit
dc4ba57566
@@ -26,7 +26,7 @@ union Data<T, F> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
/// #![feature(lazy_cell)]
|
||||
///
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
@@ -54,7 +54,7 @@ union Data<T, F> {
|
||||
/// // Some("Hoyten")
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
pub struct LazyLock<T, F = fn() -> T> {
|
||||
once: Once,
|
||||
data: UnsafeCell<Data<T, F>>,
|
||||
@@ -64,7 +64,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
||||
/// Creates a new lazy value with the given initializing
|
||||
/// function.
|
||||
#[inline]
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
pub const fn new(f: F) -> LazyLock<T, F> {
|
||||
LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }
|
||||
}
|
||||
@@ -76,7 +76,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
/// #![feature(lazy_cell)]
|
||||
///
|
||||
/// use std::sync::LazyLock;
|
||||
///
|
||||
@@ -86,7 +86,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
||||
/// assert_eq!(&*lazy, &92);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
pub fn force(this: &LazyLock<T, F>) -> &T {
|
||||
this.once.call_once(|| {
|
||||
// SAFETY: `call_once` only runs this closure once, ever.
|
||||
@@ -122,7 +122,7 @@ impl<T, F> LazyLock<T, F> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T, F> Drop for LazyLock<T, F> {
|
||||
fn drop(&mut self) {
|
||||
match self.once.state() {
|
||||
@@ -135,7 +135,7 @@ impl<T, F> Drop for LazyLock<T, F> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
|
||||
type Target = T;
|
||||
|
||||
@@ -145,7 +145,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T: Default> Default for LazyLock<T> {
|
||||
/// Creates a new lazy value using `Default` as the initializing function.
|
||||
#[inline]
|
||||
@@ -154,7 +154,7 @@ impl<T: Default> Default for LazyLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.get() {
|
||||
@@ -166,13 +166,13 @@ 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`
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F> {}
|
||||
// auto-derived `Send` impl is OK.
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F> {}
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F> {}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -172,9 +172,9 @@ pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[unstable(feature = "lazy_cell", issue = "109736")]
|
||||
pub use self::lazy_lock::LazyLock;
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub use self::once_lock::OnceLock;
|
||||
|
||||
pub(crate) use self::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
||||
|
||||
@@ -14,8 +14,6 @@ use crate::sync::Once;
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// static CELL: OnceLock<String> = OnceLock::new();
|
||||
@@ -32,7 +30,7 @@ use crate::sync::Once;
|
||||
/// assert!(value.is_some());
|
||||
/// assert_eq!(value.unwrap().as_str(), "Hello, World!");
|
||||
/// ```
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub struct OnceLock<T> {
|
||||
once: Once,
|
||||
// Whether or not the value is initialized is tracked by `once.is_completed()`.
|
||||
@@ -40,8 +38,6 @@ pub struct OnceLock<T> {
|
||||
/// `PhantomData` to make sure dropck understands we're dropping T in our Drop impl.
|
||||
///
|
||||
/// ```compile_fail,E0597
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// struct A<'a>(&'a str);
|
||||
@@ -63,7 +59,8 @@ impl<T> OnceLock<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() -> OnceLock<T> {
|
||||
OnceLock {
|
||||
once: Once::new(),
|
||||
@@ -77,7 +74,7 @@ impl<T> OnceLock<T> {
|
||||
/// Returns `None` if the cell is empty, or being initialized. This
|
||||
/// method never blocks.
|
||||
#[inline]
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub fn get(&self) -> Option<&T> {
|
||||
if self.is_initialized() {
|
||||
// Safe b/c checked is_initialized
|
||||
@@ -91,7 +88,7 @@ impl<T> OnceLock<T> {
|
||||
///
|
||||
/// Returns `None` if the cell is empty. This method never blocks.
|
||||
#[inline]
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub fn get_mut(&mut self) -> Option<&mut T> {
|
||||
if self.is_initialized() {
|
||||
// Safe b/c checked is_initialized and we have a unique access
|
||||
@@ -111,8 +108,6 @@ impl<T> OnceLock<T> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// static CELL: OnceLock<i32> = OnceLock::new();
|
||||
@@ -129,7 +124,7 @@ impl<T> OnceLock<T> {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub fn set(&self, value: T) -> Result<(), T> {
|
||||
let mut value = Some(value);
|
||||
self.get_or_init(|| value.take().unwrap());
|
||||
@@ -158,8 +153,6 @@ impl<T> OnceLock<T> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// let cell = OnceLock::new();
|
||||
@@ -169,7 +162,7 @@ impl<T> OnceLock<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,
|
||||
@@ -195,7 +188,7 @@ impl<T> OnceLock<T> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
/// #![feature(once_cell_try)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
@@ -209,7 +202,7 @@ impl<T> OnceLock<T> {
|
||||
/// assert_eq!(cell.get(), Some(&92))
|
||||
/// ```
|
||||
#[inline]
|
||||
#[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>,
|
||||
@@ -236,8 +229,6 @@ impl<T> OnceLock<T> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// let cell: OnceLock<String> = OnceLock::new();
|
||||
@@ -248,7 +239,7 @@ impl<T> OnceLock<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(mut self) -> Option<T> {
|
||||
self.take()
|
||||
}
|
||||
@@ -262,8 +253,6 @@ impl<T> OnceLock<T> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// let mut cell: OnceLock<String> = OnceLock::new();
|
||||
@@ -275,7 +264,7 @@ impl<T> OnceLock<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> {
|
||||
if self.is_initialized() {
|
||||
self.once = Once::new();
|
||||
@@ -344,17 +333,17 @@ impl<T> OnceLock<T> {
|
||||
// scoped thread B, which fills the cell, which is
|
||||
// then destroyed by A. That is, destructor observes
|
||||
// a sent value.
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
unsafe impl<T: Sync + Send> Sync for OnceLock<T> {}
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
unsafe impl<T: Send> Send for OnceLock<T> {}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T> {}
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
||||
impl<T> const Default for OnceLock<T> {
|
||||
/// Creates a new empty cell.
|
||||
@@ -362,8 +351,6 @@ impl<T> const Default for OnceLock<T> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// fn main() {
|
||||
@@ -376,7 +363,7 @@ impl<T> const Default for OnceLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.get() {
|
||||
@@ -386,7 +373,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: Clone> Clone for OnceLock<T> {
|
||||
#[inline]
|
||||
fn clone(&self) -> OnceLock<T> {
|
||||
@@ -401,15 +388,13 @@ impl<T: Clone> Clone for OnceLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T> From<T> for OnceLock<T> {
|
||||
/// Create a new cell with its contents set to `value`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(once_cell)]
|
||||
///
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// # fn main() -> Result<(), i32> {
|
||||
@@ -430,7 +415,7 @@ impl<T> From<T> for OnceLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: PartialEq> PartialEq for OnceLock<T> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &OnceLock<T>) -> bool {
|
||||
@@ -438,10 +423,10 @@ impl<T: PartialEq> PartialEq for OnceLock<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
impl<T: Eq> Eq for OnceLock<T> {}
|
||||
|
||||
#[unstable(feature = "once_cell", issue = "74465")]
|
||||
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
|
||||
unsafe impl<#[may_dangle] T> Drop for OnceLock<T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user