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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::mem;

/// A type that represents an object that consumes a memory slice on demand.
/// The consumer lives as long as the slice is alive.
pub struct ByteConsumer<'b> {
        /// The memory slice that get's consumed.
        buffer: &'b [u8],
}

impl<'b> ByteConsumer<'b> {
        /// Creates a new ByteConsumer from a stream of bytes.
        pub fn new(stream: &'b [u8]) -> ByteConsumer {
            ByteConsumer { buffer: stream }
    }
    
    /// Consumes `nr_bytes` from the original slice and returns an `Option` that holds a reference
    /// to a memory slice which is `nr_bytes` long. If `nr_bytes` is greater than the remaining
    /// bytes of the consumer's slice then `None` is returned.
    /// 
    /// *Note*: The memory slice returned is not copied, it's a reference to the initial slice
    /// 
    /// # Arguments
    /// 
    /// * `nr_bytes` - A number representing the number of bytes to consume
    /// 
    /// 
    /// # Examples
    /// 
    /// ```
    /// use byte_consumer::ByteConsumer;
    /// let mut consumer = ByteConsumer::new(&[10, 20, 30, 40]);
    /// let first_two = consumer.consume(2);
    /// let second_two = consumer.consume(2);
    /// 
    /// assert!(first_two.is_some());
    /// assert_eq!(first_two.unwrap(), [10, 20]);
    /// 
    /// assert!(second_two.is_some());
    /// assert_eq!(second_two.unwrap(), [30, 40]);
    /// 
    /// assert_eq!(consumer.consume(1), None);
    /// ```
    /// 
    #[inline]
        pub fn consume(&mut self, nr_bytes: usize) -> Option<&'b [u8]> {
            if nr_bytes > self.buffer.len() || nr_bytes == 0 {
                None
        } else {
            let (res, remaining) = self.buffer.split_at(nr_bytes);
                self.buffer = remaining;
                Some(res)
        }
    }
    
    /// Consumes `nr_bytes` from the original slice and returns a reference to a memory slice which is `nr_bytes` long.
    /// 
    /// *Note*: The memory slice returned is not copied, it's a reference to the initial slice
    /// 
    /// # Arguments
    /// 
    /// * `nr_bytes` - A number representing the number of bytes to consume
    /// 
    /// # Panics
    /// 
    /// If `nr_bytes` is greater than the remaining bytes in the consumer's slice then
    /// then it panics with a custom message.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use byte_consumer::ByteConsumer;
    /// let mut consumer = ByteConsumer::new(&[10, 20, 30, 40]);
    /// let first_two = consumer.consume_unchecked(2);
    /// let second_two = consumer.consume_unchecked(2);
    /// 
    /// assert_eq!(first_two, [10, 20]);
    /// assert_eq!(second_two, [30, 40]);
    /// 
    /// // let next = consumer.consume_unchecked(1) <-- This should panic.
    /// ``` 
    #[inline]
        pub fn consume_unchecked(&mut self, nr_bytes: usize) -> &'b [u8] {
            self.consume(nr_bytes).expect(format!(
            "[ByteConsumer]: The number of bytes requested ({}) is greater that the number of bytes available ({}).",
            nr_bytes,
            self.buffer.len()
            ).as_str())
    }
    
    
    /// Consumes as many bytes as the size of type `T` and converts them to an object of type `T`.
    /// If the size of type `T` in number of bytes is greater than the remaining bytes in the consumer's buffer
    /// then it returns None.
    /// 
    /// *Note*: The type `T` must implement the `Copy` trait as we are moving the bytes out of the slice
    /// 
    /// # Examples
    /// 
    /// ```
    /// use byte_consumer::ByteConsumer;
    /// 
    /// let mut consumer = ByteConsumer::new(&[0x1, 0x2, 0x1, 0x1]);
    /// let first_byte_as_u8: Option<u8> = consumer.consume_as();
    /// let second_byte_as_u8 = consumer.consume_as::<u8>();
    /// let last_two_as_u16 = consumer.consume_as::<u16>();
    /// 
    /// assert!(first_byte_as_u8.is_some());
    /// assert_eq!(first_byte_as_u8.unwrap(), 1);
    /// 
    /// assert!(second_byte_as_u8.is_some());
    /// assert_eq!(second_byte_as_u8.unwrap(), 2);
    /// 
    /// assert!(last_two_as_u16.is_some());
    /// assert_eq!(last_two_as_u16.unwrap(), 257);
    /// 
    /// assert_eq!(consumer.consume_as::<u32>(), None);
    /// ```
    #[inline]
        pub fn consume_as<T: Copy>(&mut self) -> Option<T> {
            let bytes = self.consume(mem::size_of::<T>());
            match bytes {
                Some(bytes) => unsafe { Some(*(bytes.as_ptr() as *const T)) },
                None => None,
        }
    }
    
    /// Consumes as many bytes as the size of type `T` and converts them to an object of type `T`.
    /// 
    /// *Note*: The type `T` must implement the `Copy` trait as we are moving the bytes out of the slice
    /// 
    /// # Panics
    /// 
    /// If size of type `T` in bytes is greater than the remaining bytes in the consumer's buffer
    /// then it panics with a custom message.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use byte_consumer::ByteConsumer;
    /// 
    /// let mut consumer = ByteConsumer::new(&[0x1, 0x2, 0x1, 0x1]);
    /// let first_byte_as_u8: u8 = consumer.consume_as_unchecked();
    /// let second_byte_as_u8 = consumer.consume_as_unchecked::<u8>();
    /// let last_two_as_u16 = consumer.consume_as_unchecked::<u16>();
    /// 
    /// assert_eq!(first_byte_as_u8, 1);
    /// assert_eq!(second_byte_as_u8, 2);
    /// assert_eq!(last_two_as_u16, 257);
    /// 
    /// // let next = consumer.consume_as_unchecked::<u32>() <-- This should panic.
    /// ```
    #[inline]
        pub fn consume_as_unchecked<T: Copy>(&mut self) -> T {
            self.consume_as::<T>().expect(format!(
            "[ByteConsumer]: The number of bytes ({}) in the type ({}) is greater that the number of bytes available ({}).",
            mem::size_of::<T>(),
            std::any::type_name::<T>(),
            self.buffer.len()
            ).as_str())
    }
    
    /// Returns whether there are remaining bytes to consume or not.
    /// # Examples
    ///
    /// ```
    /// // Calling has_remaining on a consumer with an empty slice should return false.
    /// use byte_consumer::ByteConsumer;
    /// let mut consumer = ByteConsumer::new(&[]);
    /// assert!(!consumer.has_remaining());
    /// ```
    ///
    /// ```
    /// // Calling has_remaining on a non consumed or partially consumed slice should return true.
    /// // After consuming everything, we should get false
    /// use byte_consumer::ByteConsumer;
    /// let mut consumer = ByteConsumer::new(&[1, 2, 3, 4]);
    /// assert!(consumer.has_remaining());
    /// consumer.consume(3);
    /// assert!(consumer.has_remaining());
    /// consumer.consume(1);
    /// assert!(!consumer.has_remaining());
    /// ```
    #[inline]
        pub fn has_remaining(&mut self) -> bool {
            self.buffer.len() != 0
    }
}

