11 releases (7 breaking)
new 0.8.0 | Nov 13, 2024 |
---|---|
0.7.2 | Oct 31, 2024 |
0.6.1 | Oct 10, 2024 |
0.5.0 | Oct 10, 2024 |
0.1.0 | Sep 28, 2024 |
#314 in Encoding
525 downloads per month
Used in 3 crates
185KB
2.5K
SLoC
dh
Data handling in Rust, made easy.
Features
- Read and write files in streams
- No unsafe code
- Support for a lot of data types (including variable length integers and custom length integers)
- Read and write u8 vectors
- std::io::Read and std::io::Write implementations for
Readable
andWritable
(happens automatically as they extend these traits) - Copying data from a
Readable
to aWritable
- Partial read & write access
Planned features
- Floating point number support
- Zero-cost cloning
- Zero-cost subarray clones
- Reading and writing of data that does not fill a whole byte
Installation
cargo add dh
Documentation
The documentation can be found on docs.rs.
Usage
Simple file reading
use dh::recommended::*;
fn main() {
let mut file = dh::file::open_r("data.txt").unwrap();
let size = file.size().unwrap();
assert_eq!(file.read_utf8(size).unwrap(), "Hello, world!\n");
}
Simple file writing
use dh::recommended::*;
fn main() {
let mut file = dh::file::open_w("data.txt").unwrap();
file.write_utf8_at("Hello, world!\n", 0).unwrap();
file.close().unwrap(); // optional, but recommended
}
Open a file in read/write mode
use dh::recommended::*;
fn main() {
let mut file = dh::file::open_rw("data.txt").unwrap();
file.write_utf8_at("Hello, world!\n", 0).unwrap();
file.rewind().unwrap();
let size = file.size().unwrap();
assert_eq!(file.read_utf8(size).unwrap(), "Hello, world!\n");
}
Read and write u8 vectors
Recommended: borrowing
Immutable borrowing
use dh::recommended::*;
fn main() {
let mut data = vec![31u8; 1];
let mut rw = dh::data::read_ref(&data);
assert_eq!(rw.read_u8().unwrap(), 31);
}
Mutable borrowing
use dh::recommended::*;
fn main() {
let mut data = vec![0u8; 1];
let mut rw = dh::data::rw_ref(&mut data);
rw.write_u8(31).unwrap();
rw.rewind().unwrap();
assert_eq!(rw.read_u8().unwrap(), 31);
}
Alternative: moving
use dh::recommended::*;
fn main() {
let data = vec![0u8; 1];
let mut rw = dh::data::rw(data);
rw.write_u8(31).unwrap();
rw.rewind().unwrap();
assert_eq!(rw.read_u8().unwrap(), 31);
let data = dh::data::close(rw).unwrap();
assert_eq!(data, vec![31]);
}
Limit readable space
use dh::recommended::*;
fn main() {
let mut file = dh::file::open_r("data.txt").unwrap();
let mut limited = file.limit(0, 5).unwrap();
assert_eq!(limited.read_utf8(5).unwrap(), "Hello");
}
License
This project is licensed under the MIT License - see the LICENSE file for details.