Struct borrow_bag::BorrowBag [] [src]

pub struct BorrowBag<V> { /* fields omitted */ }

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.

use borrow_bag::new_borrow_bag;

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

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

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

let bag = new_borrow_bag();
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);

Methods

impl<V> BorrowBag<V>
[src]

Adds a value to the bag.

Trait bounds are used to constrain and define the BorrowBag implementation, but are not intended to provide any restrictions on the value being added.

let bag = new_borrow_bag();
// 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);

Borrows a value previously added to the bag.

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