| 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/::{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 | /// A simple codec that ships bytes around |
| 6 | /// |
| 7 | /// # Example |
| 8 | /// |
| 9 | /// ``` |
| 10 | /// # futures::executor::block_on(async move { |
| 11 | /// use bytes::Bytes; |
| 12 | /// use futures::{SinkExt, TryStreamExt}; |
| 13 | /// use futures::io::Cursor; |
| 14 | /// use futures_codec::{BytesCodec, Framed}; |
| 15 | /// |
| 16 | /// let mut buf = vec![]; |
| 17 | /// // Cursor implements AsyncRead and AsyncWrite |
| 18 | /// let cur = Cursor::new(&mut buf); |
| 19 | /// let mut framed = Framed::new(cur, BytesCodec); |
| 20 | /// |
| 21 | /// framed.send(Bytes::from("Hello World!")).await?; |
| 22 | /// |
| 23 | /// while let Some(bytes) = framed.try_next().await? { |
| 24 | /// dbg!(bytes); |
| 25 | /// } |
| 26 | /// # Ok::<_, std::io::Error>(()) |
| 27 | /// # }).unwrap(); |
| 28 | /// ``` |
| 29 | pub struct BytesCodecrust-analyzer cargo futures_codec 0.4.1 codec/bytes/BytesCodec#; |
| 30 | |
| 31 | impl Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder# for BytesCodecrust-analyzer cargo futures_codec 0.4.1 codec/bytes/BytesCodec# { |
| 32 | type Itemrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Encoder]Item# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#; |
| 33 | type Errorrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Encoder]Error# = Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#; |
| 34 | |
| 35 | fn encoderust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Encoder]encode().(&mut self, src: Selfrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][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/bytes/impl#[BytesCodec][Encoder]::Errorrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#Error#> { |
| 36 | dst.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&src); |
| 37 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder# for BytesCodecrust-analyzer cargo futures_codec 0.4.1 codec/bytes/BytesCodec# { |
| 42 | type Itemrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Decoder]Item# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#; |
| 43 | type Errorrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Decoder]Error# = Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#; |
| 44 | |
| 45 | fn decoderust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][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/bytes/impl#[BytesCodec][Decoder]::Itemrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Item#>, Selfrust-analyzer cargo futures_codec 0.4.1 codec/bytes/impl#[BytesCodec][Decoder]::Errorrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Error#> { |
| 46 | let len = src.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(); |
| 47 | if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 48 | 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().())) |
| 49 | } else { |
| 50 | 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#) |
| 51 | } |
| 52 | } |
| 53 | } |