1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Types associated within reading and binding to a [`Buf`].
//!
//! Buffers are slices of bytes without any inherent alignment. They allow use
//! to safely convert offset-like types to references for types which implements
//! [`ZeroCopy`].
//!
//! # Extension traits
//!
//! This module provides a couple of extension traits which can be used to alter
//! how types interact with buffers.
//!
//! The following is a more compact [`Slice<u8>`] which only occupies 4 bytes,
//! but restricts the offset and length.
//!
//! [`Slice<u8>`]: crate::pointer::Slice
//!
//! ```
//! use musli_zerocopy::{Error, ZeroCopy};
//! use musli_zerocopy::buf::{AlignedBuf, Buf, Load, LoadMut, Visit};
//! use musli_zerocopy::pointer::{Slice, Ref};
//!
//! #[derive(ZeroCopy)]
//! #[repr(C, packed)]
//! struct CompactSlice {
//! // 3 bytes of offset.
//! offset: [u8; 3],
//! // one byte of length.
//! len: u8,
//! }
//!
//! impl CompactSlice {
//! /// Construct a new compact slice, panic if offset and length
//! /// overflow 3 and 1 byte integers respectively.
//! pub fn new(offset: usize, len: usize) -> Self {
//! assert!(offset <= 0xffffffusize && len <= 0xff);
//! let [a, b, c, ..] = offset.to_le_bytes();
//!
//! Self {
//! offset: [a, b, c],
//! len: len as u8,
//! }
//! }
//!
//! /// Get the length of the compact slice.
//! pub fn len(&self) -> usize {
//! self.len as usize
//! }
//!
//! fn to_slice(&self) -> Slice<u8> {
//! let [a, b, c] = self.offset;
//! let offset = u32::from_le_bytes([a, b, c, 0]);
//! Slice::new(offset as usize, self.len as usize)
//! }
//! }
//!
//! impl Load for CompactSlice {
//! type Target = [u8];
//!
//! fn load<'buf>(&self, buf: &'buf Buf) -> Result<&'buf Self::Target, Error> {
//! buf.load(self.to_slice())
//! }
//! }
//!
//! impl LoadMut for CompactSlice {
//! fn load_mut<'buf>(&self, buf: &'buf mut Buf) -> Result<&'buf mut Self::Target, Error> {
//! buf.load_mut(self.to_slice())
//! }
//! }
//!
//! let mut buf1 = AlignedBuf::new();
//! let slice = buf1.store_slice(&[1u8, 2, 3, 4]);
//! let slice_ref: Ref<Slice<u8>> = buf1.store(&slice);
//! assert_eq!(buf1.len(), 12);
//!
//! let buf1 = buf1.as_aligned();
//! let slice = buf1.load(slice_ref)?;
//! assert_eq!(slice.len(), 4);
//! assert_eq!(buf1.load(slice)?, &[1, 2, 3, 4]);
//!
//! let mut buf2 = AlignedBuf::new();
//! let slice = buf2.store_slice(&[1u8, 2, 3, 4]);
//! let slice = CompactSlice::new(slice.offset(), slice.len());
//! let slice_ref: Ref<CompactSlice> = buf2.store(&slice);
//! assert_eq!(buf2.len(), 8);
//!
//! let buf2 = buf2.as_aligned();
//! let slice = buf2.load(slice_ref)?;
//! assert_eq!(slice.len(), 4);
//! assert_eq!(buf2.load(slice)?, &[1, 2, 3, 4]);
//! # Ok::<_, musli_zerocopy::Error>(())
//! ```
pub use Buf;
pub use Bindable;
pub use ;
pub use Visit;
pub use Visit;
pub
pub use Validator;
pub use Padder;
pub use BufMut;
pub use RawBufMut;
pub use AlignedBuf;
use ;
use Cow;
use crateError;
use crateZeroCopy;
/// The type used to calculate default alignment for [`AlignedBuf`].
pub type DefaultAlignment = usize;
/// Return the max capacity of this vector. This depends on the requested
/// alignment.
///
/// This follows how it's defined by `max_size_for_align` in [`Layout`].
///
/// [`Layout`]: core::alloc::Layout
/// Construct a buffer with an alignment matching that of `T` which is either
/// wrapped in a [`Buf`] if it is already correctly aligned, or inside of an
/// allocated [`AlignedBuf`].
///
/// # Examples
///
/// ```no_run
/// use std::fs::read;
/// use musli_zerocopy::ZeroCopy;
/// use musli_zerocopy::buf;
/// use musli_zerocopy::pointer::{Ref, Unsized};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Person {
/// name: Unsized<str>,
/// age: u32,
/// }
///
/// let bytes = read("person.bin")?;
/// let buf = buf::aligned_buf::<u128>(&bytes);
///
/// let s = buf.load(Ref::<Person>::zero())?;
/// # Ok::<_, anyhow::Error>(())
/// ```
/// Construct a buffer with a specific alignment which is either wrapped in a
/// [`Buf`] if it is already correctly aligned, or inside of an allocated
/// [`AlignedBuf`].
///
/// # Panics
///
/// Panics if `align` is not a power of two or if the size of the buffer is
/// larger than [`max_capacity_for_align(align)`].
///
/// # Examples
///
/// ```no_run
/// use std::fs::read;
/// use musli_zerocopy::ZeroCopy;
/// use musli_zerocopy::buf;
/// use musli_zerocopy::pointer::{Ref, Unsized};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Person {
/// name: Unsized<str>,
/// age: u32,
/// }
///
/// let bytes = read("person.bin")?;
/// let buf = buf::aligned_buf_with(&bytes, 16);
///
/// let s = buf.load(Ref::<Person>::zero())?;
/// # Ok::<_, anyhow::Error>(())
/// ```
pub
pub unsafe
/// Calculate padding with the assumption that alignment is a power of two.
pub