bytecodec/
bytes.rs

1//! Encoders and decoders for reading/writing byte sequences.
2use crate::{ByteCount, Decode, Encode, Eos, ErrorKind, Result, SizedEncode};
3use std::cmp;
4use std::mem;
5use trackable::error::ErrorKindExt;
6
7/// `BytesEncoder` writes the given bytes into an output byte sequence.
8///
9/// # Examples
10///
11/// ```
12/// use bytecodec::{Encode, EncodeExt};
13/// use bytecodec::bytes::BytesEncoder;
14/// use bytecodec::io::IoEncodeExt;
15///
16/// let mut output = Vec::new();
17/// let mut encoder = BytesEncoder::with_item(b"foo").unwrap();
18/// encoder.encode_all(&mut output).unwrap();
19/// assert!(encoder.is_idle());
20/// assert_eq!(output, b"foo");
21/// ```
22#[derive(Debug)]
23pub struct BytesEncoder<B = Vec<u8>> {
24    bytes: Option<B>,
25    offset: usize,
26}
27impl<B> BytesEncoder<B> {
28    /// Makes a new `BytesEncoder` instance.
29    pub fn new() -> Self {
30        Self::default()
31    }
32}
33impl<B> Default for BytesEncoder<B> {
34    fn default() -> Self {
35        BytesEncoder {
36            bytes: None,
37            offset: 0,
38        }
39    }
40}
41impl<B: AsRef<[u8]>> Encode for BytesEncoder<B> {
42    type Item = B;
43
44    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
45        let mut size = 0;
46        let drop_item = if let Some(ref b) = self.bytes {
47            size = cmp::min(buf.len(), b.as_ref().len() - self.offset);
48            buf[..size].copy_from_slice(&b.as_ref()[self.offset..][..size]);
49            self.offset += size;
50            if self.offset == b.as_ref().len() {
51                true
52            } else {
53                track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos;
54                              buf.len(), size, self.offset, b.as_ref().len());
55                false
56            }
57        } else {
58            false
59        };
60        if drop_item {
61            self.bytes = None;
62        }
63        Ok(size)
64    }
65
66    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
67        track_assert!(self.is_idle(), ErrorKind::EncoderFull);
68        self.bytes = Some(item);
69        self.offset = 0;
70        Ok(())
71    }
72
73    fn requiring_bytes(&self) -> ByteCount {
74        ByteCount::Finite(self.exact_requiring_bytes())
75    }
76
77    fn is_idle(&self) -> bool {
78        self.bytes.is_none()
79    }
80}
81impl<B: AsRef<[u8]>> SizedEncode for BytesEncoder<B> {
82    fn exact_requiring_bytes(&self) -> u64 {
83        self.bytes
84            .as_ref()
85            .map_or(0, |b| b.as_ref().len() - self.offset) as u64
86    }
87}
88
89/// A variant of `BytesDecoder` for copyable bytes types.
90///
91/// Unlike `BytesDecoder`, this has no restriction on decoding count.
92///
93/// # Examples
94///
95/// ```
96/// use bytecodec::{Decode, Eos};
97/// use bytecodec::bytes::CopyableBytesDecoder;
98///
99/// let mut decoder = CopyableBytesDecoder::new([0; 3]);
100/// let mut input = b"foobar";
101///
102/// // Decodes first item
103/// assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));
104/// decoder.decode(&input[0..3], Eos::new(false)).unwrap();
105/// assert_eq!(decoder.is_idle(), true);
106/// assert_eq!(decoder.finish_decoding().unwrap(), *b"foo");
107///
108/// // Decodes second item
109/// assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));
110/// decoder.decode(&input[3..5], Eos::new(false)).unwrap();
111/// assert_eq!(decoder.is_idle(), false);
112/// assert_eq!(decoder.requiring_bytes().to_u64(), Some(1));
113///
114/// decoder.decode(&input[5..], Eos::new(true)).unwrap();
115/// assert_eq!(decoder.is_idle(), true);
116/// assert_eq!(decoder.finish_decoding().unwrap(), *b"bar");
117/// ```
118#[derive(Debug, Default)]
119pub struct CopyableBytesDecoder<B> {
120    bytes: B,
121    offset: usize,
122}
123impl<B> CopyableBytesDecoder<B> {
124    /// Makes a new `CopyableBytesDecoder` instance.
125    pub fn new(bytes: B) -> Self {
126        CopyableBytesDecoder { bytes, offset: 0 }
127    }
128
129    /// Returns a reference to the inner bytes.
130    pub fn inner_ref(&self) -> &B {
131        &self.bytes
132    }
133
134    /// Returns a mutable reference to the inner bytes.
135    pub fn inner_mut(&mut self) -> &mut B {
136        &mut self.bytes
137    }
138
139    /// Takes ownership of this instance and returns the inner bytes.
140    pub fn into_inner(self) -> B {
141        self.bytes
142    }
143}
144impl<B: AsRef<[u8]> + AsMut<[u8]> + Copy> Decode for CopyableBytesDecoder<B> {
145    type Item = B;
146
147    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
148        let size = cmp::min(buf.len(), self.bytes.as_ref().len() - self.offset);
149        self.bytes.as_mut()[self.offset..][..size].copy_from_slice(&buf[..size]);
150        self.offset += size;
151        if self.offset != self.bytes.as_mut().len() {
152            track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos;
153                          self.offset, self.bytes.as_ref().len());
154        }
155        Ok(size)
156    }
157
158    fn finish_decoding(&mut self) -> Result<Self::Item> {
159        track_assert_eq!(
160            self.offset,
161            self.bytes.as_ref().len(),
162            ErrorKind::IncompleteDecoding
163        );
164        self.offset = 0;
165        Ok(self.bytes)
166    }
167
168    fn requiring_bytes(&self) -> ByteCount {
169        ByteCount::Finite((self.bytes.as_ref().len() - self.offset) as u64)
170    }
171
172    fn is_idle(&self) -> bool {
173        self.offset == self.bytes.as_ref().len()
174    }
175}
176
177/// `BytesDecoder` copies bytes from an input sequence to a slice.
178///
179/// This is a oneshot decoder (i.e., it decodes only one item).
180///
181/// # Examples
182///
183/// ```
184/// use bytecodec::Decode;
185/// use bytecodec::bytes::BytesDecoder;
186/// use bytecodec::io::IoDecodeExt;
187///
188/// let mut decoder = BytesDecoder::new([0; 3]);
189/// assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));
190///
191/// let item = decoder.decode_exact(b"foobar".as_ref()).unwrap();
192/// assert_eq!(item.as_ref(), b"foo");
193/// assert_eq!(decoder.requiring_bytes().to_u64(), Some(0)); // no more items are decoded
194/// ```
195#[derive(Debug)]
196pub struct BytesDecoder<B = Vec<u8>> {
197    bytes: Option<B>,
198    offset: usize,
199}
200impl<B: AsRef<[u8]> + AsMut<[u8]>> BytesDecoder<B> {
201    /// Makes a new `BytesDecoder` instance for filling the given byte slice.
202    pub fn new(bytes: B) -> Self {
203        BytesDecoder {
204            bytes: Some(bytes),
205            offset: 0,
206        }
207    }
208
209    /// Sets the byte slice to be filled.
210    pub fn set_bytes(&mut self, bytes: B) {
211        self.bytes = Some(bytes);
212        self.offset = 0;
213    }
214
215    fn exact_requiring_bytes(&self) -> u64 {
216        self.bytes
217            .as_ref()
218            .map_or(0, |b| b.as_ref().len() - self.offset) as u64
219    }
220
221    fn buf_len(&self) -> usize {
222        self.bytes.as_ref().map_or(0, |b| b.as_ref().len())
223    }
224}
225impl<B> Default for BytesDecoder<B> {
226    fn default() -> Self {
227        BytesDecoder {
228            bytes: None,
229            offset: 0,
230        }
231    }
232}
233impl<B: AsRef<[u8]> + AsMut<[u8]>> Decode for BytesDecoder<B> {
234    type Item = B;
235
236    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
237        let size = {
238            let bytes = track_assert_some!(self.bytes.as_mut(), ErrorKind::DecoderTerminated);
239            let size = cmp::min(buf.len(), bytes.as_ref().len() - self.offset);
240            bytes.as_mut()[self.offset..][..size].copy_from_slice(&buf[..size]);
241            self.offset += size;
242            size
243        };
244        if self.exact_requiring_bytes() != 0 {
245            track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos; self.offset, self.buf_len());
246        }
247        Ok(size)
248    }
249
250    fn finish_decoding(&mut self) -> Result<Self::Item> {
251        track_assert_eq!(
252            self.exact_requiring_bytes(),
253            0,
254            ErrorKind::IncompleteDecoding
255        );
256        let bytes = track_assert_some!(self.bytes.take(), ErrorKind::DecoderTerminated);
257        Ok(bytes)
258    }
259
260    fn requiring_bytes(&self) -> ByteCount {
261        ByteCount::Finite(self.exact_requiring_bytes())
262    }
263
264    fn is_idle(&self) -> bool {
265        self.exact_requiring_bytes() == 0
266    }
267}
268
269/// `RemainingBytesDecoder` reads all the bytes from a input sequence until it reaches EOS.
270///
271/// # Examples
272///
273/// ```
274/// use bytecodec::{Decode, Eos};
275/// use bytecodec::bytes::RemainingBytesDecoder;
276///
277/// let mut decoder = RemainingBytesDecoder::new();
278/// assert_eq!(decoder.requiring_bytes().to_u64(), None);
279///
280/// let size = decoder.decode(b"foo", Eos::new(false)).unwrap();
281/// assert_eq!(size, 3);
282/// assert_eq!(decoder.is_idle(), false);
283///
284/// let size = decoder.decode(b"bar", Eos::new(true)).unwrap();
285/// assert_eq!(size, 3);
286/// assert_eq!(decoder.is_idle(), true);
287/// assert_eq!(decoder.finish_decoding().unwrap(), b"foobar");
288/// ```
289#[derive(Debug, Default)]
290pub struct RemainingBytesDecoder {
291    buf: Vec<u8>,
292    eos: bool,
293}
294impl RemainingBytesDecoder {
295    /// Makes a new `RemainingBytesDecoder` instance.
296    pub fn new() -> Self {
297        Self::default()
298    }
299}
300impl Decode for RemainingBytesDecoder {
301    type Item = Vec<u8>;
302
303    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
304        if self.eos {
305            return Ok(0);
306        }
307
308        if let Some(remaining) = eos.remaining_bytes().to_u64() {
309            self.buf.reserve_exact(buf.len() + remaining as usize);
310        }
311        self.buf.extend_from_slice(buf);
312        self.eos = eos.is_reached();
313        Ok(buf.len())
314    }
315
316    fn finish_decoding(&mut self) -> Result<Self::Item> {
317        track_assert!(self.eos, ErrorKind::IncompleteDecoding);
318        self.eos = false;
319        let bytes = mem::take(&mut self.buf);
320        Ok(bytes)
321    }
322
323    fn requiring_bytes(&self) -> ByteCount {
324        if self.eos {
325            ByteCount::Finite(0)
326        } else {
327            ByteCount::Infinite
328        }
329    }
330
331    fn is_idle(&self) -> bool {
332        self.eos
333    }
334}
335
336#[derive(Debug)]
337struct Utf8Bytes<T>(T);
338impl<T: AsRef<str>> AsRef<[u8]> for Utf8Bytes<T> {
339    fn as_ref(&self) -> &[u8] {
340        self.0.as_ref().as_bytes()
341    }
342}
343
344/// `Utf8Encoder` writes the given Rust string into an output byte sequence.
345///
346/// # Examples
347///
348/// ```
349/// use bytecodec::{Encode, EncodeExt};
350/// use bytecodec::bytes::Utf8Encoder;
351/// use bytecodec::io::IoEncodeExt;
352///
353/// let mut output = Vec::new();
354/// let mut encoder = Utf8Encoder::with_item("foo").unwrap();
355/// encoder.encode_all(&mut output).unwrap();
356/// assert!(encoder.is_idle());
357/// assert_eq!(output, b"foo");
358/// ```
359#[derive(Debug)]
360pub struct Utf8Encoder<S = String>(BytesEncoder<Utf8Bytes<S>>);
361impl<S> Utf8Encoder<S> {
362    /// Makes a new `Utf8Encoder` instance.
363    pub fn new() -> Self {
364        Utf8Encoder(BytesEncoder::new())
365    }
366}
367impl<S> Default for Utf8Encoder<S> {
368    fn default() -> Self {
369        Self::new()
370    }
371}
372impl<S: AsRef<str>> Encode for Utf8Encoder<S> {
373    type Item = S;
374
375    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
376        track!(self.0.encode(buf, eos))
377    }
378
379    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
380        track!(self.0.start_encoding(Utf8Bytes(item)))
381    }
382
383    fn requiring_bytes(&self) -> ByteCount {
384        self.0.requiring_bytes()
385    }
386
387    fn is_idle(&self) -> bool {
388        self.0.is_idle()
389    }
390}
391impl<S: AsRef<str>> SizedEncode for Utf8Encoder<S> {
392    fn exact_requiring_bytes(&self) -> u64 {
393        self.0.exact_requiring_bytes()
394    }
395}
396
397/// `Utf8Decoder` decodes Rust strings from a input byte sequence.
398///
399/// # Examples
400///
401/// ```
402/// use bytecodec::{Decode, Eos};
403/// use bytecodec::bytes::Utf8Decoder;
404///
405/// let mut decoder = Utf8Decoder::new();
406///
407/// decoder.decode(b"foo", Eos::new(true)).unwrap();
408/// assert_eq!(decoder.finish_decoding().unwrap(), "foo");
409/// ```
410#[derive(Debug, Default)]
411pub struct Utf8Decoder<D = RemainingBytesDecoder>(D);
412impl Utf8Decoder<RemainingBytesDecoder> {
413    /// Makes a new `Utf8Decoder` that uses `RemainingBytesDecoder` as the internal bytes decoder.
414    pub fn new() -> Self {
415        Utf8Decoder(RemainingBytesDecoder::new())
416    }
417}
418impl<D> Utf8Decoder<D>
419where
420    D: Decode<Item = Vec<u8>>,
421{
422    /// Makes a new `Utf8Decoder` with the given bytes decoder.
423    pub fn with_bytes_decoder(bytes_decoder: D) -> Self {
424        Utf8Decoder(bytes_decoder)
425    }
426
427    /// Returns a reference to the inner bytes decoder.
428    pub fn inner_ref(&self) -> &D {
429        &self.0
430    }
431
432    /// Returns a mutable reference to the inner bytes decoder.
433    pub fn inner_mut(&mut self) -> &mut D {
434        &mut self.0
435    }
436
437    /// Takes ownership of this instance and returns the inner bytes decoder.
438    pub fn into_inner(self) -> D {
439        self.0
440    }
441}
442impl<D> Decode for Utf8Decoder<D>
443where
444    D: Decode<Item = Vec<u8>>,
445{
446    type Item = String;
447
448    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
449        track!(self.0.decode(buf, eos))
450    }
451
452    fn finish_decoding(&mut self) -> Result<Self::Item> {
453        let b = track!(self.0.finish_decoding())?;
454        let s = track!(String::from_utf8(b).map_err(|e| ErrorKind::InvalidInput.cause(e)))?;
455        Ok(s)
456    }
457
458    fn requiring_bytes(&self) -> ByteCount {
459        self.0.requiring_bytes()
460    }
461
462    fn is_idle(&self) -> bool {
463        self.0.is_idle()
464    }
465}
466
467#[cfg(test)]
468mod test {
469    use super::*;
470    use crate::io::{IoDecodeExt, IoEncodeExt};
471    use crate::{Encode, EncodeExt, ErrorKind};
472
473    #[test]
474    fn bytes_decoder_works() {
475        let mut decoder = BytesDecoder::new([0; 3]);
476        assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));
477
478        let mut input = b"foobar".as_ref();
479        let item = decoder.decode_exact(&mut input).unwrap();
480        assert_eq!(item.as_ref(), b"foo");
481        assert_eq!(decoder.requiring_bytes().to_u64(), Some(0));
482
483        assert_eq!(
484            decoder.decode_exact(&mut input).err().map(|e| *e.kind()),
485            Some(ErrorKind::DecoderTerminated)
486        );
487    }
488
489    #[test]
490    fn utf8_encoder_works() {
491        let mut buf = Vec::new();
492        let mut encoder = Utf8Encoder::with_item("foo").unwrap();
493        encoder.encode_all(&mut buf).unwrap();
494        assert!(encoder.is_idle());
495        assert_eq!(buf, b"foo");
496    }
497}