Skip to content

Commit

Permalink
codec: auto implement Encoder/Decoder for &mut T if T is Encoder/Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
suikammd committed Jun 28, 2023
1 parent 17a4891 commit a3c398e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tokio-util/src/codec/decoder.rs
Expand Up @@ -182,3 +182,13 @@ pub trait Decoder {
Framed::new(io, self)
}
}

impl<T: Decoder> Decoder for &mut T {
type Item = T::Item;

type Error = T::Error;

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
(**self).decode(src)
}
}
8 changes: 8 additions & 0 deletions tokio-util/src/codec/encoder.rs
Expand Up @@ -23,3 +23,11 @@ pub trait Encoder<Item> {
/// [`FramedWrite`]: crate::codec::FramedWrite
fn encode(&mut self, item: Item, dst: &mut BytesMut) -> Result<(), Self::Error>;
}

impl<I, T: Encoder<I>> Encoder<I> for &mut T {
type Error = T::Error;

fn encode(&mut self, item: I, dst: &mut BytesMut) -> Result<(), Self::Error> {
(**self).encode(item, dst)
}
}

0 comments on commit a3c398e

Please sign in to comment.