link once_cell feature to #74465

This commit is contained in:
Ashley Mannix 2020-07-18 10:09:47 +10:00
parent 48844fed2b
commit fe63905708
4 changed files with 51 additions and 51 deletions

View File

@ -26,20 +26,20 @@ use crate::ops::Deref;
/// assert_eq!(value, "Hello, World!");
/// assert!(cell.get().is_some());
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub struct OnceCell<T> {
// Invariant: written to at most once.
inner: UnsafeCell<Option<T>>,
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T> Default for OnceCell<T> {
fn default() -> Self {
Self::new()
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.get() {
@ -49,7 +49,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Clone> Clone for OnceCell<T> {
fn clone(&self) -> OnceCell<T> {
let res = OnceCell::new();
@ -63,17 +63,17 @@ impl<T: Clone> Clone for OnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: PartialEq> PartialEq for OnceCell<T> {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Eq> Eq for OnceCell<T> {}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T> From<T> for OnceCell<T> {
fn from(value: T) -> Self {
OnceCell { inner: UnsafeCell::new(Some(value)) }
@ -82,7 +82,7 @@ impl<T> From<T> for OnceCell<T> {
impl<T> OnceCell<T> {
/// Creates a new empty cell.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub const fn new() -> OnceCell<T> {
OnceCell { inner: UnsafeCell::new(None) }
}
@ -90,7 +90,7 @@ impl<T> OnceCell<T> {
/// Gets the reference to the underlying value.
///
/// Returns `None` if the cell is empty.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get(&self) -> Option<&T> {
// Safety: Safe due to `inner`'s invariant
unsafe { &*self.inner.get() }.as_ref()
@ -99,7 +99,7 @@ impl<T> OnceCell<T> {
/// Gets the mutable reference to the underlying value.
///
/// Returns `None` if the cell is empty.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get_mut(&mut self) -> Option<&mut T> {
// Safety: Safe because we have unique access
unsafe { &mut *self.inner.get() }.as_mut()
@ -127,7 +127,7 @@ impl<T> OnceCell<T> {
///
/// assert!(cell.get().is_some());
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn set(&self, value: T) -> Result<(), T> {
// Safety: Safe because we cannot have overlapping mutable borrows
let slot = unsafe { &*self.inner.get() };
@ -168,7 +168,7 @@ impl<T> OnceCell<T> {
/// let value = cell.get_or_init(|| unreachable!());
/// assert_eq!(value, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get_or_init<F>(&self, f: F) -> &T
where
F: FnOnce() -> T,
@ -206,7 +206,7 @@ impl<T> OnceCell<T> {
/// assert_eq!(value, Ok(&92));
/// assert_eq!(cell.get(), Some(&92))
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
where
F: FnOnce() -> Result<T, E>,
@ -241,7 +241,7 @@ impl<T> OnceCell<T> {
/// cell.set("hello".to_string()).unwrap();
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
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>`.
@ -269,7 +269,7 @@ impl<T> OnceCell<T> {
/// assert_eq!(cell.take(), Some("hello".to_string()));
/// assert_eq!(cell.get(), None);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn take(&mut self) -> Option<T> {
mem::take(self).into_inner()
}
@ -298,13 +298,13 @@ impl<T> OnceCell<T> {
/// // 92
/// // 92
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub struct Lazy<T, F = fn() -> T> {
cell: OnceCell<T>,
init: Cell<Option<F>>,
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: fmt::Debug, F> fmt::Debug for Lazy<T, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
@ -329,7 +329,7 @@ impl<T, F> Lazy<T, F> {
/// assert_eq!(&*lazy, "HELLO, WORLD!");
/// # }
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub const fn new(init: F) -> Lazy<T, F> {
Lazy { cell: OnceCell::new(), init: Cell::new(Some(init)) }
}
@ -353,7 +353,7 @@ impl<T, F: FnOnce() -> T> Lazy<T, F> {
/// assert_eq!(Lazy::force(&lazy), &92);
/// assert_eq!(&*lazy, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn force(this: &Lazy<T, F>) -> &T {
this.cell.get_or_init(|| match this.init.take() {
Some(f) => f(),
@ -362,7 +362,7 @@ impl<T, F: FnOnce() -> T> Lazy<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
type Target = T;
fn deref(&self) -> &T {
@ -370,7 +370,7 @@ impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Default> Default for Lazy<T> {
/// Creates a new lazy value using `Default` as the initializing function.
fn default() -> Lazy<T> {

View File

@ -239,7 +239,7 @@ pub mod char;
pub mod ffi;
#[cfg(not(test))] // See #65860
pub mod iter;
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub mod lazy;
pub mod option;
pub mod panic;

View File

@ -10,7 +10,7 @@ use crate::{
};
#[doc(inline)]
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub use core::lazy::*;
/// A synchronization primitive which can be written to only once.
@ -38,7 +38,7 @@ pub use core::lazy::*;
/// assert!(value.is_some());
/// assert_eq!(value.unwrap().as_str(), "Hello, World!");
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub struct SyncOnceCell<T> {
once: Once,
// Whether or not the value is initialized is tracked by `state_and_queue`.
@ -50,24 +50,24 @@ pub struct SyncOnceCell<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 = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
unsafe impl<T: Sync + Send> Sync for SyncOnceCell<T> {}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
unsafe impl<T: Send> Send for SyncOnceCell<T> {}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SyncOnceCell<T> {}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: UnwindSafe> UnwindSafe for SyncOnceCell<T> {}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T> Default for SyncOnceCell<T> {
fn default() -> SyncOnceCell<T> {
SyncOnceCell::new()
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.get() {
@ -77,7 +77,7 @@ impl<T: fmt::Debug> fmt::Debug for SyncOnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Clone> Clone for SyncOnceCell<T> {
fn clone(&self) -> SyncOnceCell<T> {
let cell = Self::new();
@ -91,7 +91,7 @@ impl<T: Clone> Clone for SyncOnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T> From<T> for SyncOnceCell<T> {
fn from(value: T) -> Self {
let cell = Self::new();
@ -102,19 +102,19 @@ impl<T> From<T> for SyncOnceCell<T> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: PartialEq> PartialEq for SyncOnceCell<T> {
fn eq(&self, other: &SyncOnceCell<T>) -> bool {
self.get() == other.get()
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Eq> Eq for SyncOnceCell<T> {}
impl<T> SyncOnceCell<T> {
/// Creates a new empty cell.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub const fn new() -> SyncOnceCell<T> {
SyncOnceCell { once: Once::new(), value: UnsafeCell::new(MaybeUninit::uninit()) }
}
@ -123,7 +123,7 @@ impl<T> SyncOnceCell<T> {
///
/// Returns `None` if the cell is empty, or being initialized. This
/// method never blocks.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get(&self) -> Option<&T> {
if self.is_initialized() {
// Safe b/c checked is_initialized
@ -136,7 +136,7 @@ impl<T> SyncOnceCell<T> {
/// Gets the mutable reference to the underlying value.
///
/// Returns `None` if the cell is empty. This method never blocks.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
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
@ -170,7 +170,7 @@ impl<T> SyncOnceCell<T> {
/// assert_eq!(CELL.get(), Some(&92));
/// }
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn set(&self, value: T) -> Result<(), T> {
let mut value = Some(value);
self.get_or_init(|| value.take().unwrap());
@ -209,7 +209,7 @@ impl<T> SyncOnceCell<T> {
/// let value = cell.get_or_init(|| unreachable!());
/// assert_eq!(value, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get_or_init<F>(&self, f: F) -> &T
where
F: FnOnce() -> T,
@ -248,7 +248,7 @@ impl<T> SyncOnceCell<T> {
/// assert_eq!(value, Ok(&92));
/// assert_eq!(cell.get(), Some(&92))
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
where
F: FnOnce() -> Result<T, E>,
@ -286,7 +286,7 @@ impl<T> SyncOnceCell<T> {
/// cell.set("hello".to_string()).unwrap();
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn into_inner(mut self) -> Option<T> {
// Safety: Safe because we immediately free `self` without dropping
let inner = unsafe { self.take_inner() };
@ -318,7 +318,7 @@ impl<T> SyncOnceCell<T> {
/// assert_eq!(cell.take(), Some("hello".to_string()));
/// assert_eq!(cell.get(), None);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn take(&mut self) -> Option<T> {
mem::take(self).into_inner()
}
@ -428,13 +428,13 @@ impl<T> Drop for SyncOnceCell<T> {
/// // Some("Hoyten")
/// }
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub struct SyncLazy<T, F = fn() -> T> {
cell: SyncOnceCell<T>,
init: Cell<Option<F>>,
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: fmt::Debug, F> fmt::Debug for SyncLazy<T, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
@ -446,17 +446,17 @@ impl<T: fmt::Debug, F> fmt::Debug for SyncLazy<T, F> {
// we do create a `&mut Option<F>` in `force`, but this is
// properly synchronized, so it only happens once
// so it also does not contribute to this impl.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
unsafe impl<T, F: Send> Sync for SyncLazy<T, F> where SyncOnceCell<T>: Sync {}
// auto-derived `Send` impl is OK.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T, F: RefUnwindSafe> RefUnwindSafe for SyncLazy<T, F> where SyncOnceCell<T>: RefUnwindSafe {}
impl<T, F> SyncLazy<T, F> {
/// Creates a new lazy value with the given initializing
/// function.
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub const fn new(f: F) -> SyncLazy<T, F> {
SyncLazy { cell: SyncOnceCell::new(), init: Cell::new(Some(f)) }
}
@ -479,7 +479,7 @@ impl<T, F: FnOnce() -> T> SyncLazy<T, F> {
/// assert_eq!(SyncLazy::force(&lazy), &92);
/// assert_eq!(&*lazy, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub fn force(this: &SyncLazy<T, F>) -> &T {
this.cell.get_or_init(|| match this.init.take() {
Some(f) => f(),
@ -488,7 +488,7 @@ impl<T, F: FnOnce() -> T> SyncLazy<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T, F: FnOnce() -> T> Deref for SyncLazy<T, F> {
type Target = T;
fn deref(&self) -> &T {
@ -496,7 +496,7 @@ impl<T, F: FnOnce() -> T> Deref for SyncLazy<T, F> {
}
}
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Default> Default for SyncLazy<T> {
/// Creates a new lazy value using `Default` as the initializing function.
fn default() -> SyncLazy<T> {

View File

@ -478,7 +478,7 @@ pub mod process;
pub mod sync;
pub mod time;
#[unstable(feature = "once_cell", issue = "68198")]
#[unstable(feature = "once_cell", issue = "74465")]
pub mod lazy;
#[stable(feature = "futures_api", since = "1.36.0")]