From 4a12163d7c434c0267fd76d7ec27239edceefec4 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 3 Jan 2022 22:21:43 +0100 Subject: [PATCH] util: add mutable reference getters for codecs to pinned `Framed` (#4372) --- tokio-util/src/codec/framed.rs | 9 +++++++++ tokio-util/src/codec/framed_read.rs | 5 +++++ tokio-util/src/codec/framed_write.rs | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/tokio-util/src/codec/framed.rs b/tokio-util/src/codec/framed.rs index aff577f22..3f7e4207d 100644 --- a/tokio-util/src/codec/framed.rs +++ b/tokio-util/src/codec/framed.rs @@ -204,6 +204,15 @@ impl Framed { &mut self.inner.codec } + /// Returns a mutable reference to the underlying codec wrapped by + /// `Framed`. + /// + /// Note that care should be taken to not tamper with the underlying codec + /// as it may corrupt the stream of frames otherwise being worked with. + pub fn codec_pin_mut(self: Pin<&mut Self>) -> &mut U { + self.project().inner.project().codec + } + /// Returns a reference to the read buffer. pub fn read_buffer(&self) -> &BytesMut { &self.inner.state.read.buffer diff --git a/tokio-util/src/codec/framed_read.rs b/tokio-util/src/codec/framed_read.rs index 502a073d0..d6e34dbaf 100644 --- a/tokio-util/src/codec/framed_read.rs +++ b/tokio-util/src/codec/framed_read.rs @@ -108,6 +108,11 @@ impl FramedRead { &mut self.inner.codec } + /// Returns a mutable reference to the underlying decoder. + pub fn decoder_pin_mut(self: Pin<&mut Self>) -> &mut D { + self.project().inner.project().codec + } + /// Returns a reference to the read buffer. pub fn read_buffer(&self) -> &BytesMut { &self.inner.state.buffer diff --git a/tokio-util/src/codec/framed_write.rs b/tokio-util/src/codec/framed_write.rs index d2f6cb2d5..b827d9736 100644 --- a/tokio-util/src/codec/framed_write.rs +++ b/tokio-util/src/codec/framed_write.rs @@ -88,6 +88,11 @@ impl FramedWrite { &mut self.inner.codec } + /// Returns a mutable reference to the underlying encoder. + pub fn encoder_pin_mut(self: Pin<&mut Self>) -> &mut E { + self.project().inner.project().codec + } + /// Returns a reference to the write buffer. pub fn write_buffer(&self) -> &BytesMut { &self.inner.state.buffer