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 TakeCell::get_mut function #2077

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/tock-cells/src/take_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ impl<'a, T: ?Sized> TakeCell<'a, T> {
self.val.replace(Some(val))
}

/// Retrieves a mutable reference to the inner value that only lives as long
/// as the reference to this does.
///
/// This escapes the "take" aspect of TakeCell in a way which is guaranteed
/// safe due to the returned reference sharing the lifetime of `&mut self`.
pub fn get_mut(&mut self) -> Option<&mut T> {
self.val.get_mut().as_mut().map(|v| &mut **v)
}

/// Allows `closure` to borrow the contents of the `TakeCell` if-and-only-if
/// it is not `take`n already. The state of the `TakeCell` is unchanged
/// after the closure completes.
Expand Down