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
#[cfg(test)]
macro_rules! assert_size_of {
($size_of:expr, $ident:ident) => (assert_eq!($size_of, ::std::mem::size_of::<$ident>(),
concat!("Unexpected sizeof(", stringify!($ident), ")")));
}
#[test]
#[should_panic]
fn assert_size_of() {
struct Foo(i32);
assert_size_of!(8, Foo);
}
macro_rules! offset_of {
($ty:ty, $($field_bits:tt)*) => {
unsafe { &(*(0 as *const $ty)).$($field_bits)* as *const _ as usize }
}
}
mod c_str;
mod wide_str;
mod pod;
mod guid;
#[cfg(feature = "serde")]
pub(crate) mod serde_helper;
pub use self::c_str::CStr;
pub use self::wide_str::WideStr;
pub use self::pod::Pod;
pub trait FromBytes {
const MIN_SIZE_OF: usize;
const ALIGN_OF: usize;
unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self>;
}
#[inline]
pub(crate) fn split_f<T, F: FnMut(&T) -> bool>(slice: &[T], f: F) -> (&[T], &[T]) {
let i = slice.iter().position(f).unwrap_or(slice.len());
(&slice[..i], &slice[i..])
}
#[inline]
pub fn strn(buf: &[u8]) -> &[u8] {
split_f(buf, |&byte| byte == 0).0
}
#[inline]
pub fn wstrn(buf: &[u16]) -> &[u16] {
split_f(buf, |&word| word == 0).0
}