1use std::marker::PhantomData;
2
3#[derive(Debug, Clone, Copy, Default)]
8pub struct Dep<Head, Tail>(pub Head, pub Tail);
9impl Dep<(), ()> {
10 pub fn new() -> Self {
11 Self((), ())
12 }
13}
14
15impl<Head, Tail> Dep<Head, Tail> {
16 pub fn add<T>(self, item: T) -> Dep<T, Self> {
18 Dep(item, self)
19 }
20
21 #[inline(always)]
26 pub fn get<T, I>(&self) -> &T
27 where
28 Self: Has<T, I>,
29 {
30 Has::<T, I>::get(self)
31 }
32
33 #[inline(always)]
35 pub fn get_mut<T, I>(&mut self) -> &mut T
36 where
37 Self: Has<T, I>,
38 {
39 Has::<T, I>::get_mut(self)
40 }
41}
42pub struct Here;
44
45pub struct There<INDEX>(PhantomData<INDEX>);
47
48pub trait Has<T, Index = ()> {
52 fn get(&self) -> &T;
53 fn get_mut(&mut self) -> &mut T;
54}
55
56impl<T, Tail> Has<T, Here> for Dep<T, Tail> {
58 #[inline(always)]
59 fn get(&self) -> &T {
60 &self.0
61 }
62
63 #[inline(always)]
64 fn get_mut(&mut self) -> &mut T {
65 &mut self.0
66 }
67}
68
69impl<Head, Tail, T, Index> Has<T, There<Index>> for Dep<Head, Tail>
71where
72 Tail: Has<T, Index>,
73{
74 #[inline(always)]
75 fn get(&self) -> &T {
76 self.1.get()
77 }
78
79 #[inline(always)]
80 fn get_mut(&mut self) -> &mut T {
81 self.1.get_mut()
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use crate::dep::{Dep};
88
89 #[test]
90 fn test_dep() {
91 #[derive(Debug, PartialEq)]
92 struct A(i32);
93 #[derive(Debug, PartialEq)]
94 struct B(&'static str);
95 #[derive(Debug, PartialEq)]
96 struct C([i32; 4]);
97
98 let mut dep = Dep::new().add(A(42)).add(B("hello")).add(C([1, 2, 3, 4]));
99
100 assert_eq!(dep.get::<A, _>(), &A(42));
105 let a: &A = dep.get();
107 assert_eq!(a, &A(42));
108
109 assert_eq!(dep.get::<B, _>(), &B("hello"));
111
112 assert_eq!(dep.get::<C, _>().0[0], 1);
114
115 dep.get_mut::<A, _>().0 = 100;
117 assert_eq!(dep.get::<A, _>().0, 100);
118 }
119}