Skip to content

Commit 7c360dc

Browse files
committed
Move/rename lazy::{OnceCell, Lazy} to cell::{OnceCell, LazyCell}
1 parent 392d272 commit 7c360dc

File tree

11 files changed

+407
-400
lines changed

11 files changed

+407
-400
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use rustc_target::abi::Size;
3636

3737
use libc::c_uint;
3838
use smallvec::SmallVec;
39+
use std::cell::OnceCell;
3940
use std::cell::RefCell;
4041
use std::iter;
41-
use std::lazy::OnceCell;
4242
use tracing::debug;
4343

4444
mod create_scope_map;

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ use regex::Regex;
3838
use tempfile::Builder as TempFileBuilder;
3939

4040
use std::borrow::Borrow;
41+
use std::cell::OnceCell;
4142
use std::collections::BTreeSet;
4243
use std::ffi::OsString;
4344
use std::fs::{File, OpenOptions};
4445
use std::io::{BufWriter, Write};
45-
use std::lazy::OnceCell;
4646
use std::ops::Deref;
4747
use std::path::{Path, PathBuf};
4848
use std::process::{ExitStatus, Output, Stdio};

compiler/rustc_data_structures/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ cfg_if! {
173173
pub use std::cell::RefMut as LockGuard;
174174
pub use std::cell::RefMut as MappedLockGuard;
175175

176-
pub use std::lazy::OnceCell;
176+
pub use std::cell::OnceCell;
177177

178178
use std::cell::RefCell as InnerRwLock;
179179
use std::cell::RefCell as InnerLock;

compiler/rustc_error_messages/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::path::{Path, PathBuf};
1818
use tracing::{instrument, trace};
1919

2020
#[cfg(not(parallel_compiler))]
21-
use std::lazy::Lazy;
21+
use std::cell::LazyCell as Lazy;
2222
#[cfg(parallel_compiler)]
2323
use std::lazy::SyncLazy as Lazy;
2424

compiler/rustc_typeck/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use rustc_span::{Span, DUMMY_SP};
2929
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
3030
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, WellFormedLoc};
3131

32+
use std::cell::LazyCell;
3233
use std::convert::TryInto;
3334
use std::iter;
34-
use std::lazy::Lazy;
3535
use std::ops::ControlFlow;
3636

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

17301730
// Lazily calculated because it is only needed in case of an error.
1731-
let explicitly_bounded_params = Lazy::new(|| {
1731+
let explicitly_bounded_params = LazyCell::new(|| {
17321732
let icx = crate::collect::ItemCtxt::new(tcx, item.def_id.to_def_id());
17331733
hir_generics
17341734
.predicates

library/core/src/cell.rs

+8
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ use crate::mem;
199199
use crate::ops::{CoerceUnsized, Deref, DerefMut};
200200
use crate::ptr::{self, NonNull};
201201

202+
mod lazy;
203+
mod once;
204+
205+
#[unstable(feature = "once_cell", issue = "74465")]
206+
pub use lazy::LazyCell;
207+
#[unstable(feature = "once_cell", issue = "74465")]
208+
pub use once::OnceCell;
209+
202210
/// A mutable memory location.
203211
///
204212
/// # Examples

library/core/src/cell/lazy.rs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use crate::cell::{Cell, OnceCell};
2+
use crate::fmt;
3+
use crate::ops::Deref;
4+
5+
/// A value which is initialized on the first access.
6+
///
7+
/// # Examples
8+
///
9+
/// ```
10+
/// #![feature(once_cell)]
11+
///
12+
/// use std::cell::LazyCell;
13+
///
14+
/// let lazy: LazyCell<i32> = LazyCell::new(|| {
15+
/// println!("initializing");
16+
/// 92
17+
/// });
18+
/// println!("ready");
19+
/// println!("{}", *lazy);
20+
/// println!("{}", *lazy);
21+
///
22+
/// // Prints:
23+
/// // ready
24+
/// // initializing
25+
/// // 92
26+
/// // 92
27+
/// ```
28+
#[unstable(feature = "once_cell", issue = "74465")]
29+
pub struct LazyCell<T, F = fn() -> T> {
30+
cell: OnceCell<T>,
31+
init: Cell<Option<F>>,
32+
}
33+
34+
impl<T, F> LazyCell<T, F> {
35+
/// Creates a new lazy value with the given initializing function.
36+
///
37+
/// # Examples
38+
///
39+
/// ```
40+
/// #![feature(once_cell)]
41+
///
42+
/// use std::cell::LazyCell;
43+
///
44+
/// let hello = "Hello, World!".to_string();
45+
///
46+
/// let lazy = LazyCell::new(|| hello.to_uppercase());
47+
///
48+
/// assert_eq!(&*lazy, "HELLO, WORLD!");
49+
/// ```
50+
#[unstable(feature = "once_cell", issue = "74465")]
51+
pub const fn new(init: F) -> LazyCell<T, F> {
52+
LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
53+
}
54+
}
55+
56+
impl<T, F: FnOnce() -> T> LazyCell<T, F> {
57+
/// Forces the evaluation of this lazy value and returns a reference to
58+
/// the result.
59+
///
60+
/// This is equivalent to the `Deref` impl, but is explicit.
61+
///
62+
/// # Examples
63+
///
64+
/// ```
65+
/// #![feature(once_cell)]
66+
///
67+
/// use std::cell::LazyCell;
68+
///
69+
/// let lazy = LazyCell::new(|| 92);
70+
///
71+
/// assert_eq!(LazyCell::force(&lazy), &92);
72+
/// assert_eq!(&*lazy, &92);
73+
/// ```
74+
#[unstable(feature = "once_cell", issue = "74465")]
75+
pub fn force(this: &LazyCell<T, F>) -> &T {
76+
this.cell.get_or_init(|| match this.init.take() {
77+
Some(f) => f(),
78+
None => panic!("`Lazy` instance has previously been poisoned"),
79+
})
80+
}
81+
}
82+
83+
#[unstable(feature = "once_cell", issue = "74465")]
84+
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
85+
type Target = T;
86+
fn deref(&self) -> &T {
87+
LazyCell::force(self)
88+
}
89+
}
90+
91+
#[unstable(feature = "once_cell", issue = "74465")]
92+
impl<T: Default> Default for LazyCell<T> {
93+
/// Creates a new lazy value using `Default` as the initializing function.
94+
fn default() -> LazyCell<T> {
95+
LazyCell::new(T::default)
96+
}
97+
}
98+
99+
#[unstable(feature = "once_cell", issue = "74465")]
100+
impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
101+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102+
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
103+
}
104+
}

0 commit comments

Comments
 (0)