Skip to content

Commit

Permalink
Merge matklad#183
Browse files Browse the repository at this point in the history
183: Add a `Lazy::get` function, similar to `OnceCell::get` r=matklad a=glandium



Co-authored-by: Mike Hommey <mh@glandium.org>
  • Loading branch information
bors[bot] and glandium committed Jul 4, 2022
2 parents 2be67cc + 2b0e3e5 commit eda22ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

-

## 1.13.0

- Add `Lazy::get`, similar to `OnceCell::get`.

## 1.12.1

- Remove incorrect `debug_assert`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "once_cell"
version = "1.12.1"
version = "1.13.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, F>) -> Option<&T> {
this.cell.get()
}
}

impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
Expand Down Expand Up @@ -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<T, F>) -> Option<&T> {
this.cell.get()
}
}

impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
Expand Down

0 comments on commit eda22ce

Please sign in to comment.