unibox/stack/
buffer.rs

1/// Interface for supported buffer types.
2/// 
3/// The internal buffer of all uniboxes must implement this trait.
4pub unsafe trait Buffer {
5    /// Init the type.
6    fn init() -> Self;
7    /// Raw pointer to type.
8    fn ptr<T>(&self) -> *const T;
9    /// Copy from byte array to type *len* bytes.
10    fn copy_from_byte(&mut self, src: &[u8], len: usize);
11    /// Copy from type to type *len* bytes.
12    fn copy_from_type(&mut self, src: &Self, len: usize);
13}
14
15unsafe impl Buffer for [u8; 32] {
16    fn init() -> Self {
17        [0; 32]
18    }
19
20    fn ptr<T>(&self) -> *const T {
21        self.as_ptr() as *const T
22    }
23
24    fn copy_from_byte(&mut self, src: &[u8], len: usize) {
25        self[0..len].clone_from_slice(src);
26    }
27
28    fn copy_from_type(&mut self, src: &Self, len: usize) {
29        self[0..len].clone_from_slice(&src[0..len]);
30    }
31}
32
33unsafe impl Buffer for [u8; 64] {
34    fn init() -> Self {
35        [0; 64]
36    }
37
38    fn ptr<T>(&self) -> *const T {
39        self.as_ptr() as *const T
40    }
41
42    fn copy_from_byte(&mut self, src: &[u8], len: usize) {
43        self[0..len].clone_from_slice(src);
44    }
45
46    fn copy_from_type(&mut self, src: &Self, len: usize) {
47        self[0..len].clone_from_slice(&src[0..len]);
48    }
49}
50
51unsafe impl Buffer for [u8; 128] {
52    fn init() -> Self {
53        [0; 128]
54    }
55
56    fn ptr<T>(&self) -> *const T {
57        self.as_ptr() as *const T
58    }
59
60    fn copy_from_byte(&mut self, src: &[u8], len: usize) {
61        self[0..len].clone_from_slice(src);
62    }
63
64    fn copy_from_type(&mut self, src: &Self, len: usize) {
65        self[0..len].clone_from_slice(&src[0..len]);
66    }
67}
68
69unsafe impl Buffer for [u8; 256] {
70    fn init() -> Self {
71        [0; 256]
72    }
73
74    fn ptr<T>(&self) -> *const T {
75        self.as_ptr() as *const T
76    }
77
78    fn copy_from_byte(&mut self, src: &[u8], len: usize) {
79        self[0..len].clone_from_slice(src);
80    }
81
82    fn copy_from_type(&mut self, src: &Self, len: usize) {
83        self[0..len].clone_from_slice(&src[0..len]);
84    }
85}