1use std::{fmt, hash::Hash, marker::PhantomData};
13
14use serde::{Deserialize, Serialize};
15
16use super::{Family, HandlerKey};
17
18#[derive(Clone, Serialize, Deserialize)]
23#[serde(transparent, bound = "")]
24pub struct Key<F: Family> {
25 raw: HandlerKey,
26 #[serde(skip)]
27 _family: PhantomData<fn() -> F>,
28}
29
30impl<F: Family> Key<F> {
31 pub const fn new_unchecked(raw: HandlerKey) -> Self {
36 Self {
37 raw,
38 _family: PhantomData,
39 }
40 }
41
42 pub const fn raw(&self) -> &HandlerKey {
44 &self.raw
45 }
46
47 pub fn into_raw(self) -> HandlerKey {
49 self.raw
50 }
51
52 pub fn as_str(&self) -> &str {
54 self.raw.as_str()
55 }
56}
57
58impl<F: Family> From<Key<F>> for HandlerKey {
59 fn from(key: Key<F>) -> Self {
60 key.raw
61 }
62}
63
64impl<F: Family> AsRef<HandlerKey> for Key<F> {
65 fn as_ref(&self) -> &HandlerKey {
66 &self.raw
67 }
68}
69
70impl<F: Family> PartialEq for Key<F> {
72 fn eq(&self, other: &Self) -> bool {
73 self.raw == other.raw
74 }
75}
76impl<F: Family> Eq for Key<F> {}
77impl<F: Family> PartialOrd for Key<F> {
78 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
79 Some(self.cmp(other))
80 }
81}
82impl<F: Family> Ord for Key<F> {
83 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
84 self.raw.cmp(&other.raw)
85 }
86}
87impl<F: Family> Hash for Key<F> {
88 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
89 self.raw.hash(state);
90 }
91}
92impl<F: Family> fmt::Debug for Key<F> {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 write!(f, "Key<{}>({:?})", F::FAMILY, self.raw.as_str())
95 }
96}
97impl<F: Family> fmt::Display for Key<F> {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 fmt::Display::fmt(&self.raw, f)
100 }
101}
102
103const _: () = {
105 const fn assert_send_sync<T: Send + Sync + 'static>() {}
106 assert_send_sync::<Key<super::family::Completion>>();
107};