commonware_utils/
stable_buf.rs1#[allow(clippy::len_without_is_empty)]
10pub unsafe trait StableBuf: Unpin + Send + 'static {
11 fn stable_ptr(&self) -> *const u8;
13
14 fn len(&self) -> usize;
16
17 fn as_ref(&self) -> &[u8] {
19 unsafe { std::slice::from_raw_parts(self.stable_ptr(), self.len()) }
20 }
21}
22
23unsafe impl StableBuf for Vec<u8> {
24 fn stable_ptr(&self) -> *const u8 {
25 self.as_ptr()
26 }
27
28 fn len(&self) -> usize {
29 self.len()
30 }
31}
32
33unsafe impl StableBuf for &'static [u8] {
34 fn stable_ptr(&self) -> *const u8 {
35 self.as_ptr()
36 }
37
38 fn len(&self) -> usize {
39 <[u8]>::len(self)
40 }
41}
42
43unsafe impl StableBuf for &'static str {
44 fn stable_ptr(&self) -> *const u8 {
45 self.as_ptr()
46 }
47
48 fn len(&self) -> usize {
49 <str>::len(self)
50 }
51}
52
53unsafe impl StableBuf for bytes::Bytes {
54 fn stable_ptr(&self) -> *const u8 {
55 self.as_ptr()
56 }
57
58 fn len(&self) -> usize {
59 self.len()
60 }
61}
62
63unsafe impl StableBuf for bytes::BytesMut {
64 fn stable_ptr(&self) -> *const u8 {
65 self.as_ptr()
66 }
67
68 fn len(&self) -> usize {
69 self.len()
70 }
71}