coap_message/
core_impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Implementations of our traits for various [core] types

use crate::error::RenderableOnMinimal;
use crate::message::ReadableMessage;
use core::fmt::Debug;

impl RenderableOnMinimal for core::convert::Infallible {
    type Error<IE: RenderableOnMinimal + Debug> = core::convert::Infallible;

    fn render<M: crate::MinimalWritableMessage>(
        self,
        _: &mut M,
    ) -> Result<(), core::convert::Infallible> {
        match self {}
    }
}

impl<T: RenderableOnMinimal, E: RenderableOnMinimal> RenderableOnMinimal for Result<T, E> {
    type Error<IE: RenderableOnMinimal + Debug> = Result<T::Error<IE>, E::Error<IE>>;

    fn render<M: crate::MinimalWritableMessage>(
        self,
        msg: &mut M,
    ) -> Result<(), Self::Error<M::UnionError>> {
        Ok(match self {
            Ok(t) => t.render(msg).map_err(Ok)?,
            Err(e) => e.render(msg).map_err(Err)?,
        })
    }
}

impl<T: ReadableMessage> ReadableMessage for &T {
    type Code = T::Code;

    type MessageOption<'a> = T::MessageOption<'a>
    where
        Self: 'a;

    type OptionsIter<'a> = T::OptionsIter<'a>
    where
        Self: 'a;

    fn code(&self) -> Self::Code {
        (*self).code()
    }

    fn options(&self) -> Self::OptionsIter<'_> {
        (*self).options()
    }

    fn payload(&self) -> &[u8] {
        (*self).payload()
    }
}