mdns_proto/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![forbid(unsafe_code)]
4#![deny(missing_docs)]
5#![allow(unexpected_cfgs)]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![cfg_attr(docsrs, allow(unused_attributes))]
8#![allow(clippy::needless_return)]
9#![allow(unreachable_code)]
10
11#[cfg(feature = "slab")]
12pub use slab;
13pub use srv::*;
14pub use txt::*;
15
16/// The error type for the mDNS protocol
17pub mod error;
18
19/// The server endpoint
20pub mod server;
21
22/// The client endpoint
23pub mod client;
24
25/// An implementation of the mDNS protocol
26pub mod proto {
27  pub use super::srv::Srv;
28  pub use super::txt::{Str, Strings, Txt};
29  pub use dns_protocol::{
30    Cursor, Deserialize, Flags, Header, Label, LabelSegment, Message, MessageType, Opcode,
31    Question, ResourceRecord, ResourceType, ResponseCode, Serialize,
32  };
33}
34
35mod srv;
36mod txt;
37
38/// Internal identifier for a `Connection` currently associated with an endpoint
39#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
40pub struct ConnectionHandle(pub usize);
41
42impl From<ConnectionHandle> for usize {
43  fn from(x: ConnectionHandle) -> Self {
44    x.0
45  }
46}
47
48impl core::fmt::Display for ConnectionHandle {
49  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50    write!(f, "{}", self.0)
51  }
52}
53
54/// Pre-allocated storage for a uniform data type.
55pub trait Pool<V> {
56  /// The type of the errors that can occur when interacting with the slab.
57  type Error: core::error::Error;
58
59  /// The iterator type for the slab.
60  type Iter<'a>: Iterator<Item = (usize, &'a V)>
61  where
62    Self: 'a,
63    V: 'a;
64
65  /// Returns a new, empty slab.
66  fn new() -> Self;
67
68  /// Returns a new slab with the specified capacity.
69  ///
70  /// Returns an error if the slab cannot hold the specified number of entries.
71  fn with_capacity(capacity: usize) -> Result<Self, Self::Error>
72  where
73    Self: Sized;
74
75  /// Returns the key of the next vacant entry.
76  ///
77  /// If the slab cannot hold any more entries, an error is returned.
78  fn vacant_key(&self) -> Result<usize, Self::Error>;
79
80  /// Returns `true` if the slab is empty.
81  fn is_empty(&self) -> bool;
82
83  /// Returns the number of entries in the slab.
84  fn len(&self) -> usize;
85
86  /// Return a reference to the value associated with the given key.
87  ///
88  /// If the given key is not associated with a value, then `None` is
89  /// returned.
90  fn get(&self, key: usize) -> Option<&V>;
91
92  /// Return a mutable reference to the value associated with the given key.
93  ///
94  /// If the given key is not associated with a value, then `None` is
95  /// returned.
96  fn get_mut(&mut self, key: usize) -> Option<&mut V>;
97
98  /// Insert a value in the slab, returning key assigned to the value.
99  ///
100  /// The returned key can later be used to retrieve or remove the value using indexed
101  /// lookup and `remove`.
102  ///
103  /// Returns an error if the slab cannot hold any more entries.
104  fn insert(&mut self, value: V) -> Result<usize, Self::Error>;
105
106  /// Tries to remove the value associated with the given key,
107  /// returning the value if the key existed.
108  ///
109  /// The key is then released and may be associated with future stored
110  /// values.
111  fn try_remove(&mut self, key: usize) -> Option<V>;
112
113  /// Returns an iterator over the slab.
114  fn iter(&self) -> Self::Iter<'_>;
115}
116
117#[cfg(feature = "slab")]
118impl<T> Pool<T> for slab::Slab<T> {
119  type Error = core::convert::Infallible;
120
121  type Iter<'a>
122    = slab::Iter<'a, T>
123  where
124    Self: 'a;
125
126  fn new() -> Self {
127    slab::Slab::new()
128  }
129
130  fn with_capacity(capacity: usize) -> Result<Self, Self::Error>
131  where
132    Self: Sized,
133  {
134    Ok(slab::Slab::with_capacity(capacity))
135  }
136
137  fn vacant_key(&self) -> Result<usize, Self::Error> {
138    Ok(slab::Slab::vacant_key(self))
139  }
140
141  fn is_empty(&self) -> bool {
142    slab::Slab::is_empty(self)
143  }
144
145  fn len(&self) -> usize {
146    slab::Slab::len(self)
147  }
148
149  fn get(&self, key: usize) -> Option<&T> {
150    slab::Slab::get(self, key)
151  }
152
153  fn get_mut(&mut self, key: usize) -> Option<&mut T> {
154    slab::Slab::get_mut(self, key)
155  }
156
157  fn insert(&mut self, value: T) -> Result<usize, Self::Error> {
158    Ok(slab::Slab::insert(self, value))
159  }
160
161  fn try_remove(&mut self, key: usize) -> Option<T> {
162    slab::Slab::try_remove(self, key)
163  }
164
165  fn iter(&self) -> Self::Iter<'_> {
166    slab::Slab::iter(self)
167  }
168}