async_coap/option/
key.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/// Typed option key, for type-safe access to CoAP options.
19#[derive(Hash, PartialEq, Eq, Ord, PartialOrd)]
20pub struct OptionKey<T>(pub OptionNumber, core::marker::PhantomData<*const T>);
21
22impl<T> OptionKey<T> {
23    /// Creates a new instance with the given option number.
24    pub const fn new(n: OptionNumber) -> OptionKey<T> {
25        OptionKey(n, core::marker::PhantomData)
26    }
27}
28
29impl<T> Copy for OptionKey<T> {}
30
31impl<T> Clone for OptionKey<T> {
32    fn clone(&self) -> Self {
33        OptionKey(self.0, core::marker::PhantomData)
34    }
35}
36
37unsafe impl<T> Send for OptionKey<T> {}
38
39impl<T> core::fmt::Debug for OptionKey<T> {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        write!(f, "{:?}", self.0)
42    }
43}
44
45impl<T> core::ops::Deref for OptionKey<T> {
46    type Target = OptionNumber;
47
48    fn deref(&self) -> &Self::Target {
49        &self.0
50    }
51}
52
53/// Typed key for IF_MATCH option.
54pub const IF_MATCH: OptionKey<ETag> = OptionKey::new(OptionNumber::IF_MATCH);
55
56/// Typed key for URI_HOST option.
57pub const URI_HOST: OptionKey<&str> = OptionKey::new(OptionNumber::URI_HOST);
58
59/// Typed key for ETAG option.
60pub const ETAG: OptionKey<ETag> = OptionKey::new(OptionNumber::ETAG);
61
62/// Typed key for IF_NONE_MATCH option.
63pub const IF_NONE_MATCH: OptionKey<()> = OptionKey::new(OptionNumber::IF_NONE_MATCH);
64
65/// Typed key for Observe option.
66pub const OBSERVE: OptionKey<u32> = OptionKey::new(OptionNumber::OBSERVE);
67
68/// Typed key for URI-Port option.
69pub const URI_PORT: OptionKey<u16> = OptionKey::new(OptionNumber::URI_PORT);
70
71/// Typed key for Location-Path option.
72pub const LOCATION_PATH: OptionKey<&str> = OptionKey::new(OptionNumber::LOCATION_PATH);
73
74/// Typed key for OSCORE option.
75pub const OSCORE: OptionKey<&[u8]> = OptionKey::new(OptionNumber::OSCORE);
76
77/// Typed key for URI-Path option.
78pub const URI_PATH: OptionKey<&str> = OptionKey::new(OptionNumber::URI_PATH);
79
80/// Typed key for Content-Format option.
81pub const CONTENT_FORMAT: OptionKey<ContentFormat> = OptionKey::new(OptionNumber::CONTENT_FORMAT);
82
83/// Typed key for Max-Age option.
84pub const MAX_AGE: OptionKey<u32> = OptionKey::new(OptionNumber::MAX_AGE);
85
86/// Typed key for URI-Query option.
87pub const URI_QUERY: OptionKey<&str> = OptionKey::new(OptionNumber::URI_QUERY);
88
89/// Typed key for Accept option.
90pub const ACCEPT: OptionKey<ContentFormat> = OptionKey::new(OptionNumber::ACCEPT);
91
92/// Typed key for Location-Query option.
93pub const LOCATION_QUERY: OptionKey<&str> = OptionKey::new(OptionNumber::LOCATION_QUERY);
94
95/// Typed key for Block2 option.
96pub const BLOCK2: OptionKey<BlockInfo> = OptionKey::new(OptionNumber::BLOCK2);
97
98/// Typed key for Block1 option.
99pub const BLOCK1: OptionKey<BlockInfo> = OptionKey::new(OptionNumber::BLOCK1);
100
101/// Typed key for Size2 option.
102pub const SIZE2: OptionKey<u32> = OptionKey::new(OptionNumber::SIZE2);
103
104/// Typed key for Proxy-URI option.
105pub const PROXY_URI: OptionKey<&str> = OptionKey::new(OptionNumber::PROXY_URI);
106
107/// Typed key for Proxy-Scheme option.
108pub const PROXY_SCHEME: OptionKey<&str> = OptionKey::new(OptionNumber::PROXY_SCHEME);
109
110/// Typed key for Size1 option.
111pub const SIZE1: OptionKey<u32> = OptionKey::new(OptionNumber::SIZE1);