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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use core::{mem, ops, ptr, slice};
use super::Pod;

/// Read and write data from and to the underlying byte buffer.
///
/// Construct the data view through [`Pod::as_data_view`] or [`Pod::as_data_view_mut`].
///
/// # Operations
///
/// Each set of operations may support a try, panicking and unchecked variations, see below for more information.
///
/// * `copy(offset)`
///
///   Copies a (potentially unaligned) value out of the view.
///
/// * `copy_into(offset, dest)`
///
///    Copies a (potentially unaligned) value out of the view into the dest argument.
///
/// * `read(offset)`
///
///   Returns a reference to the data given the offset.
///   Errors if the final pointer is misaligned for the given type.
///
/// * `read_mut(offset)`
///
///   Returns a mutable reference to the data given the offset.
///   Errors if the final pointer is misaligned for the given type.
///
/// * `slice(offset, len)`
///
///   Returns a slice to the data given the offset and len.
///   Errors if the final pointer is misaligned for the given type.
///
/// * `slice_mut(offset, len)`
///
///   Returns a mutable slice to the data given the offset.
///   Errors if the final pointer is misaligned for the given type.
///
/// * `write(offset, value)`
///
///   Writes a value to the view at the given offset.
///
/// # Panics
///
/// *Panicking* methods have no prefix or suffix. They invoke the *Try* methods and panic if they return `None`.
///
/// When calling *Panicking* variation with an offset that ends up out of bounds or if the final pointer is misaligned
/// for the given type the method panics with the message `"invalid offset"`.
///
/// The relevant methods are annotated with `#[track_caller]` providing a useful location where the error happened.
///
/// # Safety
///
/// The *Unchecked* methods have the `_unchecked` suffix and simply assume the offset is correct.
/// This is *Undefined Behavior* when it results in an out of bounds read or write or if a misaligned reference is produced.
///
/// If the *Try* variation would have returned `None` then the *Unchecked* variation is *Undefined Behavior*.
pub struct DataView([u8]);

impl AsRef<[u8]> for DataView {
	#[inline]
	fn as_ref(&self) -> &[u8] {
		&self.0
	}
}
impl AsMut<[u8]> for DataView {
	#[inline]
	fn as_mut(&mut self) -> &mut [u8] {
		&mut self.0
	}
}

unsafe impl Pod for DataView {}

