#limit #input

reftake

A non-owning version of std::io::Take that wraps an existing reader by reference, allowing limited reads without transferring ownership

1 unstable release

Uses new Rust 2024

0.1.0 Jul 14, 2025

#2122 in Parser implementations

MIT/Apache

11KB
145 lines

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.


No runtime deps