| 1 | use craterust-analyzer cargo futures_codec 0.4.1 crate/::{Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#, Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#}; |
| 2 | use bytesrust-analyzer cargo bytes 1.12.1 crate/::{Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#, BufMutrust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#, Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#, BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#}; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#; |
| 4 | |
| 5 | const U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH.: usize = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<u64>(); |
| 6 | |
| 7 | /// A simple `Codec` implementation sending your data by prefixing it by its length. |
| 8 | /// |
| 9 | /// # Example |
| 10 | /// |
| 11 | /// This codec will most likely be used wrapped in another codec like so. |
| 12 | /// |
| 13 | /// ``` |
| 14 | /// use futures_codec::{Decoder, Encoder, LengthCodec}; |
| 15 | /// use bytes::{Bytes, BytesMut}; |
| 16 | /// use std::io::{Error, ErrorKind}; |
| 17 | /// |
| 18 | /// pub struct MyStringCodec(LengthCodec); |
| 19 | /// |
| 20 | /// impl Encoder for MyStringCodec { |
| 21 | /// type Item = String; |
| 22 | /// type Error = Error; |
| 23 | /// |
| 24 | /// fn encode(&mut self, src: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { |
| 25 | /// let bytes = Bytes::from(src); |
| 26 | /// self.0.encode(bytes, dst) |
| 27 | /// } |
| 28 | /// } |
| 29 | /// |
| 30 | /// impl Decoder for MyStringCodec { |
| 31 | /// type Item = String; |
| 32 | /// type Error = Error; |
| 33 | /// |
| 34 | /// fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 35 | /// match self.0.decode(src)? { |
| 36 | /// Some(bytes) => { |
| 37 | /// match String::from_utf8(bytes.to_vec()) { |
| 38 | /// Ok(string) => Ok(Some(string)), |
| 39 | /// Err(e) => Err(Error::new(ErrorKind::InvalidData, e)) |
| 40 | /// } |
| 41 | /// }, |
| 42 | /// None => Ok(None), |
| 43 | /// } |
| 44 | /// } |
| 45 | /// } |
| 46 | /// ``` |
| 47 | pub struct LengthCodecrust-analyzer cargo futures_codec 0.4.1 codec/length/LengthCodec#; |
| 48 | |
| 49 | impl Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder# for LengthCodecrust-analyzer cargo futures_codec 0.4.1 codec/length/LengthCodec# { |
| 50 | type Itemrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Encoder]Item# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#; |
| 51 | type Errorrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Encoder]Error# = Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#; |
| 52 | |
| 53 | fn encoderust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Encoder]encode().(&mut self, src: Selfrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Encoder]::Itemrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#Item#, dst: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Encoder]::Errorrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#Error#> { |
| 54 | dst.reserverust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]reserve().(U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().src.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().()); |
| 55 | dst.put_u64rust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#put_u64().(src.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().() as u64); |
| 56 | dst.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&src); |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder# for LengthCodecrust-analyzer cargo futures_codec 0.4.1 codec/length/LengthCodec# { |
| 62 | type Itemrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]Item# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#; |
| 63 | type Errorrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]Error# = Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#; |
| 64 | |
| 65 | fn decoderust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]decode().(&mut self, src: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]::Itemrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Item#>, Selfrust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]::Errorrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Error#> { |
| 66 | if src.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH. { |
| 67 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 68 | } |
| 69 | |
| 70 | let mut len_bytes = [0u8; U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH.]; |
| 71 | len_bytes.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&src[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 72 | let len = u64::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]from_be_bytes().(len_bytes) as usize; |
| 73 | |
| 74 | if src.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len { |
| 75 | // Skip the length header we already read. |
| 76 | src.advancerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][Buf]advance().(U64_LENGTHrust-analyzer cargo futures_codec 0.4.1 codec/length/U64_LENGTH.); |
| 77 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(src.split_torust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]split_to().(len).freezerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]freeze().())) |
| 78 | } else { |
| 79 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[cfg(test)] |
| 85 | mod testsrust-analyzer cargo futures_codec 0.4.1 codec/length/tests/ { |
| 86 | use superrust-analyzer cargo futures_codec 0.4.1 codec/length/::*; |
| 87 | |
| 88 | mod decoderust-analyzer cargo futures_codec 0.4.1 codec/length/tests/decode/ { |
| 89 | use superrust-analyzer cargo futures_codec 0.4.1 codec/length/tests/::*; |
| 90 | |
| 91 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 92 | fn it_returns_bytes_withouth_length_headerrust-analyzer cargo futures_codec 0.4.1 codec/length/tests/decode/it_returns_bytes_withouth_length_header().() { |
| 93 | let mut codec = LengthCodecrust-analyzer cargo futures_codec 0.4.1 codec/length/LengthCodec# {}; |
| 94 | |
| 95 | let mut src = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]with_capacity().(5); |
| 96 | src.putrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][BufMut]put().(&[0, 0, 0, 0, 0, 0, 0, 3u8, 1, 2, 3, 4][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().); |
| 97 | let item = codec.decoderust-analyzer cargo futures_codec 0.4.1 codec/length/impl#[LengthCodec][Decoder]decode().(&mut src).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 98 | |
| 99 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(item == Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::fromrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes][`From<&'static [u8]>`]from().(&[1u8, 2, 3][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().))); |
| 100 | } |
| 101 | } |
| 102 | } |