pub struct SyncStream<S> { /* private fields */ }io and compat only.Expand description
A growable buffered stream adapter that bridges async I/O with sync traits.
§Buffer Growth Strategy
- Read buffer: Grows as needed to accommodate incoming data, up to
max_buffer_size - Write buffer: Grows as needed for outgoing data, up to
max_buffer_size - Both buffers shrink back to
base_capacitywhen fully consumed and capacity exceeds 4x base
§Usage Pattern
The sync Read and Write implementations will return WouldBlock errors
when buffers need servicing via the async methods:
- Call
fill_read_buf()whenRead::read()returnsWouldBlock - Call
flush_write_buf()whenWrite::write()returnsWouldBlock
§Note on flush()
The Write::flush() method intentionally returns Ok(()) without checking
if there’s buffered data. This is for compatibility with libraries like
tungstenite that call flush() after every write. Actual flushing happens
via the async flush_write_buf() method.
Implementations§
Source§impl<S> SyncStream<S>
impl<S> SyncStream<S>
Sourcepub fn new(stream: S) -> SyncStream<S> ⓘ
pub fn new(stream: S) -> SyncStream<S> ⓘ
Creates a new SyncStream with default buffer sizes.
- Base capacity: 8KiB
- Max buffer size: 64MiB
Sourcepub fn with_capacity(base_capacity: usize, stream: S) -> SyncStream<S> ⓘ
pub fn with_capacity(base_capacity: usize, stream: S) -> SyncStream<S> ⓘ
Creates a new SyncStream with a custom base capacity.
The maximum buffer size defaults to 64MiB.
Sourcepub fn with_limits(
base_capacity: usize,
max_buffer_size: usize,
stream: S,
) -> SyncStream<S> ⓘ
pub fn with_limits( base_capacity: usize, max_buffer_size: usize, stream: S, ) -> SyncStream<S> ⓘ
Creates a new SyncStream with custom base capacity and maximum
buffer size.
Sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Consumes the SyncStream, returning the underlying stream.
Sourcepub fn read_buf_uninit(
&mut self,
buf: &mut [MaybeUninit<u8>],
) -> Result<usize, Error>
pub fn read_buf_uninit( &mut self, buf: &mut [MaybeUninit<u8>], ) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer.
Source§impl<S> SyncStream<S>where
S: AsyncRead,
impl<S> SyncStream<S>where
S: AsyncRead,
Sourcepub async fn fill_read_buf(&mut self) -> Result<usize, Error>
pub async fn fill_read_buf(&mut self) -> Result<usize, Error>
Fills the read buffer by reading from the underlying async stream.
This method:
- Compacts the buffer if there’s unconsumed data
- Ensures there’s space for at least
base_capacitymore bytes - Reads data from the underlying stream
- Returns the number of bytes read (0 indicates EOF)
§Errors
Returns an error if:
- The read buffer has reached
max_buffer_size - The underlying stream returns an error
Source§impl<S> SyncStream<S>where
S: AsyncWrite,
impl<S> SyncStream<S>where
S: AsyncWrite,
Sourcepub async fn flush_write_buf(&mut self) -> Result<usize, Error>
pub async fn flush_write_buf(&mut self) -> Result<usize, Error>
Flushes the write buffer to the underlying async stream.
This method:
- Writes all buffered data to the underlying stream
- Calls
flush()on the underlying stream - Returns the total number of bytes flushed
On error, any unwritten data remains in the buffer and can be retried.
§Errors
Returns an error if the underlying stream returns an error. In this case, the buffer retains any data that wasn’t successfully written.
Trait Implementations§
Source§impl<S> BufRead for SyncStream<S>
impl<S> BufRead for SyncStream<S>
Source§fn fill_buf(&mut self) -> Result<&[u8], Error>
fn fill_buf(&mut self) -> Result<&[u8], Error>
Read methods, if empty. Read moreSource§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amount of additional bytes from the internal buffer as having been read.
Subsequent calls to read only return bytes that have not been marked as read. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left)read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA byte) is reached, and append
them to the provided String buffer. Read moreSource§impl<S> Debug for SyncStream<S>where
S: Debug,
impl<S> Debug for SyncStream<S>where
S: Debug,
Source§impl<S> Read for SyncStream<S>
impl<S> Read for SyncStream<S>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Reads data from the internal buffer.
Returns WouldBlock if the buffer is empty and not at EOF,
indicating that fill_read_buf() should be called.
Source§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl<S> Write for SyncStream<S>
impl<S> Write for SyncStream<S>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Writes data to the internal buffer.
Returns WouldBlock if the buffer needs flushing or has reached max
capacity. In the latter case, it may write partial data before
returning WouldBlock.
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Returns Ok(()) without checking for buffered data.
Important: This does NOT actually flush data to the underlying
stream. This behavior is intentional for compatibility with
libraries like tungstenite that call flush() after every write
operation. The actual async flush happens when flush_write_buf()
is called.
This prevents spurious errors in sync code that expects flush() to
succeed after successfully buffering data.
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)Auto Trait Implementations§
impl<S> Freeze for SyncStream<S>where
S: Freeze,
impl<S> RefUnwindSafe for SyncStream<S>where
S: RefUnwindSafe,
impl<S> Send for SyncStream<S>where
S: Send,
impl<S> Sync for SyncStream<S>where
S: Sync,
impl<S> Unpin for SyncStream<S>where
S: Unpin,
impl<S> UnwindSafe for SyncStream<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more