Skip to content

Commit 14b293b

Browse files
Merge pull request #56 from triblespace/codex/add-example-for-bytearea-usage
Add ByteArea example and documentation
2 parents 845ee41 + 7619864 commit 14b293b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- added example demonstrating `ByteArea` with multiple typed sections, concurrent mutations, and freezing or persisting the area
45
- added example combining Python bindings with winnow parsing
56
- added Python example demonstrating structured parsing with winnow's `view`
67
- added `ByteSource` support for `VecDeque<T>` when `zerocopy` is enabled and kept the deque as owner

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ The area only aligns allocations to the element type and may share pages
125125
between adjacent sections to minimize wasted space. Multiple sections may be
126126
active simultaneously; their byte ranges do not overlap.
127127

128+
See [`examples/byte_area.rs`](examples/byte_area.rs) for a complete example
129+
that reserves different typed sections, mutates them simultaneously, and then
130+
either freezes the area into `Bytes` or persists it to disk.
131+
128132
## Features
129133

130134
By default the crate enables the `mmap` and `zerocopy` features.
@@ -149,6 +153,7 @@ needs these libraries installed; otherwise disable the feature during testing.
149153
- [`examples/from_python.rs`](examples/from_python.rs) – wrap a Python `bytes` object into `Bytes`
150154
- [`examples/python_winnow.rs`](examples/python_winnow.rs) – parse Python bytes with winnow
151155
- [`examples/python_winnow_view.rs`](examples/python_winnow_view.rs) – parse structured data from Python bytes using winnow's `view`
156+
- [`examples/byte_area.rs`](examples/byte_area.rs) – reserve and mutate multiple typed sections, then either freeze the area into `Bytes` or persist it to disk
152157

153158
## Comparison
154159

examples/byte_area.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use anybytes::{area::ByteArea, Bytes};
2+
use tempfile::tempdir;
3+
4+
fn main() -> std::io::Result<()> {
5+
let mut area = ByteArea::new()?;
6+
let mut sections = area.sections();
7+
8+
// Reserve two sections at once and mutate them independently.
9+
let mut raw = sections.reserve::<u8>(4)?;
10+
let mut nums = sections.reserve::<u32>(2)?;
11+
12+
raw.as_mut_slice().copy_from_slice(b"test");
13+
nums.as_mut_slice().copy_from_slice(&[1, 2]);
14+
15+
// Freeze the sections into immutable `Bytes`.
16+
let frozen_raw: Bytes = raw.freeze()?;
17+
let frozen_nums: Bytes = nums.freeze()?;
18+
assert_eq!(frozen_raw.as_ref(), b"test");
19+
assert_eq!(frozen_nums.view::<[u32]>().unwrap().as_ref(), &[1, 2]);
20+
21+
drop(sections);
22+
23+
// Decide whether to keep the area in memory or persist it to disk.
24+
let memory_or_file = true;
25+
if memory_or_file {
26+
// Freeze the whole area into immutable `Bytes`.
27+
let all: Bytes = area.freeze()?;
28+
assert_eq!(&all[..4], b"test");
29+
assert_eq!(all.slice(4..).view::<[u32]>().unwrap().as_ref(), &[1, 2]);
30+
} else {
31+
// Persist the temporary file.
32+
let dir = tempdir()?;
33+
let path = dir.path().join("area.bin");
34+
area.persist(&path)?;
35+
assert!(path.exists());
36+
}
37+
38+
Ok(())
39+
}

0 commit comments

Comments
 (0)