From c8371d45bc0529f88dd168e83c1e8b14f9b9def6 Mon Sep 17 00:00:00 2001 From: Varun Doshi Date: Wed, 3 Sep 2025 17:08:51 +0530 Subject: [PATCH] codec: add `{FramedRead,FramedWrite}::into_parts()` (#7566) --- tokio-util/src/codec/framed.rs | 2 +- tokio-util/src/codec/framed_read.rs | 14 ++++++++++++++ tokio-util/src/codec/framed_write.rs | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tokio-util/src/codec/framed.rs b/tokio-util/src/codec/framed.rs index da3fec88e..ec7aa172f 100644 --- a/tokio-util/src/codec/framed.rs +++ b/tokio-util/src/codec/framed.rs @@ -377,7 +377,7 @@ pub struct FramedParts { /// This private field allows us to add additional fields in the future in a /// backwards compatible way. - _priv: (), + pub(crate) _priv: (), } impl FramedParts { diff --git a/tokio-util/src/codec/framed_read.rs b/tokio-util/src/codec/framed_read.rs index 3ede5876b..0f2e16dc3 100644 --- a/tokio-util/src/codec/framed_read.rs +++ b/tokio-util/src/codec/framed_read.rs @@ -11,6 +11,8 @@ use std::fmt; use std::pin::Pin; use std::task::{Context, Poll}; +use super::FramedParts; + pin_project! { /// A [`Stream`] of messages decoded from an [`AsyncRead`]. /// @@ -153,6 +155,18 @@ impl FramedRead { pub fn read_buffer_mut(&mut self) -> &mut BytesMut { &mut self.inner.state.buffer } + + /// Consumes the `FramedRead`, returning its underlying I/O stream, the buffer + /// with unprocessed data, and the codec. + pub fn into_parts(self) -> FramedParts { + FramedParts { + io: self.inner.inner, + codec: self.inner.codec, + read_buf: self.inner.state.buffer, + write_buf: BytesMut::new(), + _priv: (), + } + } } // This impl just defers to the underlying FramedImpl diff --git a/tokio-util/src/codec/framed_write.rs b/tokio-util/src/codec/framed_write.rs index 541aae9bf..efc369ebf 100644 --- a/tokio-util/src/codec/framed_write.rs +++ b/tokio-util/src/codec/framed_write.rs @@ -12,6 +12,8 @@ use std::io; use std::pin::Pin; use std::task::{Context, Poll}; +use super::FramedParts; + pin_project! { /// A [`Sink`] of frames encoded to an `AsyncWrite`. /// @@ -159,6 +161,18 @@ impl FramedWrite { pub fn set_backpressure_boundary(&mut self, boundary: usize) { self.inner.state.backpressure_boundary = boundary; } + + /// Consumes the `FramedWrite`, returning its underlying I/O stream, the buffer + /// with unprocessed data, and the codec. + pub fn into_parts(self) -> FramedParts { + FramedParts { + io: self.inner.inner, + codec: self.inner.codec, + read_buf: BytesMut::new(), + write_buf: self.inner.state.buffer, + _priv: (), + } + } } // This impl just defers to the underlying FramedImpl