Skip to main content

mdns_proto/
handle.rs

1//! Opaque identifiers for registered services and in-flight queries.
2//!
3//! Handles are returned from `try_register_service` / `try_start_query` and
4//! are used to route incoming events back to the matching `Service` / `Query`
5//! state machine.
6//!
7//! The wrapped `u32` representation is an internal detail; do not depend on
8//! its numeric value beyond comparison and hashing.
9
10use derive_more::Display;
11
12/// Opaque identifier for a registered service.
13#[derive(Debug, Display, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
14#[display("ServiceHandle({_0})")]
15pub struct ServiceHandle(u32);
16
17impl ServiceHandle {
18  /// Constructs a handle from a raw `u32`. Intended for internal crate use
19  /// (e.g. rebuilding a handle from persisted state).
20  #[allow(dead_code)]
21  #[inline(always)]
22  pub(crate) const fn from_raw(raw: u32) -> Self {
23    Self(raw)
24  }
25
26  /// Returns the raw `u32` representation. Stable across releases for the
27  /// purpose of equality / hashing only.
28  #[inline(always)]
29  pub const fn raw(self) -> u32 {
30    self.0
31  }
32}
33
34/// Opaque identifier for an in-flight query.
35#[derive(Debug, Display, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
36#[display("QueryHandle({_0})")]
37pub struct QueryHandle(u32);
38
39impl QueryHandle {
40  /// Constructs a handle from a raw `u32`. Crate-internal.
41  #[allow(dead_code)]
42  #[inline(always)]
43  pub(crate) const fn from_raw(raw: u32) -> Self {
44    Self(raw)
45  }
46
47  /// Returns the raw `u32` representation.
48  #[inline(always)]
49  pub const fn raw(self) -> u32 {
50    self.0
51  }
52}
53
54#[cfg(test)]
55#[allow(clippy::unwrap_used)]
56mod tests;