coap_message/
core_impl.rs

1//! Implementations of our traits for various [core] types
2
3use crate::error::RenderableOnMinimal;
4use crate::message::{MinimalWritableMessage, MutableWritableMessage, ReadableMessage};
5use core::fmt::Debug;
6
7impl RenderableOnMinimal for core::convert::Infallible {
8    type Error<IE: RenderableOnMinimal + Debug> = core::convert::Infallible;
9
10    fn render<M: crate::MinimalWritableMessage>(
11        self,
12        _: &mut M,
13    ) -> Result<(), core::convert::Infallible> {
14        match self {}
15    }
16}
17
18impl<T: RenderableOnMinimal, E: RenderableOnMinimal> RenderableOnMinimal for Result<T, E> {
19    type Error<IE: RenderableOnMinimal + Debug> = Result<T::Error<IE>, E::Error<IE>>;
20
21    fn render<M: crate::MinimalWritableMessage>(
22        self,
23        msg: &mut M,
24    ) -> Result<(), Self::Error<M::UnionError>> {
25        Ok(match self {
26            Ok(t) => t.render(msg).map_err(Ok)?,
27            Err(e) => e.render(msg).map_err(Err)?,
28        })
29    }
30}
31
32impl<T: ReadableMessage> ReadableMessage for &T {
33    type Code = T::Code;
34
35    type MessageOption<'a> = T::MessageOption<'a>
36    where
37        Self: 'a;
38
39    type OptionsIter<'a> = T::OptionsIter<'a>
40    where
41        Self: 'a;
42
43    fn code(&self) -> Self::Code {
44        (*self).code()
45    }
46
47    fn options(&self) -> Self::OptionsIter<'_> {
48        (*self).options()
49    }
50
51    fn payload(&self) -> &[u8] {
52        (*self).payload()
53    }
54}
55
56impl<T: MinimalWritableMessage> MinimalWritableMessage for &mut T {
57    type Code = T::Code;
58
59    type OptionNumber = T::OptionNumber;
60    type AddOptionError = T::AddOptionError;
61    type SetPayloadError = T::SetPayloadError;
62    type UnionError = T::UnionError;
63
64    fn set_code(&mut self, code: Self::Code) {
65        (**self).set_code(code)
66    }
67
68    fn add_option(
69        &mut self,
70        number: Self::OptionNumber,
71        value: &[u8],
72    ) -> Result<(), Self::AddOptionError> {
73        (**self).add_option(number, value)
74    }
75
76    fn set_payload(&mut self, data: &[u8]) -> Result<(), Self::SetPayloadError> {
77        (**self).set_payload(data)
78    }
79}
80
81impl<T: MutableWritableMessage> MutableWritableMessage for &mut T {
82    fn available_space(&self) -> usize {
83        (**self).available_space()
84    }
85
86    fn payload_mut_with_len(&mut self, len: usize) -> Result<&mut [u8], Self::SetPayloadError> {
87        (**self).payload_mut_with_len(len)
88    }
89
90    fn truncate(&mut self, len: usize) -> Result<(), Self::SetPayloadError> {
91        (**self).truncate(len)
92    }
93
94    fn mutate_options<F>(&mut self, callback: F)
95    where
96        F: FnMut(Self::OptionNumber, &mut [u8]),
97    {
98        (**self).mutate_options(callback)
99    }
100}