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
//
// Copyright (c) 2015-2019 Plausible Labs Cooperative, Inc.
// All rights reserved.
//
// This implementation based on List type from:
//   https://github.com/epsilonz/shoggoth.rs
//

/// A heterogeneous list that can hold elements of different types.
pub trait HList {
    /// Creates a new `HCons` with the given `X` value in head position.
    fn cons<X>(self, x: X) -> HCons<X, Self>
    where
        Self: Sized,
    {
        HCons(x, self)
    }
}

/// An empty `HList` used as the terminal element.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct HNil;

impl HList for HNil {}

/// The "cons" of a head element of type `H` and a tail `HList`.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct HCons<H, T: HList>(pub H, pub T);

impl<H, T: HList> HCons<H, T> {
    /// Returns a reference to the head element of this list.
    pub fn head(&self) -> &H {
        &self.0
    }

    /// Returns a reference to the tail of this list.
    pub fn tail(&self) -> &T {
        &self.1
    }
}

impl<H, T: HList> HList for HCons<H, T> {}

/// Allows for conversion from an `HList` to an instance of the `Self` type.
pub trait FromHList<H>
where
    H: HList,
{
    fn from_hlist(hlist: H) -> Self;
}

/// Allows for copying the contents of `Self` into an `HList`.
pub trait ToHList<H>
where
    H: HList,
{
    fn to_hlist(&self) -> H;
}

/// Allows for converting (and consuming) `Self` into an `HList`.
pub trait IntoHList<H>
where
    H: HList,
{
    fn into_hlist(self) -> H;
}

#[cfg(test)]
mod tests {
    use super::*;
    use pl_hlist_derive::HListSupport;

    #[test]
    fn head_should_work() {
        let hlist = HCons(1u8, HNil);
        assert_eq!(*hlist.head(), 1u8);
    }

    #[test]
    fn tail_should_work() {
        let hlist = HCons(1u8, HNil);
        assert_eq!(*hlist.tail(), HNil);
    }

    #[test]
    fn hlist_macros_should_work() {
        {
            let hlist1 = HNil;
            let hlist2 = hlist!();
            assert_eq!(hlist1, hlist2);
        }

        {
            let hlist1 = HCons(1u8, HNil);
            let hlist2 = hlist!(1u8);
            assert_eq!(hlist1, hlist2);
        }

        {
            let hlist1 = HCons(1u8, HCons(2i32, HCons("three", HNil)));
            let hlist2 = hlist!(1u8, 2i32, "three");
            assert_eq!(hlist1, hlist2);
        }
    }

    #[derive(Debug, PartialEq, Eq, Clone, HListSupport)]
    struct TestInnerStruct {
        f1: u8,
        f2: u8,
    }

    #[derive(Debug, PartialEq, Eq, Clone, HListSupport)]
    struct TestStruct {
        byte_field: u8,
        inner_struct: TestInnerStruct,
    }

    #[test]
    fn converting_struct_to_from_hlist_should_work() {
        {
            let s = TestInnerStruct::from_hlist(hlist!(1u8, 2u8));
            assert_eq!(s.f1, 1u8);
            assert_eq!(s.f2, 2u8);
            let hlist0 = s.to_hlist();
            assert_eq!(hlist0, hlist!(1u8, 2u8));
            let hlist1 = s.into_hlist();
            assert_eq!(hlist0, hlist1);
        }

        {
            let s =
                TestStruct::from_hlist(hlist!(7u8, TestInnerStruct::from_hlist(hlist!(1u8, 2u8))));
            assert_eq!(s.byte_field, 7u8);
            assert_eq!(s.inner_struct, TestInnerStruct { f1: 1, f2: 2 });
            let hlist0 = s.to_hlist();
            assert_eq!(hlist0, hlist!(7u8, TestInnerStruct { f1: 1, f2: 2 }));
            let hlist1 = s.into_hlist();
            assert_eq!(hlist0, hlist1);
        }
    }
}