Skip to content

Commit cf15e71

Browse files
committed
Rename OnceCell methods to be more consistent with std conventions
1 parent 38030ff commit cf15e71

File tree

14 files changed

+49
-46
lines changed

14 files changed

+49
-46
lines changed

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ pub fn create_global_ctxt<'tcx>(
766766
}
767767

768768
let gcx = sess.time("setup_global_ctxt", || {
769-
global_ctxt.get_or_init(|| {
769+
global_ctxt.get_or_insert_with(|| {
770770
TyCtxt::create_global_ctxt(
771771
sess,
772772
lint_store,

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn rustc_path<'a>() -> Option<&'a Path> {
278278

279279
const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
280280

281-
RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_ref().map(|v| &**v)
281+
RUSTC_PATH.get_or_insert_with(|| get_rustc_path_inner(BIN_PATH)).as_ref().map(|v| &**v)
282282
}
283283

284284
fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17131713
}
17141714
};
17151715

1716-
self.cdata.source_map_import_info.get_or_init(|| {
1716+
self.cdata.source_map_import_info.get_or_insert_with(|| {
17171717
let external_source_map = self.root.source_map.decode(self);
17181718

17191719
external_source_map

compiler/rustc_middle/src/mir/predecessors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl PredecessorCache {
3939
&self,
4040
basic_blocks: &IndexVec<BasicBlock, BasicBlockData<'_>>,
4141
) -> &Predecessors {
42-
self.cache.get_or_init(|| {
42+
self.cache.get_or_insert_with(|| {
4343
let mut preds = IndexVec::from_elem(SmallVec::new(), basic_blocks);
4444
for (bb, data) in basic_blocks.iter_enumerated() {
4545
if let Some(term) = &data.terminator {

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl<'sess> OnDiskCache<'sess> {
433433
T: Decodable<CacheDecoder<'a, 'tcx>>,
434434
{
435435
let cnum_map =
436-
self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
436+
self.cnum_map.get_or_insert_with(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
437437

438438
let mut decoder = CacheDecoder {
439439
tcx,

compiler/rustc_mir/src/dataflow/framework/graphviz.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ where
567567
macro_rules! regex {
568568
($re:literal $(,)?) => {{
569569
static RE: SyncOnceCell<regex::Regex> = SyncOnceCell::new();
570-
RE.get_or_init(|| Regex::new($re).unwrap())
570+
RE.get_or_insert_with(|| Regex::new($re).unwrap())
571571
}};
572572
}
573573

compiler/rustc_mir/src/transform/coverage/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
130130
pub(crate) fn debug_options<'a>() -> &'a DebugOptions {
131131
static DEBUG_OPTIONS: SyncOnceCell<DebugOptions> = SyncOnceCell::new();
132132

133-
&DEBUG_OPTIONS.get_or_init(|| DebugOptions::from_env())
133+
&DEBUG_OPTIONS.get_or_insert_with(|| DebugOptions::from_env())
134134
}
135135

136136
/// Parses and maintains coverage-specific debug options captured from the environment variable

compiler/rustc_mir_build/src/thir/pattern/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
392392
}
393393

394394
fn head_ctor<'a>(&'a self, cx: &MatchCheckCtxt<'p, 'tcx>) -> &'a Constructor<'tcx> {
395-
self.head_ctor.get_or_init(|| pat_constructor(cx, self.head()))
395+
self.head_ctor.get_or_insert_with(|| pat_constructor(cx, self.head()))
396396
}
397397

398398
fn iter(&self) -> impl Iterator<Item = &Pat<'tcx>> {

library/core/src/lazy.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::ops::Deref;
2020
/// let cell = OnceCell::new();
2121
/// assert!(cell.get().is_none());
2222
///
23-
/// let value: &String = cell.get_or_init(|| {
23+
/// let value: &String = cell.get_or_insert_with(|| {
2424
/// "Hello, World!".to_string()
2525
/// });
2626
/// assert_eq!(value, "Hello, World!");
@@ -163,17 +163,17 @@ impl<T> OnceCell<T> {
163163
/// use std::lazy::OnceCell;
164164
///
165165
/// let cell = OnceCell::new();
166-
/// let value = cell.get_or_init(|| 92);
166+
/// let value = cell.get_or_insert_with(|| 92);
167167
/// assert_eq!(value, &92);
168-
/// let value = cell.get_or_init(|| unreachable!());
168+
/// let value = cell.get_or_insert_with(|| unreachable!());
169169
/// assert_eq!(value, &92);
170170
/// ```
171171
#[unstable(feature = "once_cell", issue = "74465")]
172-
pub fn get_or_init<F>(&self, f: F) -> &T
172+
pub fn get_or_insert_with<F>(&self, f: F) -> &T
173173
where
174174
F: FnOnce() -> T,
175175
{
176-
match self.get_or_try_init(|| Ok::<T, !>(f())) {
176+
match self.try_get_or_insert_with(|| Ok::<T, !>(f())) {
177177
Ok(val) => val,
178178
}
179179
}
@@ -198,16 +198,16 @@ impl<T> OnceCell<T> {
198198
/// use std::lazy::OnceCell;
199199
///
200200
/// let cell = OnceCell::new();
201-
/// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
201+
/// assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(()));
202202
/// assert!(cell.get().is_none());
203-
/// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
203+
/// let value = cell.try_get_or_insert_with(|| -> Result<i32, ()> {
204204
/// Ok(92)
205205
/// });
206206
/// assert_eq!(value, Ok(&92));
207207
/// assert_eq!(cell.get(), Some(&92))
208208
/// ```
209209
#[unstable(feature = "once_cell", issue = "74465")]
210-
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
210+
pub fn try_get_or_insert_with<F, E>(&self, f: F) -> Result<&T, E>
211211
where
212212
F: FnOnce() -> Result<T, E>,
213213
{
@@ -355,7 +355,7 @@ impl<T, F: FnOnce() -> T> Lazy<T, F> {
355355
/// ```
356356
#[unstable(feature = "once_cell", issue = "74465")]
357357
pub fn force(this: &Lazy<T, F>) -> &T {
358-
this.cell.get_or_init(|| match this.init.take() {
358+
this.cell.get_or_insert_with(|| match this.init.take() {
359359
Some(f) => f(),
360360
None => panic!("`Lazy` instance has previously been poisoned"),
361361
})

library/core/tests/lazy.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use core::{
88
fn once_cell() {
99
let c = OnceCell::new();
1010
assert!(c.get().is_none());
11-
c.get_or_init(|| 92);
11+
c.get_or_insert_with(|| 92);
1212
assert_eq!(c.get(), Some(&92));
1313

14-
c.get_or_init(|| panic!("Kabom!"));
14+
c.get_or_insert_with(|| panic!("Kabom!"));
1515
assert_eq!(c.get(), Some(&92));
1616
}
1717

@@ -35,7 +35,7 @@ fn once_cell_drop() {
3535
}
3636

3737
let x = OnceCell::new();
38-
x.get_or_init(|| Dropper);
38+
x.get_or_insert_with(|| Dropper);
3939
assert_eq!(DROP_CNT.load(SeqCst), 0);
4040
drop(x);
4141
assert_eq!(DROP_CNT.load(SeqCst), 1);
@@ -115,8 +115,8 @@ fn aliasing_in_get() {
115115
fn reentrant_init() {
116116
let x: OnceCell<Box<i32>> = OnceCell::new();
117117
let dangling_ref: Cell<Option<&i32>> = Cell::new(None);
118-
x.get_or_init(|| {
119-
let r = x.get_or_init(|| Box::new(92));
118+
x.get_or_insert_with(|| {
119+
let r = x.get_or_insert_with(|| Box::new(92));
120120
dangling_ref.set(Some(r));
121121
Box::new(62)
122122
});

library/std/src/io/stdio.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub struct StdinLock<'a> {
310310
pub fn stdin() -> Stdin {
311311
static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
312312
Stdin {
313-
inner: INSTANCE.get_or_init(|| {
313+
inner: INSTANCE.get_or_insert_with(|| {
314314
Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
315315
}),
316316
}
@@ -549,7 +549,7 @@ pub fn stdout() -> Stdout {
549549
static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> =
550550
SyncOnceCell::new();
551551
Stdout {
552-
inner: INSTANCE.get_or_init(|| unsafe {
552+
inner: INSTANCE.get_or_insert_with(|| unsafe {
553553
let _ = sys_common::at_exit(|| {
554554
if let Some(instance) = INSTANCE.get() {
555555
// Flush the data and disable buffering during shutdown
@@ -764,7 +764,7 @@ pub fn stderr() -> Stderr {
764764
static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
765765

766766
Stderr {
767-
inner: INSTANCE.get_or_init(|| unsafe {
767+
inner: INSTANCE.get_or_insert_with(|| unsafe {
768768
let r = ReentrantMutex::new(RefCell::new(stderr_raw()));
769769
r.init();
770770
r

library/std/src/lazy.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use core::lazy::*;
3232
/// assert!(CELL.get().is_none());
3333
///
3434
/// std::thread::spawn(|| {
35-
/// let value: &String = CELL.get_or_init(|| {
35+
/// let value: &String = CELL.get_or_insert_with(|| {
3636
/// "Hello, World!".to_string()
3737
/// });
3838
/// assert_eq!(value, "Hello, World!");
@@ -201,7 +201,7 @@ impl<T> SyncOnceCell<T> {
201201
#[unstable(feature = "once_cell", issue = "74465")]
202202
pub fn set(&self, value: T) -> Result<(), T> {
203203
let mut value = Some(value);
204-
self.get_or_init(|| value.take().unwrap());
204+
self.get_or_insert_with(|| value.take().unwrap());
205205
match value {
206206
None => Ok(()),
207207
Some(value) => Err(value),
@@ -211,7 +211,7 @@ impl<T> SyncOnceCell<T> {
211211
/// Gets the contents of the cell, initializing it with `f` if the cell
212212
/// was empty.
213213
///
214-
/// Many threads may call `get_or_init` concurrently with different
214+
/// Many threads may call `get_or_insert_with` concurrently with different
215215
/// initializing functions, but it is guaranteed that only one function
216216
/// will be executed.
217217
///
@@ -232,17 +232,17 @@ impl<T> SyncOnceCell<T> {
232232
/// use std::lazy::SyncOnceCell;
233233
///
234234
/// let cell = SyncOnceCell::new();
235-
/// let value = cell.get_or_init(|| 92);
235+
/// let value = cell.get_or_insert_with(|| 92);
236236
/// assert_eq!(value, &92);
237-
/// let value = cell.get_or_init(|| unreachable!());
237+
/// let value = cell.get_or_insert_with(|| unreachable!());
238238
/// assert_eq!(value, &92);
239239
/// ```
240240
#[unstable(feature = "once_cell", issue = "74465")]
241-
pub fn get_or_init<F>(&self, f: F) -> &T
241+
pub fn get_or_insert_with<F>(&self, f: F) -> &T
242242
where
243243
F: FnOnce() -> T,
244244
{
245-
match self.get_or_try_init(|| Ok::<T, !>(f())) {
245+
match self.try_get_or_insert_with(|| Ok::<T, !>(f())) {
246246
Ok(val) => val,
247247
}
248248
}
@@ -268,16 +268,16 @@ impl<T> SyncOnceCell<T> {
268268
/// use std::lazy::SyncOnceCell;
269269
///
270270
/// let cell = SyncOnceCell::new();
271-
/// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
271+
/// assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(()));
272272
/// assert!(cell.get().is_none());
273-
/// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
273+
/// let value = cell.try_get_or_insert_with(|| -> Result<i32, ()> {
274274
/// Ok(92)
275275
/// });
276276
/// assert_eq!(value, Ok(&92));
277277
/// assert_eq!(cell.get(), Some(&92))
278278
/// ```
279279
#[unstable(feature = "once_cell", issue = "74465")]
280-
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
280+
pub fn try_get_or_insert_with<F, E>(&self, f: F) -> Result<&T, E>
281281
where
282282
F: FnOnce() -> Result<T, E>,
283283
{
@@ -499,7 +499,7 @@ impl<T, F: FnOnce() -> T> SyncLazy<T, F> {
499499
/// ```
500500
#[unstable(feature = "once_cell", issue = "74465")]
501501
pub fn force(this: &SyncLazy<T, F>) -> &T {
502-
this.cell.get_or_init(|| match this.init.take() {
502+
this.cell.get_or_insert_with(|| match this.init.take() {
503503
Some(f) => f(),
504504
None => panic!("Lazy instance has previously been poisoned"),
505505
})

library/std/src/lazy/tests.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ fn sync_once_cell() {
5454
assert!(ONCE_CELL.get().is_none());
5555

5656
spawn_and_wait(|| {
57-
ONCE_CELL.get_or_init(|| 92);
57+
ONCE_CELL.get_or_insert_with(|| 92);
5858
assert_eq!(ONCE_CELL.get(), Some(&92));
5959
});
6060

61-
ONCE_CELL.get_or_init(|| panic!("Kabom!"));
61+
ONCE_CELL.get_or_insert_with(|| panic!("Kabom!"));
6262
assert_eq!(ONCE_CELL.get(), Some(&92));
6363
}
6464

@@ -92,7 +92,7 @@ fn sync_once_cell_drop() {
9292

9393
let x = SyncOnceCell::new();
9494
spawn_and_wait(move || {
95-
x.get_or_init(|| Dropper);
95+
x.get_or_insert_with(|| Dropper);
9696
assert_eq!(DROP_CNT.load(SeqCst), 0);
9797
drop(x);
9898
});
@@ -118,18 +118,21 @@ fn clone() {
118118
}
119119

120120
#[test]
121-
fn get_or_try_init() {
121+
fn try_get_or_insert_with() {
122122
let cell: SyncOnceCell<String> = SyncOnceCell::new();
123123
assert!(cell.get().is_none());
124124

125-
let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() }));
125+
let res = panic::catch_unwind(|| cell.try_get_or_insert_with(|| -> Result<_, ()> { panic!() }));
126126
assert!(res.is_err());
127127
assert!(!cell.is_initialized());
128128
assert!(cell.get().is_none());
129129

130-
assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
130+
assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(()));
131131

132-
assert_eq!(cell.get_or_try_init(|| Ok::<_, ()>("hello".to_string())), Ok(&"hello".to_string()));
132+
assert_eq!(
133+
cell.try_get_or_insert_with(|| Ok::<_, ()>("hello".to_string())),
134+
Ok(&"hello".to_string())
135+
);
133136
assert_eq!(cell.get(), Some(&"hello".to_string()));
134137
}
135138

@@ -224,7 +227,7 @@ fn static_sync_lazy() {
224227
fn static_sync_lazy_via_fn() {
225228
fn xs() -> &'static Vec<i32> {
226229
static XS: SyncOnceCell<Vec<i32>> = SyncOnceCell::new();
227-
XS.get_or_init(|| {
230+
XS.get_or_insert_with(|| {
228231
let mut xs = Vec::new();
229232
xs.push(1);
230233
xs.push(2);
@@ -261,7 +264,7 @@ fn eval_once_macro() {
261264
fn init() -> $ty {
262265
$($body)*
263266
}
264-
ONCE_CELL.get_or_init(init)
267+
ONCE_CELL.get_or_insert_with(init)
265268
}};
266269
}
267270

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ impl PrimitiveType {
13571357
pub fn all_impls(tcx: TyCtxt<'_>) -> &'static FxHashMap<PrimitiveType, SmallVec<[DefId; 4]>> {
13581358
static CELL: OnceCell<FxHashMap<PrimitiveType, SmallVec<[DefId; 4]>>> = OnceCell::new();
13591359

1360-
CELL.get_or_init(move || {
1360+
CELL.get_or_insert_with(move || {
13611361
use self::PrimitiveType::*;
13621362

13631363
/// A macro to create a FxHashMap.

0 commit comments

Comments
 (0)