Skip to content

Commit

Permalink
Add LazyCell::borrow_mut_with and LazyCell::try_borrow_mut_with.
Browse files Browse the repository at this point in the history
- Closes indiv0#79.
- Closes indiv0#80.
  • Loading branch information
dherman committed Feb 2, 2018
1 parent 8e19f50 commit a411185
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ impl<T> LazyCell<T> {
self.borrow().unwrap()
}

/// Borrows the contents of this lazy cell mutably for the duration of the cell
/// itself.
///
/// If the cell has not yet been filled, the cell is first filled using the
/// function provided.
///
/// # Panics
///
/// Panics if the cell becomes filled as a side effect of `f`.
pub fn borrow_mut_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
if self.filled() {
self.borrow_mut().unwrap()
} else {
let value = f();
if self.fill(value).is_err() {
panic!("borrow_mut_with: cell was filled by closure")
}
self.borrow_mut().unwrap()
}
}

/// Same as `borrow_with`, but allows the initializing function to fail.
///
/// # Panics
Expand All @@ -133,6 +154,24 @@ impl<T> LazyCell<T> {
Ok(self.borrow().unwrap())
}

/// Same as `borrow_mut_with`, but allows the initializing function to fail.
///
/// # Panics
///
/// Panics if the cell becomes filled as a side effect of `f`.
pub fn try_borrow_mut_with<E, F>(&mut self, f: F) -> Result<&mut T, E>
where F: FnOnce() -> Result<T, E>
{
if self.filled() {
return Ok(self.borrow_mut().unwrap());
}
let value = f()?;
if self.fill(value).is_err() {
panic!("try_borrow_mut_with: cell was filled by closure")
}
Ok(self.borrow_mut().unwrap())
}

/// Consumes this `LazyCell`, returning the underlying value.
pub fn into_inner(self) -> Option<T> {
unsafe { self.inner.into_inner() }
Expand Down

0 comments on commit a411185

Please sign in to comment.