From 9a3321b15377df9371f414f029897583cc107808 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 13 Nov 2018 21:33:10 -0800 Subject: [PATCH] allow to check if Framed's write buffer is full --- src/codec/framed.rs | 12 +++++++++++- src/codec/framed_write.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) 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