#[cfg(test)]
mod tests {
        use super::*;
    
        #[test]
        fn test_consume_bytes() {
            let mut consumer = ByteConsumer::new(&[10, 20, 30, 40, 50]);
            let first_two = consumer.consume(2);
            let third = consumer.consume(1);
            let last_two = consumer.consume(2);
        
            assert!(first_two.is_some());
            assert_eq!(first_two.unwrap(), [10, 20]);
        
            assert!(third.is_some());
            assert_eq!(third.unwrap(), [30]);
        
            assert!(last_two.is_some());
            assert_eq!(last_two.unwrap(), [40, 50]);
        
            assert!(consumer.consume(10).is_none());
            assert!(consumer.consume(100).is_none());
    }
    
    #[test]
        fn test_consume_bytes_unchecked() {
            let mut consumer = ByteConsumer::new(&[10, 20, 30, 40, 50]);
            assert_eq!(consumer.consume_unchecked(2), [10, 20]);
            assert_eq!(consumer.consume_unchecked(3), [30, 40, 50]);
    }
    
    #[test]
        fn test_consume_bytes_iteratively() {
            let view = [10, 20, 30, 40, 50];
            let mut index = 0;
            let mut consumer = ByteConsumer::new(&view);
            while let Some(bytes) = consumer.consume(1) {
                assert!(!bytes.is_empty());
                assert_eq!(bytes.len(), 1);
                assert_eq!(view[index], bytes[0]);
                index += 1;
        }
        assert_eq!(index, view.len());
    }
    
    #[test]
        fn test_consume_bytes_iteratively_unchecked() {
            let view = [10, 20, 30, 40, 50];
            let mut index = 0;
            let mut consumer = ByteConsumer::new(&view);
            while consumer.has_remaining() {
                assert_eq!(view[index], consumer.consume_unchecked(1)[0]);
                index += 1;
        }
        assert_eq!(index, view.len());
    }
    
    #[test]
        #[should_panic]
        fn test_panic_on_uncecked_consume() {
            let mut consumer = ByteConsumer::new(&[10, 20, 30, 40]);
            assert_eq!(consumer.consume_unchecked(4), [10, 20, 30, 40]); // This should not panic
            consumer.consume_unchecked(1); // This should panic
    }
    
    #[test]
        #[should_panic]
        fn test_panic_on_unchecked_consume_iteratively() {
            let view = [10, 20, 30, 40];
            let mut consumer = ByteConsumer::new(&view);
            for _ in 0..view.len() + 1 {
                consumer.consume_unchecked(1);
        }
    }
    
    #[test]
        fn test_consume_on_empty_slice() {
            let mut consumer = ByteConsumer::new(&[]);
            assert!(consumer.consume(1).is_none());
            assert!(consumer.consume_as::<u128>().is_none());
    }
    
    #[test]
        fn test_has_remaining_on_empty_slice() {
            let mut consumer = ByteConsumer::new(&[]);
            assert!(!consumer.has_remaining());
    }
    
    #[test]
        fn test_has_remaining_on_unconsumed_slice() {
            let mut consumer = ByteConsumer::new(&[1, 2, 3, 4]);
            assert!(consumer.has_remaining());
    }
    
    #[test]
        fn test_has_remaining_on_partially_consumed_slice() {
            let mut consumer = ByteConsumer::new(&[1, 2, 3, 4]);
            consumer.consume(3);
            assert!(consumer.has_remaining());
    }
    
    #[test]
        fn test_has_remaining_on_totaly_consumed_slice() {
            let mut consumer = ByteConsumer::new(&[1, 2, 3, 4]);
            consumer.consume(4);
            assert!(!consumer.has_remaining());
    }
}