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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pub trait Lense<'a> {
    fn new(ptr: &'a mut [u8]) -> (Self, &'a mut [u8]);
    fn size() -> usize;
}

#[doc(hidden)]
pub trait IntoLense<'a> {
    type Lense: Lense<'a>;
}

/// Continuous lense reader allowing the consumer to to read as many of their type that is present
/// in the stream. TODO: Replace with an Interator
pub trait ContinuousLenseReader<'a>: Lense<'a> + Sized {
    /// Start working or continue iterating over a byte stream collecting lenses one-by-one.
    fn from_buf(mut ptr: &mut &'a mut [u8]) -> Result<Option<Self>, ()> {
        match ptr.len() {
            // Complete, nothing more to read
            0 => Ok(None),
            // Incomplete entry, possibly to be stitched together
            x if x < Self::size() => Err(()),
            // Correct size found and able to be serialised directly into the consuming lense
            _ => Ok(Some(slice_lense_chunk(&mut ptr))),
        }
    }
}

#[doc(hidden)]
pub fn slice_lense_chunk<'a, L: Lense<'a>>(ptr: &mut &'a mut [u8]) -> L {
    let mut x: &mut [u8] = &mut [];
    ::std::mem::swap(&mut x, ptr);
    let (v, rest) = L::new(x);
    *ptr = rest;
    v
}

// Implement lense for primitive types, tuples and arrays

macro_rules! lense_prim {
    ($($ty:ty),+$(,)*) => {$(
        impl<'a> Lense<'a> for &'a mut $ty {
            #[inline]
            fn new(ptr: &'a mut [u8]) -> (Self, &'a mut [u8]) {
                let (v, rest) = ptr.split_at_mut(Self::size());
                (unsafe { &mut *(v.as_ptr() as *mut $ty) }, rest)
            }

            #[inline]
            fn size() -> usize { ::std::mem::size_of::<$ty>() }
        }

        impl<'a> IntoLense<'a> for $ty {
            type Lense = &'a mut $ty;
        }
    )+};
}

macro_rules! lense_tuple {
    (@tail $head:ident) => {};

    (@tail $head:ident $($ty:ident)+) => {
        lense_tuple!{ $($ty)+ }
    };

    ($($ty:ident)*) => {
        impl<'a, $($ty: Lense<'a>),*> Lense<'a> for ($($ty,)*) {
            fn new(mut ptr: &'a mut [u8]) -> (Self, &mut [u8]) {
                (($($crate::slice_lense_chunk::<'a, $ty>(&mut ptr),)*), ptr)
            }

            fn size() -> usize {
                0 $(+ $ty::size())*
            }
        }

        impl<'a, $($ty: IntoLense<'a>),*> IntoLense<'a> for ($($ty,)*) {
            type Lense = ($(<$ty as IntoLense<'a>>::Lense,)*);
        }

        lense_tuple!{ @tail $($ty)+ }
    };
}

macro_rules! lense_array {
    (@void ($x:expr) $expr:expr) => ($expr);

    () => ();

    (($n:expr) $(($m:expr))*) => {
        impl<'a, L> Lense<'a> for [L; $n] where L: Lense<'a> {
            fn new(mut v: &'a mut [u8]) -> (Self, &mut [u8]) {
                ([$(lense_array!( @void ($m) $crate::slice_lense_chunk(&mut v) )),*], v)
            }
            fn size() -> usize {
                $n * L::size()
            }
        }

        impl<'a, L: IntoLense<'a>> IntoLense<'a> for [L; $n] {
            type Lense = [<L as IntoLense<'a>>::Lense; $n];
        }

        lense_array!{ $(($m))* }
    }
}

lense_prim!{
    u8,    i8,
   u16,   i16,
   u32,   i32,   f32,
   u64,   i64,   f64,
}

lense_tuple!{A B C D E F G H I J K L M}
 
lense_array!{
    (32) (31) (30) (29) (28) (27) (26) (25)
    (24) (23) (22) (21) (20) (19) (18) (17)
    (16) (15) (14) (13) (12) (11) (10) ( 9)
    ( 8) ( 7) ( 6) ( 5) ( 4) ( 3) ( 2) ( 1)
    ( 0)
}

// Implement user defined structs

#[macro_export]
macro_rules! lense_struct {
    (pub $lense:ident: $($name:ident: $ty:ty),* $(,)*) => {
        lense_struct!{PUB $lense: $($name: $ty),*}
    };

    ($lense:ident: $($name:ident: $ty:ty),* $(,)*) => {
        lense_struct!{PRIV $lense: $($name: $ty),*}
    };

    (PUB struct $lense:ident: $($name:ident: $ty:ty),*) => {
        pub struct $lense<'a> {
            $($name: <$ty as $crate::IntoLense<'a>>::Lense),*
        }
    };

    (PRIV struct $lense:ident: $($name:ident: $ty:ty),*) => {
        struct $lense<'a> {
            $($name: <$ty as $crate::IntoLense<'a>>::Lense),*
        }
    };

    ($vis:ident $lense:ident: $($struct_item_name:ident: $struct_item_type:ty),*) => {
        lense_struct!{ $vis struct $lense: $($struct_item_name: $struct_item_type),* }

        impl<'a> $crate::Lense<'a> for $lense<'a> {
            fn new(mut v: &'a mut [u8]) -> (Self, &mut [u8]) {
                ($lense {
                    $($struct_item_name: $crate::slice_lense_chunk(&mut v)),*
                }, v)
            }

            fn size() -> usize {
                0 $(+ <$struct_item_type as $crate::IntoLense<'a>>::Lense::size())*
            }
        }

        impl<'a> $crate::IntoLense<'a> for $lense<'a> {
            type Lense = $lense<'a>;
        }

        impl<'a> $crate::ContinuousLenseReader<'a> for $lense<'a> { }
    }
}