1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{Cons, HList};
use super::{Here, Index, There};
/// Retrieve element of the heterogenous list by type.
pub trait Get<T, I>: HList
where
I: Index,
{
/// Retrieves a reference to the element of the heterogenous list by type.
///
/// # Examples
///
/// ```
/// use hlist2::{hlist, ops::Get};
///
/// let list = hlist![0_i32, 1_i64];
/// let a: i64 = *list.get();
/// assert_eq!(a, 1);
/// ```
fn get(&self) -> &T;
/// Retrieves a mutable reference to the element of the heterogenous list by type.
///
/// # Examples
///
/// ```
/// use hlist2::{hlist, ops::Get};
///
/// let mut list = hlist![0_i32, 1_i64];
/// *list.get_mut() = 5_i32;
/// let a: i32 = *list.get();
/// assert_eq!(a, 5);
/// ```
fn get_mut(&mut self) -> &mut T;
}
/// Desired type is located in the head of the heterogenous list.
impl<Head, Tail> Get<Head, Here> for Cons<Head, Tail>
where
Tail: HList + ?Sized,
{
fn get(&self) -> &Head {
let Cons(head, _) = self;
head
}
fn get_mut(&mut self) -> &mut Head {
let Cons(head, _) = self;
head
}
}
/// Desired type is located somewhere in the tail of the heterogenous list.
impl<Head, Tail, FromTail, TailIndex> Get<FromTail, There<TailIndex>> for Cons<Head, Tail>
where
Tail: Get<FromTail, TailIndex> + ?Sized,
TailIndex: Index,
{
fn get(&self) -> &FromTail {
let Cons(_, tail) = self;
tail.get()
}
fn get_mut(&mut self) -> &mut FromTail {
let Cons(_, tail) = self;
tail.get_mut()
}
}