Crate tokio_io_rewind

source ·
Expand description

This crate provides a Rewind struct that wraps any type that implements the AsyncRead and/or AsyncWrite traits, allowing for the prepending of bytes before the actual read happens. This is particularly useful in scenarios where you’ve read bytes from a stream but need to “put them back” to be read again, effectively allowing you to rewind the stream.

§Examples

Basic usage of Rewind with an AsyncRead implementor (tokio::io::Cursor):

use bytes::Bytes;
use tokio::io::AsyncReadExt;
use tokio_io_rewind::Rewind;
use std::io::Cursor;

#[tokio::main]
async fn main() {
    // Create a new Rewind instance with a Cursor wrapped inside.
    let mut rw = Rewind::new(Cursor::new(b"world".to_vec()));

    // Prepend "hello " to the stream.
    rw.rewind(Bytes::from_static(b"hello "));

    // Read all bytes from the rewinded stream.
    let mut buf = Vec::new();
    rw.read_to_end(&mut buf).await.unwrap();

    assert_eq!(buf, b"hello world");
}

This module also supports asynchronous write operations if the underlying type implements AsyncWrite.

§Features

  • Rewind::new(inner): Create a new Rewind instance with no pre-buffered bytes.
  • Rewind::new_buffered(inner, pre): Create a new Rewind instance with an initial buffer of bytes to prepend.
  • Rewind::rewind(pre): Prepend bytes to the current buffer. If there’s already a buffer, the new bytes are prepended before the existing ones.
  • Rewind::into_inner(): Consumes the Rewind, returning the inner type and any un-read pre-buffered bytes.

Rewind can be especially useful in protocols or situations where a piece of data is read to determine what comes next, but where that data also needs to be part of the eventual input stream.

Structs§

  • Wraps an AsyncRead and/or AsyncWrite implementor, allowing bytes to be prepended to the stream.