reftake 0.1.0

A non-owning version of `std::io::Take` that wraps an existing reader by reference, allowing limited reads without transferring ownership.
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented2 out of 7 items with examples
  • Size
  • Source code size: 21.91 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.63 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • gzsombor/reftake
    1 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • gzsombor

RefTake

RefTake is a non-owning alternative to [std::io::Take] that allows limiting how many bytes can be read from a referenced reader without taking ownership of it.

It is useful in scenarios where:

  • You need to apply a byte limit to an existing borrowed reader.
  • You’re implementing stream parsers or protocols where ownership cannot be moved.
  • You want to reuse a single reader across multiple limited reads.

✨ Features

  • ✅ Works with any type implementing Read or BufRead
  • ✅ Does not take ownership — wraps &mut R instead of consuming R
  • Read and BufRead implementations respect the byte limit
  • ✅ Supports dynamic limit adjustment via .set_limit()
  • ✅ Extension trait to simplify usage: .take_ref(limit)

📦 Usage

Add to your project

Add it to your Cargo.toml:

[dependencies]
reftake = "0.1"

Example

use std::io::{Cursor, Read};
use ref_take::RefTakeExt;

fn main() {
    let mut reader = Cursor::new(b"hello world");
    let mut limited = (&mut reader).by_ref_take(5);

    let mut buffer = String::new();
    limited.read_to_string(&mut buffer).unwrap();
    assert_eq!(buffer, "hello");
}

🔒 License

MIT OR Apache-2.0


🔧 Contributing

Feel free to open issues, suggest improvements, or submit pull requests.


📎 Related