Skip to main content

libutils_report/
state.rs

1//^
2//^ STATE
3//^
4
5//> STATE -> BASE
6pub trait State {
7    fn chain(&self) -> Vec<&'static str>;
8    fn link<'valid>(&'valid mut self) -> &'valid mut Vec<&'static str>;
9}
10
11//> STATE -> DERIVED
12pub trait DerivedState<'valid>: State + Convert<'valid> {}
13
14//> STATE -> CONVERT
15pub trait Convert<'valid> {
16    fn convert<Current: State>(value: &'valid mut Current) -> Self;
17}
18
19
20//^
21//^ MAIN
22//^
23
24//> MAIN -> STRUCT
25pub struct Main {
26    pub chain: Vec<&'static str> = Vec::new()
27}
28
29//> MAIN -> STATE
30impl State for Main {
31    fn chain(&self) -> Vec<&'static str> {return self.chain.clone()}
32    fn link<'valid>(&'valid mut self) -> &'valid mut Vec<&'static str> {return &mut self.chain}
33}
34
35
36//^
37//^ SAME
38//^
39
40//> SAME -> STRUCT
41pub struct Same<'this> {
42    link: &'this mut Vec<&'static str>
43} 
44
45//> SAME -> STATE
46impl<'this> State for Same<'this> {
47    fn chain(&self) -> Vec<&'static str> {return self.link.clone()}
48    fn link<'valid>(&'valid mut self) -> &'valid mut Vec<&'static str> {return self.link}
49}
50
51//> SAME -> DERIVED STATE
52impl<'valid> DerivedState<'valid> for Same<'valid> {}
53
54//> SAME -> CONVERT
55impl<'valid> Convert<'valid> for Same<'valid> {
56    fn convert<Current: State>(value: &'valid mut Current) -> Self {
57        return Self {
58            link: value.link()
59        }
60    }
61}
62
63
64//^
65//^ NAME
66//^
67
68//> NAME -> STRUCT
69pub struct Name<'valid, const NAME: &'static str> {
70    link: &'valid mut Vec<&'static str>
71} 
72
73//> NAME -> STATE
74impl<'this, const NAME: &'static str> State for Name<'this, NAME> {
75    fn chain(&self) -> Vec<&'static str> {return self.link.clone()}
76    fn link<'valid>(&'valid mut self) -> &'valid mut Vec<&'static str> {return self.link}
77} 
78
79//> NAME -> DROP
80impl<'valid, const NAME: &'static str> Drop for Name<'valid, NAME> {
81    fn drop(&mut self) {self.link.pop();}
82}
83
84//> NAME -> DERIVED STATE
85impl<'valid, const NAME: &'static str> DerivedState<'valid> for Name<'valid, NAME> {}
86
87//> NAME -> CONVERT
88impl<'valid, const NAME: &'static str> Convert<'valid> for Name<'valid, NAME> {
89    fn convert<Current: State>(value: &'valid mut Current) -> Self {
90        value.link().push(NAME);
91        return Self {
92            link: value.link()
93        }
94    }
95}