borrow_bag/lookup.rs
1use handle::{Skip, Take};
2
3/// Allows borrowing a value of type `T` from the implementing type. This can be used to constrain
4/// a `Handle` argument to ensure it can be used with the corresponding `BorrowBag`.
5///
6/// # Examples
7///
8/// ```rust
9/// # use borrow_bag::*;
10/// #
11/// fn borrow_from<V, T, N>(bag: &BorrowBag<V>, handle: Handle<T, N>) -> &T
12/// where
13/// V: Lookup<T, N>,
14/// {
15/// bag.borrow(handle)
16/// }
17/// #
18/// # fn main() {
19/// # let bag = BorrowBag::new();
20/// # let (bag, handle) = bag.add(1u8);
21/// #
22/// # assert_eq!(1u8, *borrow_from(&bag, handle));
23/// # }
24/// ```
25pub trait Lookup<T, N> {
26 /// Borrows the value of type `T`. Internal API and not for public use.
27 #[doc(hidden)]
28 fn get_from(&self) -> &T;
29}
30
31#[doc(hidden)]
32impl<T, U, V, N> Lookup<T, (Skip, N)> for (U, V)
33where
34 V: Lookup<T, N>,
35{
36 fn get_from(&self) -> &T {
37 self.1.get_from()
38 }
39}
40
41#[doc(hidden)]
42impl<T, V> Lookup<T, Take> for (T, V) {
43 fn get_from(&self) -> &T {
44 &self.0
45 }
46}