coap_message_implementations/inmemory/
buffer_impl.rs1#![cfg_attr(feature = "downcast", allow(unsafe_code))]
3
4use super::*;
5
6#[derive(Clone, Debug)]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub struct SliceBuffer<'a> {
12 code: u8,
13 options_and_payload: &'a [u8],
14}
15
16impl<'a> SliceBuffer<'a> {
17 #[must_use]
22 pub fn new(code: u8, options_and_payload: &'a [u8]) -> Self {
23 Self {
24 code,
25 options_and_payload,
26 }
27 }
28}
29
30impl MessageBuffer for SliceBuffer<'_> {
31 fn code(&self) -> u8 {
32 self.code
33 }
34 fn tail(&self) -> &[u8] {
35 self.options_and_payload
36 }
37
38 #[cfg(feature = "downcast")]
39 fn static_variant() -> Option<LifetimesMatterLittle<impl 'static + MessageBuffer>> {
40 Some(unsafe { LifetimesMatterLittle::<SliceBuffer<'static>>::new() })
42 }
43}
44
45pub struct SliceBufferMut<'a> {
47 code: &'a mut u8,
48 tail: &'a mut [u8],
49}
50
51impl<'a> SliceBufferMut<'a> {
52 pub fn new(code: &'a mut u8, tail: &'a mut [u8]) -> Self {
57 Self { code, tail }
58 }
59}
60
61impl MessageBufferMut for SliceBufferMut<'_> {
62 fn code_mut(&mut self) -> &mut u8 {
63 self.code
64 }
65
66 fn tail_mut(&mut self) -> &mut [u8] {
67 self.tail
68 }
69
70 #[cfg(feature = "downcast")]
71 fn static_mut_variant() -> Option<LifetimesMatterLittle<impl 'static + MessageBufferMut>> {
72 Some(unsafe { LifetimesMatterLittle::<SliceBufferMut<'static>>::new() })
74 }
75}
76
77impl MessageBuffer for SliceBufferMut<'_> {
78 fn code(&self) -> u8 {
79 *self.code
80 }
81
82 fn tail(&self) -> &[u8] {
83 self.tail
84 }
85}
86
87impl MessageBuffer for core::convert::Infallible {
88 fn code(&self) -> u8 {
89 match *self {}
90 }
91
92 fn tail(&self) -> &[u8] {
93 match *self {}
94 }
95}
96
97impl MessageBufferMut for core::convert::Infallible {
98 fn code_mut(&mut self) -> &mut u8 {
99 match *self {}
100 }
101
102 fn tail_mut(&mut self) -> &mut [u8] {
103 match *self {}
104 }
105}