impl DataView {
	/// Returns the number of bytes in the instance.
	#[inline]
	pub const fn len(&self) -> usize {
		self.0.len()
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Copies a (potentially unaligned) value from the view.
	#[inline]
	pub fn try_copy<T: Pod>(&self, offset: usize) -> Option<T> {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get(index)?;
		unsafe {
			let src = bytes.as_ptr() as *const T;
			Some(ptr::read_unaligned(src))
		}
	}
	/// Copies a (potentially unaligned) value from the view.
	#[track_caller]
	#[inline]
	pub fn copy<T: Pod>(&self, offset: usize) -> T {
		match self.try_copy(offset) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Copies a (potentially unaligned) value from the view.
	#[inline]
	pub unsafe fn copy_unchecked<T: Pod>(&self, offset: usize) -> T {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get_unchecked(index);
		let src = bytes.as_ptr() as *const T;
		ptr::read_unaligned(src)
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Copies a (potentially unaligned) value from the view into the destination.
	#[inline]
	pub fn try_copy_into<T: Pod + ?Sized>(&self, offset: usize, dest: &mut T) -> Option<()> {
		let index = offset..offset + mem::size_of_val(dest);
		let bytes = self.0.get(index)?;
		unsafe {
			let src = bytes.as_ptr();
			let dst = dest.as_bytes_mut().as_mut_ptr();
			ptr::copy_nonoverlapping(src, dst, bytes.len());
			Some(())
		}
	}
	/// Copies a (potentially unaligned) value from the view into the destination.
	#[track_caller]
	#[inline]
	pub fn copy_into<T: Pod + ?Sized>(&self, offset: usize, dest: &mut T) {
		match self.try_copy_into(offset, dest) {
			Some(()) => (),
			None => invalid_offset(),
		}
	}
	/// Copies a (potentially unaligned) value from the view into the destination.
	#[inline]
	pub unsafe fn copy_into_unchecked<T: Pod + ?Sized>(&self, offset: usize, dest: &mut T) {
		let index = offset..offset + mem::size_of_val(dest);
		let bytes = self.0.get_unchecked(index);
		let src = bytes.as_ptr();
		let dst = dest.as_bytes_mut().as_mut_ptr();
		ptr::copy_nonoverlapping(src, dst, bytes.len());
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Reads an aligned value from the view.
	#[inline]
	pub fn try_read<T: Pod>(&self, offset: usize) -> Option<&T> {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get(index)?;
		if bytes.as_ptr() as usize % mem::align_of::<T>() != 0 {
			return None;
		}
		unsafe {
			Some(&*(bytes.as_ptr() as *const T))
		}
	}
	/// Reads an aligned value from the view.
	#[track_caller]
	#[inline]
	pub fn read<T: Pod>(&self, offset: usize) -> &T {
		match self.try_read(offset) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads an aligned value from the view.
	#[inline]
	pub unsafe fn read_unchecked<T: Pod>(&self, offset: usize) -> &T {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get_unchecked(index);
		&*(bytes.as_ptr() as *const T)
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Reads an aligned value from the view.
	#[inline]
	pub fn try_read_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut T> {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get_mut(index)?;
		if bytes.as_mut_ptr() as usize % mem::align_of::<T>() != 0 {
			return None;
		}
		unsafe {
			Some(&mut *(bytes.as_mut_ptr() as *mut T))
		}
	}
	/// Reads an aligned value from the view.
	#[track_caller]
	#[inline]
	pub fn read_mut<T: Pod>(&mut self, offset: usize) -> &mut T {
		match self.try_read_mut(offset) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads an aligned value from the view.
	#[inline]
	pub unsafe fn read_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut T {
		let index = offset..offset + mem::size_of::<T>();
		let bytes = self.0.get_unchecked_mut(index);
		&mut *(bytes.as_mut_ptr() as *mut T)
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Reads an aligned slice from the view.
	#[inline]
	pub fn try_slice<T: Pod>(&self, offset: usize, len: usize) -> Option<&[T]> {
		let index = offset..offset + usize::checked_mul(len, mem::size_of::<T>())?;
		let bytes = self.0.get(index)?;
		if bytes.as_ptr() as usize % mem::align_of::<T>() != 0 {
			return None;
		}
		unsafe {
			Some(slice::from_raw_parts(bytes.as_ptr() as *const T, len))
		}
	}
	/// Reads an aligned slice from the view.
	#[track_caller]
	#[inline]
	pub fn slice<T: Pod>(&self, offset: usize, len: usize) -> &[T] {
		match self.try_slice(offset, len) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads an aligned slice from the view.
	#[inline]
	pub unsafe fn slice_unchecked<T: Pod>(&self, offset: usize, len: usize) -> &[T] {
		let index = offset..offset + usize::wrapping_mul(len, mem::size_of::<T>());
		let bytes = self.0.get_unchecked(index);
		slice::from_raw_parts(bytes.as_ptr() as *const T, len)
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Reads an aligned slice from the view.
	#[inline]
	pub fn try_slice_mut<T: Pod>(&mut self, offset: usize, len: usize) -> Option<&mut [T]> {
		let index = offset..offset + usize::checked_mul(len, mem::size_of::<T>())?;
		let bytes = self.0.get_mut(index)?;
		if bytes.as_mut_ptr() as usize % mem::align_of::<T>() != 0 {
			return None;
		}
		unsafe {
			Some(slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut T, len))
		}
	}
	/// Reads an aligned slice from the view.
	#[track_caller]
	#[inline]
	pub fn slice_mut<T: Pod>(&mut self, offset: usize, len: usize) -> &mut [T] {
		match self.try_slice_mut(offset, len) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads an aligned slice from the view.
	#[inline]
	pub unsafe fn slice_unchecked_mut<T: Pod>(&mut self, offset: usize, len: usize) -> &mut [T] {
		let index = offset..offset + usize::wrapping_mul(len, mem::size_of::<T>());
		let bytes = self.0.get_unchecked_mut(index);
		slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut T, len)
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Returns the number of elements that would fit a slice starting at the given offset.
	#[inline]
	pub const fn tail_len<T>(&self, offset: usize) -> usize {
		(self.0.len() - offset) / mem::size_of::<T>()
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Writes a value to the view.
	#[inline]
	pub fn try_write<T: Pod + ?Sized>(&mut self, offset: usize, value: &T) -> Option<()> {
		let index = offset..offset + mem::size_of_val(value);
		let bytes = self.0.get_mut(index)?;
		bytes.copy_from_slice(value.as_bytes());
		Some(())
	}
	/// Writes a value to the view.
	#[track_caller]
	#[inline]
	pub fn write<T: Pod + ?Sized>(&mut self, offset: usize, value: &T) {
		match self.try_write(offset, value) {
			Some(()) => (),
			None => invalid_offset(),
		}
	}
	/// Writes a value to the view.
	#[inline]
	pub unsafe fn write_unchecked<T: Pod + ?Sized>(&mut self, offset: usize, value: &T) {
		let index = offset..offset + mem::size_of_val(value);
		let bytes = self.0.get_unchecked_mut(index);
		ptr::copy_nonoverlapping(value.as_bytes().as_ptr(), bytes.as_mut_ptr(), bytes.len());
	}
}

//----------------------------------------------------------------

impl DataView {
	/// Index the DataView creating a subview.
	#[inline]
	pub fn index<R: ops::RangeBounds<usize>>(&self, range: R) -> Option<&DataView> {
		let start = match range.start_bound() {
			ops::Bound::Unbounded => 0,
			ops::Bound::Included(start) => *start,
			ops::Bound::Excluded(start) => *start + 1,
		};
		let end = match range.end_bound() {
			ops::Bound::Unbounded => self.len(),
			ops::Bound::Included(end) => *end + 1,
			ops::Bound::Excluded(end) => *end,
		};
		let bytes = self.0.get(start..end)?;
		Some(bytes.as_data_view())
	}
	/// Index the DataView creating a mutable subview.
	#[inline]
	pub fn index_mut<R: ops::RangeBounds<usize>>(&mut self, range: R) -> Option<&mut DataView> {
		let start = match range.start_bound() {
			ops::Bound::Unbounded => 0,
			ops::Bound::Included(start) => *start,
			ops::Bound::Excluded(start) => *start + 1,
		};
		let end = match range.end_bound() {
			ops::Bound::Unbounded => self.len(),
			ops::Bound::Included(end) => *end + 1,
			ops::Bound::Excluded(end) => *end,
		};
		let bytes = self.0.get_mut(start..end)?;
		Some(bytes.as_data_view_mut())
	}
}

//----------------------------------------------------------------

impl<R: ops::RangeBounds<usize>> ops::Index<R> for DataView {
	type Output = DataView;

	#[inline]
	fn index(&self, range: R) -> &DataView {
		match self.index(range) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
}
impl<R: ops::RangeBounds<usize>> ops::IndexMut<R> for DataView {
	#[inline]
	fn index_mut(&mut self, range: R) -> &mut DataView {
		match self.index_mut(range) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
}

//----------------------------------------------------------------

#[doc(hidden)]
impl DataView {
	/// Reads the largest slice from the view.
	#[inline]
	pub fn try_slice_tail<T: Pod>(&self, offset: usize) -> Option<&[T]> {
		self.try_slice(offset, self.tail_len::<T>(offset))
	}
	/// Reads the largest slice from the view.
	#[track_caller]
	#[inline]
	pub fn slice_tail<T: Pod>(&self, offset: usize) -> &[T] {
		match self.try_slice_tail(offset) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads the largest slice from the view.
	#[inline]
	pub unsafe fn slice_tail_unchecked<T: Pod>(&self, offset: usize) -> &[T] {
		self.slice_unchecked(offset, self.tail_len::<T>(offset))
	}

	/// Reads the largest slice from the view.
	#[inline]
	pub fn try_slice_tail_mut<T: Pod>(&mut self, offset: usize) -> Option<&mut [T]> {
		self.try_slice_mut(offset, self.tail_len::<T>(offset))
	}
	/// Reads the largest slice from the view.
	#[track_caller]
	#[inline]
	pub fn slice_tail_mut<T: Pod>(&mut self, offset: usize) -> &mut [T] {
		match self.try_slice_tail_mut(offset) {
			Some(value) => value,
			None => invalid_offset(),
		}
	}
	/// Reads the largest slice from the view.
	#[inline]
	pub unsafe fn slice_tail_unchecked_mut<T: Pod>(&mut self, offset: usize) -> &mut [T] {
		self.slice_unchecked_mut(offset, self.tail_len::<T>(offset))
	}
}

//----------------------------------------------------------------

#[cold]
#[track_caller]
#[inline(never)]
fn invalid_offset() -> ! {
	panic!("invalid offset")
}