From 0b05ef638d0aa1821e6658941541bf5727e7d5e2 Mon Sep 17 00:00:00 2001 From: Sunyeop Lee Date: Tue, 8 Feb 2022 23:11:24 +0900 Subject: [PATCH] codec: implement Encoder for BytesCodec (#4465) --- tokio-util/src/codec/bytes_codec.rs | 10 ++++++++++ tokio-util/tests/codecs.rs | 3 +++ 2 files changed, 13 insertions(+) diff --git a/tokio-util/src/codec/bytes_codec.rs b/tokio-util/src/codec/bytes_codec.rs index 275031c06..ceab228b9 100644 --- a/tokio-util/src/codec/bytes_codec.rs +++ b/tokio-util/src/codec/bytes_codec.rs @@ -74,3 +74,13 @@ impl Encoder for BytesCodec { Ok(()) } } + +impl Encoder for BytesCodec { + type Error = io::Error; + + fn encode(&mut self, data: BytesMut, buf: &mut BytesMut) -> Result<(), io::Error> { + buf.reserve(data.len()); + buf.put(data); + Ok(()) + } +} diff --git a/tokio-util/tests/codecs.rs b/tokio-util/tests/codecs.rs index aae612938..f9a780140 100644 --- a/tokio-util/tests/codecs.rs +++ b/tokio-util/tests/codecs.rs @@ -38,6 +38,9 @@ fn bytes_encoder() { codec .encode(Bytes::from_static(&[0; INITIAL_CAPACITY + 1]), &mut buf) .unwrap(); + codec + .encode(BytesMut::from(&b"hello"[..]), &mut buf) + .unwrap(); } #[test]