#tokio #seek #async #reed


Offers a wrapper to tokio::io::Take that offers a tokio::io::AsyncSeek implementation

1 unstable release

0.1.0 Jun 8, 2022

#866 in Filesystem

Download history 8/week @ 2023-02-04 16/week @ 2023-02-11 10/week @ 2023-02-18 6/week @ 2023-02-25 6/week @ 2023-03-04 5/week @ 2023-03-11 6/week @ 2023-03-18 47/week @ 2023-03-25 8/week @ 2023-04-01 27/week @ 2023-04-08 12/week @ 2023-04-15 20/week @ 2023-04-22 3/week @ 2023-04-29 13/week @ 2023-05-06 27/week @ 2023-05-13 15/week @ 2023-05-20

62 downloads per month


224 lines


This creates offers a wraparound to tokio::io::Take that offers a tokio::io::AsyncSeek implementation (as long as the inner handle used implements it as well). The idea is to use the Take implementation as a upper bound and not allow reads beyond that point, and when seek operations are called, the underling limit of how many bytes can still be read is properly updated.

The provided AsyncTakeSeekExt trait offers a take_with_seek method for creating the wrapper over any type that implements AsyncRead + AsyncSeek + Unpin.


The current implementation has to do internally two seeks for every seek called. The first seek is done with SeekFrom::current(0) and it's result is used to determine how much the stream has moved so the Take's inner limit can be properly updated.

Usage example:

On this example we use the take_with_seek to guarantee that the string won't be read beyond the desired limit, while still being able to seek over the reader even after the bound is created.

async fn basic_seek_and_reed() {
    let file = tempfile::NamedTempFile::new().unwrap();
    //                                        21v               38v
    fs::write(file.path(), "this will be skipped|this will be read|this will be guarded")

    let mut handle = TakeSeek::new(take::take(fs::File::open(file.path()).await.unwrap(), 38));

    let mut data = String::default();
    handle.read_to_string(&mut data).await.unwrap();
    assert_eq!(data, "this will be read");


~94K SLoC