diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 548b6c03daa7e..98d91cf6697f1 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -766,7 +766,7 @@ pub fn create_global_ctxt<'tcx>( } let gcx = sess.time("setup_global_ctxt", || { - global_ctxt.get_or_init(|| { + global_ctxt.get_or_insert_with(|| { TyCtxt::create_global_ctxt( sess, lint_store, diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index d9ec6d51cdfa8..804b1fe74a179 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -278,7 +278,7 @@ pub fn rustc_path<'a>() -> Option<&'a Path> { const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR"); - RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_ref().map(|v| &**v) + RUSTC_PATH.get_or_insert_with(|| get_rustc_path_inner(BIN_PATH)).as_ref().map(|v| &**v) } fn get_rustc_path_inner(bin_path: &str) -> Option { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index c031e0e2e1904..2eeae5028ffde 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1713,7 +1713,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } }; - self.cdata.source_map_import_info.get_or_init(|| { + self.cdata.source_map_import_info.get_or_insert_with(|| { let external_source_map = self.root.source_map.decode(self); external_source_map diff --git a/compiler/rustc_middle/src/mir/predecessors.rs b/compiler/rustc_middle/src/mir/predecessors.rs index a8b748833556d..ffd552ae059e5 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -39,7 +39,7 @@ impl PredecessorCache { &self, basic_blocks: &IndexVec>, ) -> &Predecessors { - self.cache.get_or_init(|| { + self.cache.get_or_insert_with(|| { let mut preds = IndexVec::from_elem(SmallVec::new(), basic_blocks); for (bb, data) in basic_blocks.iter_enumerated() { if let Some(term) = &data.terminator { diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 173e9a31928b5..062b571fe106b 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -433,7 +433,7 @@ impl<'sess> OnDiskCache<'sess> { T: Decodable>, { let cnum_map = - self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..])); + self.cnum_map.get_or_insert_with(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..])); let mut decoder = CacheDecoder { tcx, diff --git a/compiler/rustc_mir/src/dataflow/framework/graphviz.rs b/compiler/rustc_mir/src/dataflow/framework/graphviz.rs index 4e54257a1cb2d..7604cc4df6123 100644 --- a/compiler/rustc_mir/src/dataflow/framework/graphviz.rs +++ b/compiler/rustc_mir/src/dataflow/framework/graphviz.rs @@ -567,7 +567,7 @@ where macro_rules! regex { ($re:literal $(,)?) => {{ static RE: SyncOnceCell = SyncOnceCell::new(); - RE.get_or_init(|| Regex::new($re).unwrap()) + RE.get_or_insert_with(|| Regex::new($re).unwrap()) }}; } diff --git a/compiler/rustc_mir/src/transform/coverage/debug.rs b/compiler/rustc_mir/src/transform/coverage/debug.rs index ffa795134e257..877f7f65a4a9d 100644 --- a/compiler/rustc_mir/src/transform/coverage/debug.rs +++ b/compiler/rustc_mir/src/transform/coverage/debug.rs @@ -130,7 +130,7 @@ const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS"; pub(crate) fn debug_options<'a>() -> &'a DebugOptions { static DEBUG_OPTIONS: SyncOnceCell = SyncOnceCell::new(); - &DEBUG_OPTIONS.get_or_init(|| DebugOptions::from_env()) + &DEBUG_OPTIONS.get_or_insert_with(|| DebugOptions::from_env()) } /// Parses and maintains coverage-specific debug options captured from the environment variable diff --git a/compiler/rustc_mir_build/src/thir/pattern/_match.rs b/compiler/rustc_mir_build/src/thir/pattern/_match.rs index 5e7e81eba6273..278793f2c8a5c 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/_match.rs @@ -392,7 +392,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> { } fn head_ctor<'a>(&'a self, cx: &MatchCheckCtxt<'p, 'tcx>) -> &'a Constructor<'tcx> { - self.head_ctor.get_or_init(|| pat_constructor(cx, self.head())) + self.head_ctor.get_or_insert_with(|| pat_constructor(cx, self.head())) } fn iter(&self) -> impl Iterator> { diff --git a/library/core/src/lazy.rs b/library/core/src/lazy.rs index 2c517371c2c9b..65ec207ed0708 100644 --- a/library/core/src/lazy.rs +++ b/library/core/src/lazy.rs @@ -20,7 +20,7 @@ use crate::ops::Deref; /// let cell = OnceCell::new(); /// assert!(cell.get().is_none()); /// -/// let value: &String = cell.get_or_init(|| { +/// let value: &String = cell.get_or_insert_with(|| { /// "Hello, World!".to_string() /// }); /// assert_eq!(value, "Hello, World!"); @@ -163,21 +163,31 @@ impl OnceCell { /// use std::lazy::OnceCell; /// /// let cell = OnceCell::new(); - /// let value = cell.get_or_init(|| 92); + /// let value = cell.get_or_insert_with(|| 92); /// assert_eq!(value, &92); - /// let value = cell.get_or_init(|| unreachable!()); + /// let value = cell.get_or_insert_with(|| unreachable!()); /// assert_eq!(value, &92); /// ``` #[unstable(feature = "once_cell", issue = "74465")] - pub fn get_or_init(&self, f: F) -> &T + pub fn get_or_insert_with(&self, f: F) -> &T where F: FnOnce() -> T, { - match self.get_or_try_init(|| Ok::(f())) { + match self.try_get_or_insert_with(|| Ok::(f())) { Ok(val) => val, } } + /// Deprecated, use `get_or_insert_with` instead. + #[rustc_deprecated = "use get_or_insert_with instead"] + #[unstable(feature = "once_cell", issue = "74465")] + pub fn get_or_init(&self, f: F) -> &T + where + F: FnOnce() -> T, + { + self.get_or_insert_with(f) + } + /// Gets the contents of the cell, initializing it with `f` if /// the cell was empty. If the cell was empty and `f` failed, an /// error is returned. @@ -198,16 +208,16 @@ impl OnceCell { /// use std::lazy::OnceCell; /// /// let cell = OnceCell::new(); - /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(())); + /// assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(())); /// assert!(cell.get().is_none()); - /// let value = cell.get_or_try_init(|| -> Result { + /// let value = cell.try_get_or_insert_with(|| -> Result { /// Ok(92) /// }); /// assert_eq!(value, Ok(&92)); /// assert_eq!(cell.get(), Some(&92)) /// ``` #[unstable(feature = "once_cell", issue = "74465")] - pub fn get_or_try_init(&self, f: F) -> Result<&T, E> + pub fn try_get_or_insert_with(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result, { @@ -223,6 +233,16 @@ impl OnceCell { Ok(self.get().unwrap()) } + /// Deprecated, use `try_get_or_insert_with` instead. + #[rustc_deprecated = "use try_get_or_insert_with instead"] + #[unstable(feature = "once_cell", issue = "74465")] + pub fn get_or_try_init(&self, f: F) -> Result<&T, E> + where + F: FnOnce() -> Result, + { + self.try_get_or_insert_with(f) + } + /// Consumes the cell, returning the wrapped value. /// /// Returns `None` if the cell was empty. @@ -355,7 +375,7 @@ impl T> Lazy { /// ``` #[unstable(feature = "once_cell", issue = "74465")] pub fn force(this: &Lazy) -> &T { - this.cell.get_or_init(|| match this.init.take() { + this.cell.get_or_insert_with(|| match this.init.take() { Some(f) => f(), None => panic!("`Lazy` instance has previously been poisoned"), }) diff --git a/library/core/tests/lazy.rs b/library/core/tests/lazy.rs index 24f921ca7e4dc..6b6f8a9530021 100644 --- a/library/core/tests/lazy.rs +++ b/library/core/tests/lazy.rs @@ -8,10 +8,10 @@ use core::{ fn once_cell() { let c = OnceCell::new(); assert!(c.get().is_none()); - c.get_or_init(|| 92); + c.get_or_insert_with(|| 92); assert_eq!(c.get(), Some(&92)); - c.get_or_init(|| panic!("Kabom!")); + c.get_or_insert_with(|| panic!("Kabom!")); assert_eq!(c.get(), Some(&92)); } @@ -35,7 +35,7 @@ fn once_cell_drop() { } let x = OnceCell::new(); - x.get_or_init(|| Dropper); + x.get_or_insert_with(|| Dropper); assert_eq!(DROP_CNT.load(SeqCst), 0); drop(x); assert_eq!(DROP_CNT.load(SeqCst), 1); @@ -115,8 +115,8 @@ fn aliasing_in_get() { fn reentrant_init() { let x: OnceCell> = OnceCell::new(); let dangling_ref: Cell> = Cell::new(None); - x.get_or_init(|| { - let r = x.get_or_init(|| Box::new(92)); + x.get_or_insert_with(|| { + let r = x.get_or_insert_with(|| Box::new(92)); dangling_ref.set(Some(r)); Box::new(62) }); diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 2eb5fb4528620..d72581d464431 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -310,7 +310,7 @@ pub struct StdinLock<'a> { pub fn stdin() -> Stdin { static INSTANCE: SyncOnceCell>> = SyncOnceCell::new(); Stdin { - inner: INSTANCE.get_or_init(|| { + inner: INSTANCE.get_or_insert_with(|| { Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw())) }), } @@ -549,7 +549,7 @@ pub fn stdout() -> Stdout { static INSTANCE: SyncOnceCell>>> = SyncOnceCell::new(); Stdout { - inner: INSTANCE.get_or_init(|| unsafe { + inner: INSTANCE.get_or_insert_with(|| unsafe { let _ = sys_common::at_exit(|| { if let Some(instance) = INSTANCE.get() { // Flush the data and disable buffering during shutdown @@ -764,7 +764,7 @@ pub fn stderr() -> Stderr { static INSTANCE: SyncOnceCell>> = SyncOnceCell::new(); Stderr { - inner: INSTANCE.get_or_init(|| unsafe { + inner: INSTANCE.get_or_insert_with(|| unsafe { let r = ReentrantMutex::new(RefCell::new(stderr_raw())); r.init(); r diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index e0095e64faf31..bc04f048e6cd7 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -32,7 +32,7 @@ pub use core::lazy::*; /// assert!(CELL.get().is_none()); /// /// std::thread::spawn(|| { -/// let value: &String = CELL.get_or_init(|| { +/// let value: &String = CELL.get_or_insert_with(|| { /// "Hello, World!".to_string() /// }); /// assert_eq!(value, "Hello, World!"); @@ -201,7 +201,7 @@ impl SyncOnceCell { #[unstable(feature = "once_cell", issue = "74465")] pub fn set(&self, value: T) -> Result<(), T> { let mut value = Some(value); - self.get_or_init(|| value.take().unwrap()); + self.get_or_insert_with(|| value.take().unwrap()); match value { None => Ok(()), Some(value) => Err(value), @@ -211,7 +211,7 @@ impl SyncOnceCell { /// Gets the contents of the cell, initializing it with `f` if the cell /// was empty. /// - /// Many threads may call `get_or_init` concurrently with different + /// Many threads may call `get_or_insert_with` concurrently with different /// initializing functions, but it is guaranteed that only one function /// will be executed. /// @@ -232,21 +232,31 @@ impl SyncOnceCell { /// use std::lazy::SyncOnceCell; /// /// let cell = SyncOnceCell::new(); - /// let value = cell.get_or_init(|| 92); + /// let value = cell.get_or_insert_with(|| 92); /// assert_eq!(value, &92); - /// let value = cell.get_or_init(|| unreachable!()); + /// let value = cell.get_or_insert_with(|| unreachable!()); /// assert_eq!(value, &92); /// ``` #[unstable(feature = "once_cell", issue = "74465")] - pub fn get_or_init(&self, f: F) -> &T + pub fn get_or_insert_with(&self, f: F) -> &T where F: FnOnce() -> T, { - match self.get_or_try_init(|| Ok::(f())) { + match self.try_get_or_insert_with(|| Ok::(f())) { Ok(val) => val, } } + /// Deprecated, use `get_or_insert_with` instead. + #[rustc_deprecated = "use get_or_insert_with instead"] + #[unstable(feature = "once_cell", issue = "74465")] + pub fn get_or_init(&self, f: F) -> &T + where + F: FnOnce() -> T, + { + self.get_or_insert_with(f) + } + /// Gets the contents of the cell, initializing it with `f` if /// the cell was empty. If the cell was empty and `f` failed, an /// error is returned. @@ -268,16 +278,16 @@ impl SyncOnceCell { /// use std::lazy::SyncOnceCell; /// /// let cell = SyncOnceCell::new(); - /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(())); + /// assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(())); /// assert!(cell.get().is_none()); - /// let value = cell.get_or_try_init(|| -> Result { + /// let value = cell.try_get_or_insert_with(|| -> Result { /// Ok(92) /// }); /// assert_eq!(value, Ok(&92)); /// assert_eq!(cell.get(), Some(&92)) /// ``` #[unstable(feature = "once_cell", issue = "74465")] - pub fn get_or_try_init(&self, f: F) -> Result<&T, E> + pub fn try_get_or_insert_with(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result, { @@ -297,6 +307,16 @@ impl SyncOnceCell { Ok(unsafe { self.get_unchecked() }) } + /// Deprecated, use `try_get_or_insert_with` instead. + #[rustc_deprecated = "use try_get_or_insert_with instead"] + #[unstable(feature = "once_cell", issue = "74465")] + pub fn get_or_try_init(&self, f: F) -> Result<&T, E> + where + F: FnOnce() -> Result, + { + self.try_get_or_insert_with(f) + } + /// Consumes the `SyncOnceCell`, returning the wrapped value. Returns /// `None` if the cell was empty. /// @@ -499,7 +519,7 @@ impl T> SyncLazy { /// ``` #[unstable(feature = "once_cell", issue = "74465")] pub fn force(this: &SyncLazy) -> &T { - this.cell.get_or_init(|| match this.init.take() { + this.cell.get_or_insert_with(|| match this.init.take() { Some(f) => f(), None => panic!("Lazy instance has previously been poisoned"), }) diff --git a/library/std/src/lazy/tests.rs b/library/std/src/lazy/tests.rs index a170edbd997dd..c3ec4f8617bdb 100644 --- a/library/std/src/lazy/tests.rs +++ b/library/std/src/lazy/tests.rs @@ -54,11 +54,11 @@ fn sync_once_cell() { assert!(ONCE_CELL.get().is_none()); spawn_and_wait(|| { - ONCE_CELL.get_or_init(|| 92); + ONCE_CELL.get_or_insert_with(|| 92); assert_eq!(ONCE_CELL.get(), Some(&92)); }); - ONCE_CELL.get_or_init(|| panic!("Kabom!")); + ONCE_CELL.get_or_insert_with(|| panic!("Kabom!")); assert_eq!(ONCE_CELL.get(), Some(&92)); } @@ -92,7 +92,7 @@ fn sync_once_cell_drop() { let x = SyncOnceCell::new(); spawn_and_wait(move || { - x.get_or_init(|| Dropper); + x.get_or_insert_with(|| Dropper); assert_eq!(DROP_CNT.load(SeqCst), 0); drop(x); }); @@ -118,18 +118,21 @@ fn clone() { } #[test] -fn get_or_try_init() { +fn try_get_or_insert_with() { let cell: SyncOnceCell = SyncOnceCell::new(); assert!(cell.get().is_none()); - let res = panic::catch_unwind(|| cell.get_or_try_init(|| -> Result<_, ()> { panic!() })); + let res = panic::catch_unwind(|| cell.try_get_or_insert_with(|| -> Result<_, ()> { panic!() })); assert!(res.is_err()); assert!(!cell.is_initialized()); assert!(cell.get().is_none()); - assert_eq!(cell.get_or_try_init(|| Err(())), Err(())); + assert_eq!(cell.try_get_or_insert_with(|| Err(())), Err(())); - assert_eq!(cell.get_or_try_init(|| Ok::<_, ()>("hello".to_string())), Ok(&"hello".to_string())); + assert_eq!( + cell.try_get_or_insert_with(|| Ok::<_, ()>("hello".to_string())), + Ok(&"hello".to_string()) + ); assert_eq!(cell.get(), Some(&"hello".to_string())); } @@ -224,7 +227,7 @@ fn static_sync_lazy() { fn static_sync_lazy_via_fn() { fn xs() -> &'static Vec { static XS: SyncOnceCell> = SyncOnceCell::new(); - XS.get_or_init(|| { + XS.get_or_insert_with(|| { let mut xs = Vec::new(); xs.push(1); xs.push(2); @@ -261,7 +264,7 @@ fn eval_once_macro() { fn init() -> $ty { $($body)* } - ONCE_CELL.get_or_init(init) + ONCE_CELL.get_or_insert_with(init) }}; } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 32b3f69ecd4f0..c6d0ddd7bd0aa 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1357,7 +1357,7 @@ impl PrimitiveType { pub fn all_impls(tcx: TyCtxt<'_>) -> &'static FxHashMap> { static CELL: OnceCell>> = OnceCell::new(); - CELL.get_or_init(move || { + CELL.get_or_insert_with(move || { use self::PrimitiveType::*; /// A macro to create a FxHashMap.