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
//a ByteBuffer
//tp ByteBuffer
/// A trait for all types that are to be used as sources of data for
/// buffers of, e.g. vertex data, indices, etc
///
/// The data is viewed by OpenGL as a pointer and byte length; these
/// methods provide access to the data in that way.
///
/// These methods are all safe - any use of the information they
/// provide may be unsafe.
pub trait ByteBuffer {
/// Get the length of the data buffer in bytes
fn byte_length(&self) -> usize;
/// Borrow the data as an array of bytes
fn borrow_bytes(&self) -> &[u8];
/// Return a pointer to the first byte of the data contents
fn as_u8_ptr(&self) -> *const u8;
}
//ti ByteBuffer for [T; N]
/// Implement ByteBuffer for slice of T
impl<T, const N: usize> ByteBuffer for [T; N] {
//fp byte_length
fn byte_length(&self) -> usize {
std::mem::size_of::<T>() * N
}
//fp borrow_bytes
///
fn borrow_bytes(&self) -> &[u8] {
let len = std::mem::size_of::<T>() * self.len();
let data = self.as_u8_ptr();
// # Safety
//
// The resultant slice is derived from a valid pointer and
// length; the data can be interpreted as u8 if required; so
// this is safe.
unsafe { std::slice::from_raw_parts(data, len) }
}
//fp as_u8_ptr
fn as_u8_ptr(&self) -> *const u8 {
let data: *const T = &self[0];
// # Safety
//
// The resultant pointer is a valid pointer to u8 (assuming T
// is initialized?). Use of the resultant pointer is always
// unsafe, as it is a pointer; but that is down to the user of
// a pointer.
unsafe { std::mem::transmute::<_, *const u8>(data) }
}
//zz All done
}
//ti ByteBuffer for Vec
/// Implement ByteBuffer for Vec
impl<T> ByteBuffer for Vec<T> {
//fp byte_length
fn byte_length(&self) -> usize {
std::mem::size_of::<T>() * self.len()
}
//fp borrow_bytes
fn borrow_bytes(&self) -> &[u8] {
let len = std::mem::size_of::<T>() * self.len();
let data = self.as_u8_ptr();
// # Safety
//
// The resultant slice is derived from a valid pointer and
// length; the data can be interpreted as u8 if required; so
// this is safe.
unsafe { std::slice::from_raw_parts(data, len) }
}
//fp as_u8_ptr
fn as_u8_ptr(&self) -> *const u8 {
let data: *const T = &self[0];
// # Safety
//
// The resultant pointer is a valid pointer to u8 (assuming T
// is initialized?). Use of the resultant pointer is always
// unsafe, as it is a pointer; but that is down to the user of
// a pointer.
unsafe { std::mem::transmute::<_, *const u8>(data) }
}
//zz All done
}
//ti ByteBuffer for &[T]
/// Implement ByteBuffer for &[T]
impl<T> ByteBuffer for &[T] {
//fp byte_length
fn byte_length(&self) -> usize {
std::mem::size_of_val(*self)
}
//fp borrow_bytes
fn borrow_bytes(&self) -> &[u8] {
let len = std::mem::size_of_val(*self);
let data = self.as_u8_ptr();
// # Safety
//
// The resultant slice is derived from a valid pointer and
// length; the data can be interpreted as u8 if required; so
// this is safe.
unsafe { std::slice::from_raw_parts(data, len) }
}
//fp as_u8_ptr
fn as_u8_ptr(&self) -> *const u8 {
let data: *const T = self.as_ptr();
// # Safety
//
// The resultant pointer is a valid pointer to u8 (assuming T
// is initialized?). Use of the resultant pointer is always
// unsafe, as it is a pointer; but that is down to the user of
// a pointer.
unsafe { std::mem::transmute::<_, *const u8>(data) }
}
//zz All done
}