SyncStream

Struct SyncStream 

pub struct SyncStream<S> { /* private fields */ }
Available on crate features 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_capacity when 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() when Read::read() returns WouldBlock
  • Call flush_write_buf() when Write::write() returns WouldBlock

§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§

§

impl<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

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.

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.

pub fn get_ref(&self) -> &S

Returns a reference to the underlying stream.

pub fn get_mut(&mut self) -> &mut S

Returns a mutable reference to the underlying stream.

pub fn into_inner(self) -> S

Consumes the SyncStream, returning the underlying stream.

pub fn is_eof(&self) -> bool

Returns true if the stream has reached EOF.

pub fn read_buf_uninit( &mut self, buf: &mut [MaybeUninit<u8>], ) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer.

§

impl<S> SyncStream<S>
where S: AsyncRead,

pub async fn fill_read_buf(&mut self) -> Result<usize, Error>

Fills the read buffer by reading from the underlying async stream.

This method:

  1. Compacts the buffer if there’s unconsumed data
  2. Ensures there’s space for at least base_capacity more bytes
  3. Reads data from the underlying stream
  4. 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
§

impl<S> SyncStream<S>
where S: AsyncWrite,

pub async fn flush_write_buf(&mut self) -> Result<usize, Error>

Flushes the write buffer to the underlying async stream.

This method:

  1. Writes all buffered data to the underlying stream
  2. Calls flush() on the underlying stream
  3. 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§

§

impl<S> BufRead for SyncStream<S>

§

fn fill_buf(&mut self) -> Result<&[u8], Error>

Returns the contents of the internal buffer, filling it with more data, via Read methods, if empty. Read more
§

fn consume(&mut self, amt: usize)

Marks the given 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 more
Source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Checks if there is any data left to be read. Read more
1.0.0 · Source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
1.83.0 · Source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

Skips all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · Source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · Source§

fn lines(self) -> Lines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader. Read more
§

impl<S> Debug for SyncStream<S>
where S: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<S> Read for SyncStream<S>

§

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.

§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
§

impl<S> Write for SyncStream<S>

§

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.

§

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.

1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read for T
where T: Read,

§

type Error = Error

The error type
§

fn read_exact(&mut self, data: &mut [u8]) -> Result<(), <T as Read>::Error>

Reads exactly data.len() bytes or fails
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Write for T
where T: Write,

§

type Error = Error

The error type
§

fn write_all(&mut self, data: &[u8]) -> Result<(), <T as Write>::Error>

Writes all bytes from data or fails
§

fn flush(&mut self) -> Result<(), <T as Write>::Error>

Flushes all output