LendingCell

Struct LendingCell 

Source
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>

Source

pub fn new(thing: T) -> Self

Creates a new LendingCell with the given value

Source

pub fn try_get(&self) -> Option<&T>

Get a reference to the contained value if it wasn’t borrowed with LendingCell::to_borrowed

Source

pub fn get(&self) -> &T

Get a reference to the contained value if it wasn’t borrowed with LendingCell::to_borrowed, otherwise panic

Source

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

Source

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

Source

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.

Source

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.

Source

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

Trait Implementations§

Source§

impl<T: Send> Send for LendingCell<T>

Source§

impl<T: Sync> Sync for LendingCell<T>

Auto Trait Implementations§

§

impl<T> Freeze for LendingCell<T>

§

impl<T> !RefUnwindSafe for LendingCell<T>

§

impl<T> Unpin for LendingCell<T>

§

impl<T> !UnwindSafe for LendingCell<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.