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
ReadorBufRead - ✅ Does not take ownership — wraps
&mut Rinstead of consumingR - ✅
ReadandBufReadimplementations 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:
[]
= "0.1"
Example
use ;
use RefTakeExt;
🔒 License
MIT OR Apache-2.0
🔧 Contributing
Feel free to open issues, suggest improvements, or submit pull requests.
📎 Related
std::io::Take— the owning versionBufReader— for buffered readers