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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::cons::Cons;
use crate::label::Label;
use crate::relation::{False, LabelEq, Member, True};

/// Lookup a specific element in a list by label.
pub trait LookupElemByLabel<TargetL> {
    /// The type of the returned element
    type Elem: ?Sized;
    /// Returns a reference to the element from the list
    fn elem(&self) -> &Self::Elem;
}

impl<TargetL, L, T> LookupElemByLabel<TargetL> for Cons<L, T>
where
    L: Label + LabelEq<TargetL>,
    T: Member<TargetL>,
    Self: LookupElemByLabelMatch<
        TargetL,
        <L as LabelEq<TargetL>>::Output,
        <T as Member<TargetL>>::Output,
    >,
{
    type Elem = <Self as LookupElemByLabelMatch<
        TargetL,
        <L as LabelEq<TargetL>>::Output,
        <T as Member<TargetL>>::Output,
    >>::Elem;

    fn elem(&self) -> &Self::Elem {
        LookupElemByLabelMatch::<
            TargetL,
            <L as LabelEq<TargetL>>::Output,
            <T as Member<TargetL>>::Output,
        >::elem(self)
    }
}

/// Helper trait for [LookupElemByLabel](trait.LookupElemByLabel.html).
pub trait LookupElemByLabelMatch<L, LabelMatch, TailMatch> {
    /// The type of the returned element
    type Elem: ?Sized;
    /// Returns a reference to the element from the list
    fn elem(&self) -> &Self::Elem;
}

// head matches
impl<TargetL, L, T, TailMatch> LookupElemByLabelMatch<TargetL, True, TailMatch> for Cons<L, T>
where
    L: Label,
{
    type Elem = L;

    fn elem(&self) -> &Self::Elem {
        &self.head
    }
}

// head doesn't match but tail does
impl<TargetL, L, T> LookupElemByLabelMatch<TargetL, False, True> for Cons<L, T>
where
    L: Label,
    T: LookupElemByLabel<TargetL>,
{
    type Elem = <T as LookupElemByLabel<TargetL>>::Elem;

    fn elem(&self) -> &Self::Elem {
        LookupElemByLabel::<TargetL>::elem(&self.tail)
    }
}

/// Lookup a specific mutable element in a list by label.
pub trait LookupElemByLabelMut<TargetL>: LookupElemByLabel<TargetL> {
    /// Returns a mutable reference to the element from the list
    fn elem_mut(&mut self) -> &mut Self::Elem;
}

impl<TargetL, L, T> LookupElemByLabelMut<TargetL> for Cons<L, T>
where
    L: Label + LabelEq<TargetL>,
    T: Member<TargetL>,
    Self: LookupElemByLabel<TargetL>,
    Self: LookupElemByLabelMutMatch<
        TargetL,
        <L as LabelEq<TargetL>>::Output,
        <T as Member<TargetL>>::Output,
        Elem = <Self as LookupElemByLabel<TargetL>>::Elem,
    >,
{
    fn elem_mut(&mut self) -> &mut Self::Elem {
        LookupElemByLabelMutMatch::<
            TargetL,
            <L as LabelEq<TargetL>>::Output,
            <T as Member<TargetL>>::Output,
        >::elem_mut(self)
    }
}

/// Helper trait for [LookupElemByLabelMut](trait.LookupElemByLabelMut.html).
pub trait LookupElemByLabelMutMatch<L, LabelMatch, TailMatch>:
    LookupElemByLabelMatch<L, LabelMatch, TailMatch>
{
    /// Returns a mutable reference to the element from the list
    fn elem_mut(&mut self) -> &mut Self::Elem;
}

// head matches
impl<TargetL, L, T, TailMatch> LookupElemByLabelMutMatch<TargetL, True, TailMatch> for Cons<L, T>
where
    L: Label,
{
    fn elem_mut(&mut self) -> &mut Self::Elem {
        &mut self.head
    }
}

// head doesn't match but tail does
impl<TargetL, L, T> LookupElemByLabelMutMatch<TargetL, False, True> for Cons<L, T>
where
    L: Label,
    T: LookupElemByLabelMut<TargetL>,
{
    fn elem_mut(&mut self) -> &mut Self::Elem {
        LookupElemByLabelMut::<TargetL>::elem_mut(&mut self.tail)
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[label(name="My Label", type=String, crate=crate)]
    struct Label1;

    #[label(type=u8, crate=crate)]
    struct Label2;

    #[label(type=&'static str, crate=crate)]
    struct Label3;

    #[test]
    fn lookup() {
        let list = lhlist![
            Label1 = "first value".to_string(),
            Label2 = 2,
            Label3 = "third value",
        ];
        println!("{:?}", LookupElemByLabel::<Label1>::elem(&list));
        println!("{:?}", LookupElemByLabel::<Label2>::elem(&list));
        println!("{:?}", LookupElemByLabel::<Label3>::elem(&list));
    }
}