Skip to content

Commit

Permalink
link once_cell feature to rust-lang#74465
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jul 18, 2020
1 parent 48844fe commit fe63905
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
42 changes: 21 additions & 21 deletions src/libcore/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand All @@ -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)) }
Expand All @@ -82,15 +82,15 @@ 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) }
}

/// 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()
Expand All @@ -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()
Expand Down Expand Up @@ -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() };
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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>`.
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)) }
}
Expand All @@ -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(),
Expand All @@ -362,15 +362,15 @@ 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 {
Lazy::force(self)
}
}

#[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> {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit fe63905

Please sign in to comment.