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

pub trait KnowsBranches<'a> {
    type Branches;
}

pub trait HasBranches<'a>: KnowsBranches<'a>
{
    fn branches(self) -> impl Iterator<Item = Self::Branches>;

    // fn branch<K>(&mut self, key: K) -> &mut T
    // where K: Into<T::PathSegment>,
    //       T: From<T::PathSegment>
    // {
    //     // This works and it's safe, but the borrow checker doesn't like it.
    //     // https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions
    //     let myself = unsafe { &mut *(self as *mut Self) };
    //     let key = key.into();
    //     if let Some(value) = myself.get_mut(key.clone()) {
    //         value
    //     } else {
    //         self.add_branch(T::from(key))
    //     }
    // }

    // fn get<K>(&self, key: K) -> Option<&T>
    // where K: Into<T::PathSegment> {
    //     let key = key.into();
    //     self
    //         .branches()
    //         .find(|branch| branch.path_segment() == &key)
    // }
    
    // fn get_mut<K>(&mut self, key: K) -> Option<&mut T>
    // where K: Into<T::PathSegment> {
    //     let key = key.into();
    //     self
    //         .branches_mut()
    //         .find(|branch| branch.path_segment() == &key)
    // }
}

impl<'a, T> KnowsBranches<'a> for &'a T
where T: KnowsBranches<'a>
{
    type Branches = T::Branches;
}

impl<'a, T> KnowsBranches<'a> for &'a mut T
where T: KnowsBranches<'a>
{
    type Branches = T::Branches;
}