pub struct LendingCell<T> { /* private fields */ }
Expand description
A container that allows borrowing without lifetimes.
let mut lender = LendingCell::new("borrowed");
let borrowed = lender.to_borrowed();
assert!(lender.try_get().is_none()); // `lender` is empty because it was borrowed
assert_eq!(*borrowed, "borrowed"); // it's certain that `borrowed` is valid while in scope
drop(borrowed); // dropping `borrowed` returns its value to `lender`
assert!(lender.try_get().is_some());
Implementations§
Source§impl<T> LendingCell<T>
impl<T> LendingCell<T>
Sourcepub fn try_get(&self) -> Option<&T>
pub fn try_get(&self) -> Option<&T>
Get a reference to the contained value if it wasn’t borrowed with
LendingCell::to_borrowed
Sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Get a reference to the contained value if it wasn’t borrowed with
LendingCell::to_borrowed
, otherwise panic
Sourcepub fn try_get_mut(&mut self) -> Option<&mut T>
pub fn try_get_mut(&mut self) -> Option<&mut T>
Get a mutable reference the contained value if it wasn’t borrowed with
LendingCell::to_borrowed
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Get a mutable reference the contained value if it wasn’t borrowed with
LendingCell::to_borrowed
, otherwise panic
Sourcepub fn to_borrowed(&mut self) -> BorrowedCell<T>
pub fn to_borrowed(&mut self) -> BorrowedCell<T>
Take the contained value and returned it in an owned object if it isn’t already borrowed, otherwise panic.
Sourcepub fn try_to_borrowed(&mut self) -> Option<BorrowedCell<T>>
pub fn try_to_borrowed(&mut self) -> Option<BorrowedCell<T>>
Take the contained value and returned it in an owned object if it isn’t already borrowed.
Sourcepub fn try_into_inner(self) -> Result<T, Self>
pub fn try_into_inner(self) -> Result<T, Self>
Destroy the container and return the contained object if it isn’t
being borrowed already. If it fails, return myself LendingCell