Skip to content

Commit c8371d4

Browse files
authored
codec: add {FramedRead,FramedWrite}::into_parts() (#7566)
1 parent adc3e19 commit c8371d4

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

tokio-util/src/codec/framed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub struct FramedParts<T, U> {
377377

378378
/// This private field allows us to add additional fields in the future in a
379379
/// backwards compatible way.
380-
_priv: (),
380+
pub(crate) _priv: (),
381381
}
382382

383383
impl<T, U> FramedParts<T, U> {

tokio-util/src/codec/framed_read.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::fmt;
1111
use std::pin::Pin;
1212
use std::task::{Context, Poll};
1313

14+
use super::FramedParts;
15+
1416
pin_project! {
1517
/// A [`Stream`] of messages decoded from an [`AsyncRead`].
1618
///
@@ -153,6 +155,18 @@ impl<T, D> FramedRead<T, D> {
153155
pub fn read_buffer_mut(&mut self) -> &mut BytesMut {
154156
&mut self.inner.state.buffer
155157
}
158+
159+
/// Consumes the `FramedRead`, returning its underlying I/O stream, the buffer
160+
/// with unprocessed data, and the codec.
161+
pub fn into_parts(self) -> FramedParts<T, D> {
162+
FramedParts {
163+
io: self.inner.inner,
164+
codec: self.inner.codec,
165+
read_buf: self.inner.state.buffer,
166+
write_buf: BytesMut::new(),
167+
_priv: (),
168+
}
169+
}
156170
}
157171

158172
// This impl just defers to the underlying FramedImpl

tokio-util/src/codec/framed_write.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::io;
1212
use std::pin::Pin;
1313
use std::task::{Context, Poll};
1414

15+
use super::FramedParts;
16+
1517
pin_project! {
1618
/// A [`Sink`] of frames encoded to an `AsyncWrite`.
1719
///
@@ -159,6 +161,18 @@ impl<T, E> FramedWrite<T, E> {
159161
pub fn set_backpressure_boundary(&mut self, boundary: usize) {
160162
self.inner.state.backpressure_boundary = boundary;
161163
}
164+
165+
/// Consumes the `FramedWrite`, returning its underlying I/O stream, the buffer
166+
/// with unprocessed data, and the codec.
167+
pub fn into_parts(self) -> FramedParts<T, E> {
168+
FramedParts {
169+
io: self.inner.inner,
170+
codec: self.inner.codec,
171+
read_buf: BytesMut::new(),
172+
write_buf: self.inner.state.buffer,
173+
_priv: (),
174+
}
175+
}
162176
}
163177

164178
// This impl just defers to the underlying FramedImpl

0 commit comments

Comments
 (0)