diff --git a/CHANGELOG.md b/CHANGELOG.md index e5c0ae7..d5019ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ - +## 1.13.0 + +- Add `Lazy::get`, similar to `OnceCell::get`. + +## 1.12.1 + +- Remove incorrect `debug_assert`. + ## 1.12.0 - Add `OnceCell::wait`, a blocking variant of `get`. diff --git a/Cargo.toml b/Cargo.toml index 5c77dfc..da8a3d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "once_cell" -version = "1.12.0" +version = "1.13.0" authors = ["Aleksey Kladov "] license = "MIT OR Apache-2.0" edition = "2018" diff --git a/src/imp_pl.rs b/src/imp_pl.rs index 2bd80fa..d80ca5e 100644 --- a/src/imp_pl.rs +++ b/src/imp_pl.rs @@ -168,6 +168,7 @@ fn initialize_inner(state: &AtomicU8, init: &mut dyn FnMut() -> bool) { None, ); }, + Err(INCOMPLETE) => (), Err(_) => debug_assert!(false), } } diff --git a/src/lib.rs b/src/lib.rs index e02ecb1..70f08de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -741,6 +741,23 @@ pub mod unsync { None => panic!("Lazy instance has previously been poisoned"), }) } + + /// Gets the reference to the result of this lazy value if + /// it was initialized, otherwise returns `None`. + /// + /// # Example + /// ``` + /// use once_cell::unsync::Lazy; + /// + /// let lazy = Lazy::new(|| 92); + /// + /// assert_eq!(Lazy::get(&lazy), None); + /// assert_eq!(&*lazy, &92); + /// assert_eq!(Lazy::get(&lazy), Some(&92)); + /// ``` + pub fn get(this: &Lazy) -> Option<&T> { + this.cell.get() + } } impl T> Deref for Lazy { @@ -1214,6 +1231,23 @@ pub mod sync { None => panic!("Lazy instance has previously been poisoned"), }) } + + /// Gets the reference to the result of this lazy value if + /// it was initialized, otherwise returns `None`. + /// + /// # Example + /// ``` + /// use once_cell::sync::Lazy; + /// + /// let lazy = Lazy::new(|| 92); + /// + /// assert_eq!(Lazy::get(&lazy), None); + /// assert_eq!(&*lazy, &92); + /// assert_eq!(Lazy::get(&lazy), Some(&92)); + /// ``` + pub fn get(this: &Lazy) -> Option<&T> { + this.cell.get() + } } impl T> Deref for Lazy {