Skip to content

Commit f3d072e

Browse files
committed
Update once_cell closure-taking function names
- 'get_or_init' -> 'get_or_init_with' - 'get_or_try_init' -> 'get_or_try_init_with'
1 parent acd27bb commit f3d072e

File tree

23 files changed

+61
-58
lines changed

23 files changed

+61
-58
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22822282
}
22832283

22842284
fn dominators(&self) -> &Dominators<BasicBlock> {
2285-
self.dominators.get_or_init(|| self.body.basic_blocks.dominators())
2285+
self.dominators.get_or_init_with(|| self.body.basic_blocks.dominators())
22862286
}
22872287
}
22882288

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll D
492492

493493
// FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context.
494494
fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
495-
*debug_context(cx).recursion_marker_type.get_or_init(move || {
495+
*debug_context(cx).recursion_marker_type.get_or_init_with(move || {
496496
unsafe {
497497
// The choice of type here is pretty arbitrary -
498498
// anything reading the debuginfo for a recursive

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2318,7 +2318,7 @@ fn add_native_libs_from_crate(
23182318
cmd.link_whole_staticlib(
23192319
name,
23202320
verbatim,
2321-
&search_paths.get_or_init(|| archive_search_paths(sess)),
2321+
&search_paths.get_or_init_with(|| archive_search_paths(sess)),
23222322
);
23232323
} else {
23242324
cmd.link_staticlib(name, verbatim)

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,12 @@ pub fn create_global_ctxt<'tcx>(
693693
callback(sess, &mut local_providers, &mut extern_providers);
694694
}
695695

696-
let queries = queries.get_or_init(|| {
696+
let queries = queries.get_or_init_with(|| {
697697
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
698698
});
699699

700700
sess.time("setup_global_ctxt", || {
701-
gcx_cell.get_or_init(move || {
701+
gcx_cell.get_or_init_with(move || {
702702
TyCtxt::create_global_ctxt(
703703
sess,
704704
lint_store,

compiler/rustc_interface/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn get_codegen_backend(
239239
) -> Box<dyn CodegenBackend> {
240240
static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
241241

242-
let load = LOAD.get_or_init(|| {
242+
let load = LOAD.get_or_init_with(|| {
243243
let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm");
244244

245245
match backend_name.unwrap_or(default_codegen_backend) {
@@ -264,7 +264,7 @@ pub fn rustc_path<'a>() -> Option<&'a Path> {
264264

265265
const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
266266

267-
RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
267+
RUSTC_PATH.get_or_init_with(|| get_rustc_path_inner(BIN_PATH)).as_deref()
268268
}
269269

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

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12971297
// Slow path: We need to find out the new `DefIndex` of the provided
12981298
// `DefPathHash`, if its still exists. This requires decoding every `DefPathHash`
12991299
// stored in this crate.
1300-
let map = self.cdata.expn_hash_map.get_or_init(|| {
1300+
let map = self.cdata.expn_hash_map.get_or_init_with(|| {
13011301
let end_id = self.root.expn_hashes.size() as u32;
13021302
let mut map =
13031303
UnhashMap::with_capacity_and_hasher(end_id as usize, Default::default());

compiler/rustc_middle/src/mir/basic_blocks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'tcx> BasicBlocks<'tcx> {
3838
/// Returns true if control-flow graph contains a cycle reachable from the `START_BLOCK`.
3939
#[inline]
4040
pub fn is_cfg_cyclic(&self) -> bool {
41-
*self.cache.is_cyclic.get_or_init(|| graph::is_cyclic(self))
41+
*self.cache.is_cyclic.get_or_init_with(|| graph::is_cyclic(self))
4242
}
4343

4444
pub fn dominators(&self) -> Dominators<BasicBlock> {
@@ -48,7 +48,7 @@ impl<'tcx> BasicBlocks<'tcx> {
4848
/// Returns predecessors for each basic block.
4949
#[inline]
5050
pub fn predecessors(&self) -> &Predecessors {
51-
self.cache.predecessors.get_or_init(|| {
51+
self.cache.predecessors.get_or_init_with(|| {
5252
let mut preds = IndexVec::from_elem(SmallVec::new(), &self.basic_blocks);
5353
for (bb, data) in self.basic_blocks.iter_enumerated() {
5454
if let Some(term) = &data.terminator {
@@ -64,7 +64,7 @@ impl<'tcx> BasicBlocks<'tcx> {
6464
/// Returns basic blocks in a postorder.
6565
#[inline]
6666
pub fn postorder(&self) -> &[BasicBlock] {
67-
self.cache.postorder.get_or_init(|| {
67+
self.cache.postorder.get_or_init_with(|| {
6868
Postorder::new(&self.basic_blocks, START_BLOCK).map(|(bb, _)| bb).collect()
6969
})
7070
}
@@ -73,7 +73,7 @@ impl<'tcx> BasicBlocks<'tcx> {
7373
/// values that lead to a `target` block from a `switch` block.
7474
#[inline]
7575
pub fn switch_sources(&self) -> &SwitchSources {
76-
self.cache.switch_sources.get_or_init(|| {
76+
self.cache.switch_sources.get_or_init_with(|| {
7777
let mut switch_sources: SwitchSources = FxHashMap::default();
7878
for (bb, data) in self.basic_blocks.iter_enumerated() {
7979
if let Some(Terminator {

compiler/rustc_mir_dataflow/src/framework/graphviz.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ where
594594
macro_rules! regex {
595595
($re:literal $(,)?) => {{
596596
static RE: OnceLock<regex::Regex> = OnceLock::new();
597-
RE.get_or_init(|| Regex::new($re).unwrap())
597+
RE.get_or_init_with(|| Regex::new($re).unwrap())
598598
}};
599599
}
600600

compiler/rustc_mir_transform/src/coverage/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
133133
pub(super) fn debug_options<'a>() -> &'a DebugOptions {
134134
static DEBUG_OPTIONS: OnceLock<DebugOptions> = OnceLock::new();
135135

136-
&DEBUG_OPTIONS.get_or_init(DebugOptions::from_env)
136+
&DEBUG_OPTIONS.get_or_init_with(DebugOptions::from_env)
137137
}
138138

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

library/core/src/cell/lazy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
7777
#[inline]
7878
#[unstable(feature = "once_cell", issue = "74465")]
7979
pub fn force(this: &LazyCell<T, F>) -> &T {
80-
this.cell.get_or_init(|| match this.init.take() {
80+
this.cell.get_or_init_with(|| match this.init.take() {
8181
Some(f) => f(),
8282
None => panic!("`Lazy` instance has previously been poisoned"),
8383
})

library/core/src/cell/once.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::mem;
2323
/// let cell = OnceCell::new();
2424
/// assert!(cell.get().is_none());
2525
///
26-
/// let value: &String = cell.get_or_init(|| {
26+
/// let value: &String = cell.get_or_init_with(|| {
2727
/// "Hello, World!".to_string()
2828
/// });
2929
/// assert_eq!(value, "Hello, World!");
@@ -122,18 +122,18 @@ impl<T> OnceCell<T> {
122122
/// use std::cell::OnceCell;
123123
///
124124
/// let cell = OnceCell::new();
125-
/// let value = cell.get_or_init(|| 92);
125+
/// let value = cell.get_or_init_with(|| 92);
126126
/// assert_eq!(value, &92);
127-
/// let value = cell.get_or_init(|| unreachable!());
127+
/// let value = cell.get_or_init_with(|| unreachable!());
128128
/// assert_eq!(value, &92);
129129
/// ```
130130
#[inline]
131131
#[unstable(feature = "once_cell", issue = "74465")]
132-
pub fn get_or_init<F>(&self, f: F) -> &T
132+
pub fn get_or_init_with<F>(&self, f: F) -> &T
133133
where
134134
F: FnOnce() -> T,
135135
{
136-
match self.get_or_try_init(|| Ok::<T, !>(f())) {
136+
match self.get_or_try_init_with(|| Ok::<T, !>(f())) {
137137
Ok(val) => val,
138138
}
139139
}
@@ -158,16 +158,16 @@ impl<T> OnceCell<T> {
158158
/// use std::cell::OnceCell;
159159
///
160160
/// let cell = OnceCell::new();
161-
/// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
161+
/// assert_eq!(cell.get_or_try_init_with(|| Err(())), Err(()));
162162
/// assert!(cell.get().is_none());
163-
/// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
163+
/// let value = cell.get_or_try_init_with(|| -> Result<i32, ()> {
164164
/// Ok(92)
165165
/// });
166166
/// assert_eq!(value, Ok(&92));
167167
/// assert_eq!(cell.get(), Some(&92))
168168
/// ```
169169
#[unstable(feature = "once_cell", issue = "74465")]
170-
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
170+
pub fn get_or_try_init_with<F, E>(&self, f: F) -> Result<&T, E>
171171
where
172172
F: FnOnce() -> Result<T, E>,
173173
{

library/core/tests/lazy.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use core::{
77
fn once_cell() {
88
let c = OnceCell::new();
99
assert!(c.get().is_none());
10-
c.get_or_init(|| 92);
10+
c.get_or_init_with(|| 92);
1111
assert_eq!(c.get(), Some(&92));
1212

13-
c.get_or_init(|| panic!("Kabom!"));
13+
c.get_or_init_with(|| panic!("Kabom!"));
1414
assert_eq!(c.get(), Some(&92));
1515
}
1616

@@ -34,7 +34,7 @@ fn once_cell_drop() {
3434
}
3535

3636
let x = OnceCell::new();
37-
x.get_or_init(|| Dropper);
37+
x.get_or_init_with(|| Dropper);
3838
assert_eq!(DROP_CNT.load(SeqCst), 0);
3939
drop(x);
4040
assert_eq!(DROP_CNT.load(SeqCst), 1);
@@ -126,8 +126,8 @@ fn aliasing_in_get() {
126126
fn reentrant_init() {
127127
let x: OnceCell<Box<i32>> = OnceCell::new();
128128
let dangling_ref: Cell<Option<&i32>> = Cell::new(None);
129-
x.get_or_init(|| {
130-
let r = x.get_or_init(|| Box::new(92));
129+
x.get_or_init_with(|| {
130+
let r = x.get_or_init_with(|| Box::new(92));
131131
dangling_ref.set(Some(r));
132132
Box::new(62)
133133
});

library/std/src/io/stdio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub struct StdinLock<'a> {
322322
pub fn stdin() -> Stdin {
323323
static INSTANCE: OnceLock<Mutex<BufReader<StdinRaw>>> = OnceLock::new();
324324
Stdin {
325-
inner: INSTANCE.get_or_init(|| {
325+
inner: INSTANCE.get_or_init_with(|| {
326326
Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
327327
}),
328328
}
@@ -614,7 +614,7 @@ static STDOUT: OnceLock<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = OnceLo
614614
pub fn stdout() -> Stdout {
615615
Stdout {
616616
inner: STDOUT
617-
.get_or_init(|| ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())))),
617+
.get_or_init_with(|| ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())))),
618618
}
619619
}
620620

@@ -623,7 +623,7 @@ pub fn stdout() -> Stdout {
623623
// buffering capacity.
624624
pub fn cleanup() {
625625
let mut initialized = false;
626-
let stdout = STDOUT.get_or_init(|| {
626+
let stdout = STDOUT.get_or_init_with(|| {
627627
initialized = true;
628628
ReentrantMutex::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
629629
});

library/std/src/sync/lazy_lock/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn static_sync_lazy() {
116116
fn static_sync_lazy_via_fn() {
117117
fn xs() -> &'static Vec<i32> {
118118
static XS: OnceLock<Vec<i32>> = OnceLock::new();
119-
XS.get_or_init(|| {
119+
XS.get_or_init_with(|| {
120120
let mut xs = Vec::new();
121121
xs.push(1);
122122
xs.push(2);

library/std/src/sync/once_lock.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::sync::Once;
2222
/// assert!(CELL.get().is_none());
2323
///
2424
/// std::thread::spawn(|| {
25-
/// let value: &String = CELL.get_or_init(|| {
25+
/// let value: &String = CELL.get_or_init_with(|| {
2626
/// "Hello, World!".to_string()
2727
/// });
2828
/// assert_eq!(value, "Hello, World!");
@@ -132,7 +132,7 @@ impl<T> OnceLock<T> {
132132
#[unstable(feature = "once_cell", issue = "74465")]
133133
pub fn set(&self, value: T) -> Result<(), T> {
134134
let mut value = Some(value);
135-
self.get_or_init(|| value.take().unwrap());
135+
self.get_or_init_with(|| value.take().unwrap());
136136
match value {
137137
None => Ok(()),
138138
Some(value) => Err(value),
@@ -142,7 +142,7 @@ impl<T> OnceLock<T> {
142142
/// Gets the contents of the cell, initializing it with `f` if the cell
143143
/// was empty.
144144
///
145-
/// Many threads may call `get_or_init` concurrently with different
145+
/// Many threads may call `get_or_init_with` concurrently with different
146146
/// initializing functions, but it is guaranteed that only one function
147147
/// will be executed.
148148
///
@@ -163,18 +163,18 @@ impl<T> OnceLock<T> {
163163
/// use std::sync::OnceLock;
164164
///
165165
/// let cell = OnceLock::new();
166-
/// let value = cell.get_or_init(|| 92);
166+
/// let value = cell.get_or_init_with(|| 92);
167167
/// assert_eq!(value, &92);
168-
/// let value = cell.get_or_init(|| unreachable!());
168+
/// let value = cell.get_or_init_with(|| unreachable!());
169169
/// assert_eq!(value, &92);
170170
/// ```
171171
#[inline]
172172
#[unstable(feature = "once_cell", issue = "74465")]
173-
pub fn get_or_init<F>(&self, f: F) -> &T
173+
pub fn get_or_init_with<F>(&self, f: F) -> &T
174174
where
175175
F: FnOnce() -> T,
176176
{
177-
match self.get_or_try_init(|| Ok::<T, !>(f())) {
177+
match self.get_or_try_init_with(|| Ok::<T, !>(f())) {
178178
Ok(val) => val,
179179
}
180180
}
@@ -200,17 +200,17 @@ impl<T> OnceLock<T> {
200200
/// use std::sync::OnceLock;
201201
///
202202
/// let cell = OnceLock::new();
203-
/// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
203+
/// assert_eq!(cell.get_or_try_init_with(|| Err(())), Err(()));
204204
/// assert!(cell.get().is_none());
205-
/// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
205+
/// let value = cell.get_or_try_init_with(|| -> Result<i32, ()> {
206206
/// Ok(92)
207207
/// });
208208
/// assert_eq!(value, Ok(&92));
209209
/// assert_eq!(cell.get(), Some(&92))
210210
/// ```
211211
#[inline]
212212
#[unstable(feature = "once_cell", issue = "74465")]
213-
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
213+
pub fn get_or_try_init_with<F, E>(&self, f: F) -> Result<&T, E>
214214
where
215215
F: FnOnce() -> Result<T, E>,
216216
{

library/std/src/sync/once_lock/tests.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ fn sync_once_cell() {
2020
assert!(ONCE_CELL.get().is_none());
2121

2222
spawn_and_wait(|| {
23-
ONCE_CELL.get_or_init(|| 92);
23+
ONCE_CELL.get_or_init_with(|| 92);
2424
assert_eq!(ONCE_CELL.get(), Some(&92));
2525
});
2626

27-
ONCE_CELL.get_or_init(|| panic!("Kabom!"));
27+
ONCE_CELL.get_or_init_with(|| panic!("Kabom!"));
2828
assert_eq!(ONCE_CELL.get(), Some(&92));
2929
}
3030

@@ -59,7 +59,7 @@ fn sync_once_cell_drop() {
5959

6060
let x = OnceLock::new();
6161
spawn_and_wait(move || {
62-
x.get_or_init(|| Dropper);
62+
x.get_or_init_with(|| Dropper);
6363
assert_eq!(DROP_CNT.load(SeqCst), 0);
6464
drop(x);
6565
});
@@ -85,18 +85,21 @@ fn clone() {
8585
}
8686

8787
#[test]
88-
fn get_or_try_init() {
88+
fn get_or_try_init_with() {
8989
let cell: OnceLock<String> = OnceLock::new();
9090
assert!(cell.get().is_none());
9191

92-
let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() }));
92+
let res = panic::catch_unwind(|| cell.get_or_try_init_with(|| -> Result<_, ()> { panic!() }));
9393
assert!(res.is_err());
9494
assert!(!cell.is_initialized());
9595
assert!(cell.get().is_none());
9696

97-
assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
97+
assert_eq!(cell.get_or_try_init_with(|| Err(())), Err(()));
9898

99-
assert_eq!(cell.get_or_try_init(|| Ok::<_, ()>("hello".to_string())), Ok(&"hello".to_string()));
99+
assert_eq!(
100+
cell.get_or_try_init_with(|| Ok::<_, ()>("hello".to_string())),
101+
Ok(&"hello".to_string())
102+
);
100103
assert_eq!(cell.get(), Some(&"hello".to_string()));
101104
}
102105

@@ -140,7 +143,7 @@ fn eval_once_macro() {
140143
fn init() -> $ty {
141144
$($body)*
142145
}
143-
ONCE_CELL.get_or_init(init)
146+
ONCE_CELL.get_or_init_with(init)
144147
}};
145148
}
146149

library/std/src/sys/itron/mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Mutex {
3131

3232
/// Get the inner mutex's ID, which is lazily created.
3333
fn raw(&self) -> abi::ID {
34-
match self.mtx.get_or_try_init(|| new_mtx().map(|id| (id, ()))) {
34+
match self.mtx.get_or_try_init_with(|| new_mtx().map(|id| (id, ()))) {
3535
Ok((id, ())) => id,
3636
Err(e) => fail(e, &"acre_mtx"),
3737
}

library/std/src/sys/itron/spin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<T> SpinIdOnceCell<T> {
113113
/// Warning: `f` must not perform a blocking operation, which
114114
/// includes panicking.
115115
#[inline]
116-
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<(abi::ID, &T), E>
116+
pub fn get_or_try_init_with<F, E>(&self, f: F) -> Result<(abi::ID, &T), E>
117117
where
118118
F: FnOnce() -> Result<(abi::ID, T), E>,
119119
{

0 commit comments

Comments
 (0)