borrow_bag/
lib.rs

1//! A type-safe, heterogeneous collection with zero-cost add and borrow.
2//!
3//! `BorrowBag` allows the storage of any value, and returns a `Handle` which can be used to borrow
4//! the value back later. As the `BorrowBag` is add-only, `Handle` values remain valid for the
5//! lifetime of the `BorrowBag`.
6
7#![doc(html_root_url = "https://docs.rs/borrow-bag/1.1.1")] // Update when changed in Cargo.toml
8#![warn(missing_docs, deprecated)]
9// Stricter requirements once we get to pull request stage, all warnings must be resolved.
10#![cfg_attr(feature = "ci", deny(warnings))]
11#![cfg_attr(feature = "cargo-clippy", allow(clippy::should_implement_trait))]
12#![doc(test(attr(deny(warnings))))]
13// TODO: Remove this when it's a hard error by default (error E0446).
14// See Rust issue #34537 <https://github.com/rust-lang/rust/issues/34537>
15#![deny(private_in_public)]
16
17mod append;
18mod handle;
19mod lookup;
20
21pub use append::Append;
22pub use handle::Handle;
23pub use lookup::Lookup;
24
25/// `BorrowBag` allows the storage of any value using `add(T)`, and returns a `Handle` which can be
26/// used to borrow the value back later. As the `BorrowBag` is add-only, `Handle` values remain
27/// valid for the lifetime of the `BorrowBag`.
28///
29/// After being added, the `Handle` can be passed to `borrow(Handle)`, which will return a
30/// reference to the value.
31///
32/// ## Example
33///
34/// ```rust
35/// use borrow_bag::BorrowBag;
36///
37/// #[derive(PartialEq, Debug)]
38/// struct X;
39///
40/// #[derive(PartialEq, Debug)]
41/// struct Y;
42///
43/// #[derive(PartialEq, Debug)]
44/// struct Z;
45///
46/// let bag = BorrowBag::new();
47/// let (bag, x_handle) = bag.add(X);
48/// let (bag, y_handle) = bag.add(Y);
49/// let (bag, z_handle) = bag.add(Z);
50///
51/// let x: &X = bag.borrow(x_handle);
52/// assert_eq!(x, &X);
53/// let y: &Y = bag.borrow(y_handle);
54/// assert_eq!(y, &Y);
55/// let z: &Z = bag.borrow(z_handle);
56/// assert_eq!(z, &Z);
57///
58/// // Can borrow multiple times using the same handle
59/// let x: &X = bag.borrow(x_handle);
60/// assert_eq!(x, &X);
61/// ```
62
63#[derive(Default)]
64pub struct BorrowBag<V> {
65    v: V,
66}
67
68impl BorrowBag<()> {
69    /// Creates a new, empty `BorrowBag`.
70    pub fn new() -> Self {
71        BorrowBag { v: () }
72    }
73}
74
75impl<V> BorrowBag<V> {
76    /// Adds a value to the bag, and returns a tuple containing:
77    ///
78    /// 1. The new bag containing the added element; and
79    /// 2. A `Handle` which can be used to retrieve the added element.
80    ///
81    /// The trait bound is used to constrain and define the `BorrowBag` implementation, but is not
82    /// intended to provide any restrictions on the value being added.
83    ///
84    /// ```rust
85    /// # use borrow_bag::BorrowBag;
86    /// #
87    /// let bag = BorrowBag::new();
88    /// // Consumes the empty `bag`, and produces a new `bag` containing the value. The `handle`
89    /// // can be used to borrow the value back later.
90    /// let (bag, handle) = bag.add(15u8);
91    /// #
92    /// # let _ = (bag, handle);
93    /// ```
94    // This isn't add like +..
95    // Consider renaming this method?
96    pub fn add<T>(self, t: T) -> (BorrowBag<V::Output>, Handle<T, V::Navigator>)
97    where
98        V: Append<T>,
99    {
100        let (v, handle) = Append::append(self.v, t);
101        (BorrowBag { v }, handle)
102    }
103
104    /// Borrows a value previously added to the bag.
105    ///
106    /// ```rust
107    /// # use borrow_bag::BorrowBag;
108    /// #
109    /// # let bag = BorrowBag::new();
110    /// # let (bag, handle) = bag.add(15u8);
111    /// #
112    /// let i: &u8 = bag.borrow(handle);
113    /// assert_eq!(*i, 15);
114    /// ```
115    pub fn borrow<T, N>(&self, _handle: Handle<T, N>) -> &T
116    where
117        V: Lookup<T, N>,
118    {
119        Lookup::<T, N>::get_from(&self.v)
120    }
121}