Skip to main content

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        match self {
26            Ok(t) => t.render(msg).map_err(Ok)?,
27            Err(e) => e.render(msg).map_err(Err)?,
28        };
29        Ok(())
30    }
31}
32
33impl<T: ReadableMessage> ReadableMessage for &T {
34    type Code = T::Code;
35
36    type MessageOption<'a>
37        = T::MessageOption<'a>
38    where
39        Self: 'a;
40
41    type OptionsIter<'a>
42        = T::OptionsIter<'a>
43    where
44        Self: 'a;
45
46    fn code(&self) -> Self::Code {
47        (*self).code()
48    }
49
50    fn options(&self) -> Self::OptionsIter<'_> {
51        (*self).options()
52    }
53
54    fn payload(&self) -> &[u8] {
55        (*self).payload()
56    }
57}
58
59impl<T: MinimalWritableMessage> MinimalWritableMessage for &mut T {
60    type Code = T::Code;
61
62    type OptionNumber = T::OptionNumber;
63    type AddOptionError = T::AddOptionError;
64    type SetPayloadError = T::SetPayloadError;
65    type UnionError = T::UnionError;
66
67    fn set_code(&mut self, code: Self::Code) {
68        (**self).set_code(code)
69    }
70
71    fn add_option(
72        &mut self,
73        number: Self::OptionNumber,
74        value: &[u8],
75    ) -> Result<(), Self::AddOptionError> {
76        (**self).add_option(number, value)
77    }
78
79    fn set_payload(&mut self, data: &[u8]) -> Result<(), Self::SetPayloadError> {
80        (**self).set_payload(data)
81    }
82}
83
84impl<T: MutableWritableMessage> MutableWritableMessage for &mut T {
85    fn available_space(&self) -> usize {
86        (**self).available_space()
87    }
88
89    fn payload_mut_with_len(&mut self, len: usize) -> Result<&mut [u8], Self::SetPayloadError> {
90        (**self).payload_mut_with_len(len)
91    }
92
93    fn truncate(&mut self, len: usize) -> Result<(), Self::SetPayloadError> {
94        (**self).truncate(len)
95    }
96
97    fn mutate_options<F>(&mut self, callback: F)
98    where
99        F: FnMut(Self::OptionNumber, &mut [u8]),
100    {
101        (**self).mutate_options(callback)
102    }
103}