mod3d_base/
byte_buffer.rs1pub trait ByteBuffer {
12 fn byte_length(&self) -> usize;
14 fn borrow_bytes(&self) -> &[u8];
16 fn as_u8_ptr(&self) -> *const u8;
18}
19
20impl<T, const N: usize> ByteBuffer for [T; N] {
23 fn byte_length(&self) -> usize {
25 std::mem::size_of::<T>() * N
26 }
27
28 fn borrow_bytes(&self) -> &[u8] {
30 let len = std::mem::size_of::<T>() * self.len();
31 let data = self.as_u8_ptr();
32 unsafe { std::slice::from_raw_parts(data, len) }
33 }
34
35 fn as_u8_ptr(&self) -> *const u8 {
37 let data: *const T = &self[0];
38 unsafe { std::mem::transmute::<_, *const u8>(data) }
39 }
40
41 }
43
44impl<T> ByteBuffer for Vec<T> {
47 fn byte_length(&self) -> usize {
49 std::mem::size_of::<T>() * self.len()
50 }
51
52 fn borrow_bytes(&self) -> &[u8] {
54 let len = std::mem::size_of::<T>() * self.len();
55 let data = self.as_u8_ptr();
56 unsafe { std::slice::from_raw_parts(data, len) }
57 }
58
59 fn as_u8_ptr(&self) -> *const u8 {
61 let data: *const T = &self[0];
62 unsafe { std::mem::transmute::<_, *const u8>(data) }
63 }
64
65 }
67
68impl<T> ByteBuffer for &[T] {
71 fn byte_length(&self) -> usize {
73 std::mem::size_of_val(*self)
74 }
75
76 fn borrow_bytes(&self) -> &[u8] {
78 let len = std::mem::size_of_val(*self);
79 let data = self.as_u8_ptr();
80 unsafe { std::slice::from_raw_parts(data, len) }
81 }
82
83 fn as_u8_ptr(&self) -> *const u8 {
85 let data: *const T = self.as_ptr();
86 unsafe { std::mem::transmute::<_, *const u8>(data) }
87 }
88
89 }