Skip to content

Commit

Permalink
Move/rename lazy::{OnceCell, Lazy} to cell::{OnceCell, LazyCell}
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Jun 16, 2022
1 parent 392d272 commit 7c360dc
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 400 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ use rustc_target::abi::Size;

use libc::c_uint;
use smallvec::SmallVec;
use std::cell::OnceCell;
use std::cell::RefCell;
use std::iter;
use std::lazy::OnceCell;
use tracing::debug;

mod create_scope_map;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ use regex::Regex;
use tempfile::Builder as TempFileBuilder;

use std::borrow::Borrow;
use std::cell::OnceCell;
use std::collections::BTreeSet;
use std::ffi::OsString;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::lazy::OnceCell;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output, Stdio};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cfg_if! {
pub use std::cell::RefMut as LockGuard;
pub use std::cell::RefMut as MappedLockGuard;

pub use std::lazy::OnceCell;
pub use std::cell::OnceCell;

use std::cell::RefCell as InnerRwLock;
use std::cell::RefCell as InnerLock;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::{Path, PathBuf};
use tracing::{instrument, trace};

#[cfg(not(parallel_compiler))]
use std::lazy::Lazy;
use std::cell::LazyCell as Lazy;
#[cfg(parallel_compiler)]
use std::lazy::SyncLazy as Lazy;

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use rustc_span::{Span, DUMMY_SP};
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, WellFormedLoc};

use std::cell::LazyCell;
use std::convert::TryInto;
use std::iter;
use std::lazy::Lazy;
use std::ops::ControlFlow;

/// Helper type of a temporary returned by `.for_item(...)`.
Expand Down Expand Up @@ -1728,7 +1728,7 @@ fn check_variances_for_type_defn<'tcx>(
identify_constrained_generic_params(tcx, ty_predicates, None, &mut constrained_parameters);

// Lazily calculated because it is only needed in case of an error.
let explicitly_bounded_params = Lazy::new(|| {
let explicitly_bounded_params = LazyCell::new(|| {
let icx = crate::collect::ItemCtxt::new(tcx, item.def_id.to_def_id());
hir_generics
.predicates
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ use crate::mem;
use crate::ops::{CoerceUnsized, Deref, DerefMut};
use crate::ptr::{self, NonNull};

mod lazy;
mod once;

#[unstable(feature = "once_cell", issue = "74465")]
pub use lazy::LazyCell;
#[unstable(feature = "once_cell", issue = "74465")]
pub use once::OnceCell;

/// A mutable memory location.
///
/// # Examples
Expand Down
104 changes: 104 additions & 0 deletions library/core/src/cell/lazy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use crate::cell::{Cell, OnceCell};
use crate::fmt;
use crate::ops::Deref;

/// A value which is initialized on the first access.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::LazyCell;
///
/// let lazy: LazyCell<i32> = LazyCell::new(|| {
/// println!("initializing");
/// 92
/// });
/// println!("ready");
/// println!("{}", *lazy);
/// println!("{}", *lazy);
///
/// // Prints:
/// // ready
/// // initializing
/// // 92
/// // 92
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
pub struct LazyCell<T, F = fn() -> T> {
cell: OnceCell<T>,
init: Cell<Option<F>>,
}

impl<T, F> LazyCell<T, F> {
/// Creates a new lazy value with the given initializing function.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::LazyCell;
///
/// let hello = "Hello, World!".to_string();
///
/// let lazy = LazyCell::new(|| hello.to_uppercase());
///
/// assert_eq!(&*lazy, "HELLO, WORLD!");
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
pub const fn new(init: F) -> LazyCell<T, F> {
LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
}
}

impl<T, F: FnOnce() -> T> LazyCell<T, F> {
/// Forces the evaluation of this lazy value and returns a reference to
/// the result.
///
/// This is equivalent to the `Deref` impl, but is explicit.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::cell::LazyCell;
///
/// let lazy = LazyCell::new(|| 92);
///
/// assert_eq!(LazyCell::force(&lazy), &92);
/// assert_eq!(&*lazy, &92);
/// ```
#[unstable(feature = "once_cell", issue = "74465")]
pub fn force(this: &LazyCell<T, F>) -> &T {
this.cell.get_or_init(|| match this.init.take() {
Some(f) => f(),
None => panic!("`Lazy` instance has previously been poisoned"),
})
}
}

#[unstable(feature = "once_cell", issue = "74465")]
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
type Target = T;
fn deref(&self) -> &T {
LazyCell::force(self)
}
}

#[unstable(feature = "once_cell", issue = "74465")]
impl<T: Default> Default for LazyCell<T> {
/// Creates a new lazy value using `Default` as the initializing function.
fn default() -> LazyCell<T> {
LazyCell::new(T::default)
}
}

#[unstable(feature = "once_cell", issue = "74465")]
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()
}
}

0 comments on commit 7c360dc

Please sign in to comment.