Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- derived zerocopy traits for `SectionHandle` to allow storing handles in `ByteArea` sections
- added example demonstrating `ByteArea` with multiple typed sections, concurrent mutations, and freezing or persisting the area
- added example combining Python bindings with winnow parsing
- added Python example demonstrating structured parsing with winnow's `view`
Expand Down
5 changes: 5 additions & 0 deletions src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ where

/// Handle referencing a [`Section`] within a frozen [`ByteArea`].
#[derive(Clone, Copy, Debug)]
#[repr(C)]
#[cfg_attr(
feature = "zerocopy",
derive(zerocopy::FromBytes, zerocopy::KnownLayout, zerocopy::Immutable,)
)]
pub struct SectionHandle<T> {
/// Absolute byte offset from the start of the area.
pub offset: usize,
Expand Down
26 changes: 26 additions & 0 deletions tests/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,29 @@ fn handles_reconstruct_sections() {
assert_eq!(handle_a.view(&all).unwrap().as_ref(), &[1]);
assert_eq!(handle_b.view(&all).unwrap().as_ref(), &[2]);
}

#[test]
fn handles_can_be_stored_in_sections() {
use anybytes::area::SectionHandle;

let mut area = ByteArea::new().expect("area");
let mut sections = area.sections();

let mut value = sections.reserve::<u8>(1).expect("reserve value");
value.as_mut_slice()[0] = 3;
let handle_value = value.handle();
value.freeze().expect("freeze value");

let mut handles = sections
.reserve::<SectionHandle<u8>>(1)
.expect("reserve handles");
handles.as_mut_slice()[0] = handle_value;
let handle_handles = handles.handle();
handles.freeze().expect("freeze handles");

drop(sections);
let all = area.freeze().expect("freeze area");

let stored_handle = handle_handles.view(&all).expect("view handles").as_ref()[0];
assert_eq!(stored_handle.view(&all).unwrap().as_ref(), &[3]);
}