diff --git a/CHANGELOG.md b/CHANGELOG.md index f685f9b..d5019ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - +## 1.13.0 + +- Add `Lazy::get`, similar to `OnceCell::get`. + ## 1.12.1 - Remove incorrect `debug_assert`. diff --git a/Cargo.toml b/Cargo.toml index 7ff9f8a..da8a3d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "once_cell" -version = "1.12.1" +version = "1.13.0" authors = ["Aleksey Kladov "] license = "MIT OR Apache-2.0" edition = "2018" 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 {