Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add borrow_with to LazyCell #56

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ alphabetical order.

* [Carl Lerche](https://github.com/carllerche)
* [Nikita Pekin](https://github.com/indiv0)
* [J/A](https://github.com/archer884)

[lazycell]: https://github.com/indiv0/lazycell
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ impl<T> LazyCell<T> {
unsafe { &*self.inner.get() }.as_ref()
}

/// Borrows the contents of this lazy cell for the duration of the cell
/// itself.
///
/// If the cell has not yet been filled, the cell is first filled using the
/// function provided.
pub fn borrow_with<F: FnOnce() -> T>(&self, f: F) -> &T {
let mut slot = unsafe { &mut *self.inner.get() };
if !slot.is_some() {
*slot = Some(f());
}

slot.as_ref().unwrap()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rewrite the function to be:

...
if !slot.is_some() {
    *slot = Some(f());
}

slot.as_ref().unwrap()

}

/// Consumes this `LazyCell`, returning the underlying value.
pub fn into_inner(self) -> Option<T> {
unsafe { self.inner.into_inner() }
Expand Down Expand Up @@ -186,6 +200,33 @@ mod tests {
assert_eq!(lazycell.fill(1), Err(1));
}

#[test]
fn test_borrow_with() {
let lazycell = LazyCell::new();

let value = lazycell.borrow_with(|| 1);
assert_eq!(&1, value);
Copy link
Owner

@indiv0 indiv0 Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test the positive case (i.e. borrow_with(|| 1) on a cell that's already been filled (just for complete test coverage)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll make a note to write that up. I was also thinking a test to assert that the || filler_func only gets called once would be appropriate.

}

#[test]
fn test_borrow_with_already_filled() {
let lazycell = LazyCell::new();
lazycell.fill(1).unwrap();

let value = lazycell.borrow_with(|| 1);
assert_eq!(&1, value);
}

#[test]
fn test_borrow_with_not_called_when_filled() {
let lazycell = LazyCell::new();

lazycell.fill(1).unwrap();

let value = lazycell.borrow_with(|| 2);
assert_eq!(&1, value);
}

#[test]
fn test_into_inner() {
let lazycell = LazyCell::new();
Expand Down