Skip to content

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.

License

Notifications You must be signed in to change notification settings

gzsombor/reftake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages