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 {