async_coap/message/
null.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17
18/// Null message writer. Anything written to this instance will be ignored.
19#[derive(Debug)]
20pub struct NullMessageWrite;
21impl MessageWrite for NullMessageWrite {
22    fn set_msg_type(&mut self, _: MsgType) {}
23
24    fn set_msg_id(&mut self, _: u16) {}
25
26    fn set_msg_code(&mut self, _: MsgCode) {}
27
28    fn set_msg_token(&mut self, _: MsgToken) {}
29
30    fn append_payload_bytes(&mut self, _: &[u8]) -> Result<(), Error> {
31        Ok(())
32    }
33
34    fn clear(&mut self) {}
35}
36
37impl OptionInsert for NullMessageWrite {
38    fn insert_option_with_bytes(&mut self, _key: OptionNumber, _value: &[u8]) -> Result<(), Error> {
39        Ok(())
40    }
41}
42
43/// Null message reader. Always reads as an empty CoAP reset.
44#[derive(Debug)]
45pub struct NullMessageRead;
46impl MessageRead for NullMessageRead {
47    fn msg_code(&self) -> MsgCode {
48        MsgCode::Empty
49    }
50
51    fn msg_type(&self) -> MsgType {
52        MsgType::Res
53    }
54
55    fn msg_id(&self) -> u16 {
56        0x0000
57    }
58
59    fn msg_token(&self) -> MsgToken {
60        MsgToken::EMPTY
61    }
62
63    fn payload(&self) -> &[u8] {
64        &[]
65    }
66
67    fn content_format(&self) -> Option<ContentFormat> {
68        None
69    }
70
71    fn accept(&self) -> Option<ContentFormat> {
72        None
73    }
74    fn block2(&self) -> Option<BlockInfo> {
75        None
76    }
77    fn block1(&self) -> Option<BlockInfo> {
78        None
79    }
80
81    fn options(&self) -> OptionIterator<'_> {
82        OptionIterator::new(&[])
83    }
84}
85
86impl std::fmt::Display for NullMessageRead {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        MessageDisplay(self).fmt(f)
89    }
90}