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
pub trait DataBuf: AsMut<[Self::Inner]> + AsRef<[Self::Inner]> {
type Inner: Pod;
fn extend(&mut self, len: usize) -> Result<(), ()>;
fn round_to_words(bytes: usize) -> usize {
crate::round_to_words::<Self::Inner>(bytes)
}
}
pub unsafe trait Pod: Copy + Default {}
macro_rules! impl_pod {
( $($t:ty),* ) => {
$( unsafe impl Pod for $t {} )*
}
}
impl_pod! { u8, u16, u32, u64, u128, usize }
impl<T: DataBuf> DataBuf for &mut T {
type Inner = T::Inner;
fn extend(&mut self, len: usize) -> Result<(), ()> {
(**self).extend(len)
}
}
#[cfg(not(feature = "const_generics"))]
macro_rules! impl_databuf_array {
( $($n:expr),* ) => {
$(impl<T: Pod> DataBuf for [T; $n] {
type Inner = T;
fn extend(&mut self, _: usize) -> Result<(), ()> {
Err(())
}
})*
}
}
#[cfg(not(feature = "const_generics"))]
impl_databuf_array! {
0, 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,48,
64,96,
128,192,
256
}
#[cfg(feature = "const_generics")]
impl<T: Pod, const N: usize> DataBuf for [T; N] {
type Inner = T;
fn extend(&mut self, _: usize) -> Result<(), ()> {
Err(())
}
}
#[cfg(all(feature = "alloc"))]
impl<T: Pod> crate::DataBuf for ::alloc::vec::Vec<T> {
type Inner = T;
fn extend(&mut self, len: usize) -> Result<(), ()> {
if len > self.len() {
self.resize(len, Default::default());
let cap = self.capacity();
self.resize(cap, Default::default());
}
Ok(())
}
}