| 1 | use super::framed_write::FramedWrite2; |
| 2 | use super::fuse::Fuse; |
| 3 | use bytes::BytesMut; |
| 4 | use std::io::Error; |
| 5 | |
| 6 | /// Decoding of frames via buffers, for use with `FramedRead`. |
| 7 | pub trait Decoder { |
| 8 | /// The type of items returned by `decode` |
| 9 | type Item; |
| 10 | /// The type of decoding errors. |
| 11 | type Error: From<Error>; |
| 12 | |
| 13 | /// Decode an item from the src `BytesMut` into an item |
| 14 | fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>; |
| 15 | |
| 16 | /// Called when the input stream reaches EOF, signaling a last attempt to decode |
| 17 | /// |
| 18 | /// # Notes |
| 19 | /// |
| 20 | /// The default implementation of this method invokes the `Decoder::decode` method. |
| 21 | fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 22 | self.decode(src) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<T, U: Decoder> Decoder for Fuse<T, U> { |
| 27 | type Item = U::Item; |
| 28 | type Error = U::Error; |
| 29 | |
| 30 | fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 31 | self.u.decode(src) |
| 32 | } |
| 33 | |
| 34 | fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 35 | self.u.decode_eof(src) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<T: Decoder> Decoder for FramedWrite2<T> { |
| 40 | type Item = T::Item; |
| 41 | type Error = T::Error; |
| 42 | |
| 43 | fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 44 | self.inner.decode(src) |
| 45 | } |
| 46 | |
| 47 | fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 48 | self.inner.decode_eof(src) |
| 49 | } |
| 50 | } |