Struct borrow_bag::BorrowBag

source ·
pub struct BorrowBag<V> { /* private fields */ }
Expand description

BorrowBag allows the storage of any value using add(T), and returns a Handle which can be used to borrow the value back later. As the BorrowBag is add-only, Handle values remain valid for the lifetime of the BorrowBag.

After being added, the Handle can be passed to borrow(Handle), which will return a reference to the value.

Example

use borrow_bag::BorrowBag;

#[derive(PartialEq, Debug)]
struct X;

#[derive(PartialEq, Debug)]
struct Y;

#[derive(PartialEq, Debug)]
struct Z;

let bag = BorrowBag::new();
let (bag, x_handle) = bag.add(X);
let (bag, y_handle) = bag.add(Y);
let (bag, z_handle) = bag.add(Z);

let x: &X = bag.borrow(x_handle);
assert_eq!(x, &X);
let y: &Y = bag.borrow(y_handle);
assert_eq!(y, &Y);
let z: &Z = bag.borrow(z_handle);
assert_eq!(z, &Z);

// Can borrow multiple times using the same handle
let x: &X = bag.borrow(x_handle);
assert_eq!(x, &X);

Implementations§

source§

impl BorrowBag<()>

source

pub fn new() -> Self

Creates a new, empty BorrowBag.

source§

impl<V> BorrowBag<V>

source

pub fn add<T>(self, t: T) -> (BorrowBag<V::Output>, Handle<T, V::Navigator>)where V: Append<T>,

Adds a value to the bag, and returns a tuple containing:

  1. The new bag containing the added element; and
  2. A Handle which can be used to retrieve the added element.

The trait bound is used to constrain and define the BorrowBag implementation, but is not intended to provide any restrictions on the value being added.

let bag = BorrowBag::new();
// Consumes the empty `bag`, and produces a new `bag` containing the value. The `handle`
// can be used to borrow the value back later.
let (bag, handle) = bag.add(15u8);
source

pub fn borrow<T, N>(&self, _handle: Handle<T, N>) -> &Twhere V: Lookup<T, N>,

Borrows a value previously added to the bag.

let i: &u8 = bag.borrow(handle);
assert_eq!(*i, 15);

Trait Implementations§

source§

impl<V: Default> Default for BorrowBag<V>

source§

fn default() -> BorrowBag<V>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V> RefUnwindSafe for BorrowBag<V>where V: RefUnwindSafe,

§

impl<V> Send for BorrowBag<V>where V: Send,

§

impl<V> Sync for BorrowBag<V>where V: Sync,

§

impl<V> Unpin for BorrowBag<V>where V: Unpin,

§

impl<V> UnwindSafe for BorrowBag<V>where V: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.