diff --git a/src/codec/framed.rs b/src/codec/framed.rs index 968a9689..74a204e7 100644 --- a/src/codec/framed.rs +++ b/src/codec/framed.rs @@ -85,7 +85,12 @@ impl Framed { pub fn from_parts(parts: FramedParts) -> Framed { Framed { inner: framed_read2_with_buffer( - framed_write2_with_buffer(Fuse(parts.io, parts.codec), parts.write_buf, parts.write_buf_lw, parts.write_buf_hw), + framed_write2_with_buffer( + Fuse(parts.io, parts.codec), + parts.write_buf, + parts.write_buf_lw, + parts.write_buf_hw, + ), parts.read_buf, ), } @@ -121,6 +126,11 @@ impl Framed { &mut self.inner.get_mut().get_mut().0 } + /// Check if write buffer is full. + pub fn is_full(&self) -> bool { + self.inner.get_ref().is_full() + } + /// Consumes the `Frame`, returning its underlying I/O stream. /// /// Note that care should be taken to not tamper with the underlying stream diff --git a/src/codec/framed_write.rs b/src/codec/framed_write.rs index 57789cc6..337bb4eb 100644 --- a/src/codec/framed_write.rs +++ b/src/codec/framed_write.rs @@ -72,6 +72,11 @@ impl FramedWrite { pub fn encoder_mut(&mut self) -> &mut E { &mut self.inner.inner.1 } + + /// Check if write buffer is full + pub fn is_full(&self) -> bool { + self.inner.is_full() + } } impl Sink for FramedWrite @@ -123,7 +128,11 @@ where // ===== impl FramedWrite2 ===== -pub fn framed_write2(inner: T, low_watermark: usize, high_watermark: usize) -> FramedWrite2 { +pub fn framed_write2( + inner: T, + low_watermark: usize, + high_watermark: usize, +) -> FramedWrite2 { FramedWrite2 { inner, low_watermark, @@ -132,7 +141,12 @@ pub fn framed_write2(inner: T, low_watermark: usize, high_watermark: usize) - } } -pub fn framed_write2_with_buffer(inner: T, mut buffer: BytesMut, low_watermark: usize, high_watermark: usize) -> FramedWrite2 { +pub fn framed_write2_with_buffer( + inner: T, + mut buffer: BytesMut, + low_watermark: usize, + high_watermark: usize, +) -> FramedWrite2 { if buffer.capacity() < high_watermark { let bytes_to_reserve = high_watermark - buffer.capacity(); buffer.reserve(bytes_to_reserve); @@ -155,12 +169,21 @@ impl FramedWrite2 { } pub fn into_parts(self) -> (T, BytesMut, usize, usize) { - (self.inner, self.buffer, self.low_watermark, self.high_watermark) + ( + self.inner, + self.buffer, + self.low_watermark, + self.high_watermark, + ) } pub fn get_mut(&mut self) -> &mut T { &mut self.inner } + + pub fn is_full(&self) -> bool { + self.buffer.len() >= self.high_watermark + } } impl Sink for FramedWrite2