9 releases (5 breaking)
new 0.13.1 | Mar 28, 2025 |
---|---|
0.12.2 | Dec 6, 2024 |
0.12.1 | Nov 11, 2024 |
0.12.0 | Mar 10, 2021 |
0.7.0 | Sep 17, 2018 |
#337 in Rust patterns
11,245 downloads per month
Used in 14 crates
(2 directly)
13KB
106 lines
A simplified implementation of the bytes
crate, with different features, less safety.
The crate is currently minimalist rather than maximalist, and for example does not support
methods on BytesMut
that seem like they should be safe, because they are not yet needed.
For example, BytesMut
should be able to implement Send
, and BytesMut::extract_to
could
return a BytesMut
rather than a Bytes
.
Examples
use timely_bytes::arc::BytesMut;
let bytes = vec![0u8; 1024];
let mut shared1 = BytesMut::from(bytes);
let mut shared2 = shared1.extract_to(100);
let mut shared3 = shared1.extract_to(100);
let mut shared4 = shared2.extract_to(60);
assert_eq!(shared1.len(), 824);
assert_eq!(shared2.len(), 40);
assert_eq!(shared3.len(), 100);
assert_eq!(shared4.len(), 60);
for byte in shared1.iter_mut() { *byte = 1u8; }
// memory in slabs [4, 2, 3, 1]: merge back in arbitrary order.
shared2.try_merge(shared3).ok().expect("Failed to merge 2 and 3");
shared2.try_merge(shared1.freeze()).ok().expect("Failed to merge 23 and 1");
shared4.try_merge(shared2).ok().expect("Failed to merge 4 and 231");
assert_eq!(shared4.len(), 1024);