2626//!
2727//! let mut a = sections.reserve::<u8>(1).unwrap();
2828//! a.as_mut_slice()[0] = 1;
29+ //! let handle_a = a.handle();
2930//!
3031//! let mut b = sections.reserve::<u32>(1).unwrap();
3132//! b.as_mut_slice()[0] = 2;
33+ //! let handle_b = b.handle();
3234//!
3335//! let bytes_a = a.freeze().unwrap();
3436//! let bytes_b = b.freeze().unwrap();
3537//! drop(sections);
3638//! let all = area.freeze().unwrap();
3739//!
38- //! assert_eq!(bytes_a .as_ref(), &[1]);
39- //! assert_eq!(bytes_b .as_ref(), &2u32.to_ne_bytes());
40+ //! assert_eq!(handle_a.bytes(&all) .as_ref(), &[1]);
41+ //! assert_eq!(handle_b.bytes(&all) .as_ref(), &2u32.to_ne_bytes());
4042//!
4143//! let mut expected = Vec::new();
4244//! expected.extend_from_slice(&[1]);
@@ -58,6 +60,9 @@ use crate::Bytes;
5860#[ cfg( feature = "zerocopy" ) ]
5961use zerocopy:: { FromBytes , Immutable } ;
6062
63+ #[ cfg( feature = "zerocopy" ) ]
64+ use crate :: view:: { View , ViewError } ;
65+
6166/// Alignment helper.
6267fn align_up ( val : usize , align : usize ) -> usize {
6368 ( val + align - 1 ) & !( align - 1 )
@@ -135,6 +140,7 @@ impl<'area> SectionWriter<'area> {
135140 Ok ( Section {
136141 mmap,
137142 offset,
143+ start,
138144 elems,
139145 _marker : PhantomData ,
140146 } )
@@ -148,6 +154,8 @@ pub struct Section<'arena, T> {
148154 mmap : memmap2:: MmapMut ,
149155 /// Offset from the beginning of `mmap` to the start of the buffer.
150156 offset : usize ,
157+ /// Absolute start offset within the [`ByteArea`] in bytes.
158+ start : usize ,
151159 /// Number of elements in the buffer.
152160 elems : usize ,
153161 /// Marker tying the section to the area and element type.
@@ -176,6 +184,15 @@ where
176184 let map = self . mmap . make_read_only ( ) ?;
177185 Ok ( Bytes :: from_source ( map) . slice ( offset..offset + len_bytes) )
178186 }
187+
188+ /// Return a handle that can reconstruct this section from a frozen [`ByteArea`].
189+ pub fn handle ( & self ) -> SectionHandle < T > {
190+ SectionHandle {
191+ offset : self . start ,
192+ len : self . elems * core:: mem:: size_of :: < T > ( ) ,
193+ _type : PhantomData ,
194+ }
195+ }
179196}
180197
181198impl < ' arena , T > core:: ops:: Deref for Section < ' arena , T >
@@ -221,3 +238,29 @@ where
221238 self
222239 }
223240}
241+
242+ /// Handle referencing a [`Section`] within a frozen [`ByteArea`].
243+ #[ derive( Clone , Copy , Debug ) ]
244+ pub struct SectionHandle < T > {
245+ /// Absolute byte offset from the start of the area.
246+ pub offset : usize ,
247+ /// Length of the section in bytes.
248+ pub len : usize ,
249+ /// Marker for the element type stored in the section.
250+ _type : PhantomData < T > ,
251+ }
252+
253+ impl < T > SectionHandle < T >
254+ where
255+ T : FromBytes + Immutable ,
256+ {
257+ /// Extract the raw bytes for this section from `area`.
258+ pub fn bytes < ' a > ( & self , area : & ' a Bytes ) -> Bytes {
259+ area. slice ( self . offset ..self . offset + self . len )
260+ }
261+
262+ /// Interpret the section as a typed [`View`].
263+ pub fn view ( & self , area : & Bytes ) -> Result < View < [ T ] > , ViewError > {
264+ self . bytes ( area) . view :: < [ T ] > ( )
265+ }
266+ }
0 commit comments