use std::{fmt, hash::Hash, marker::PhantomData};
use serde::{Deserialize, Serialize};
use super::{Family, HandlerKey};
#[derive(Serialize, Deserialize)]
#[serde(transparent, bound = "")]
pub struct Key<F: Family> {
raw: HandlerKey,
#[serde(skip)]
_family: PhantomData<fn() -> F>,
}
impl<F: Family> Key<F> {
#[track_caller]
pub const fn new_unchecked(raw: HandlerKey) -> Self {
Self {
raw,
_family: PhantomData,
}
}
pub const fn raw(&self) -> &HandlerKey {
&self.raw
}
pub fn into_raw(self) -> HandlerKey {
self.raw
}
pub fn as_str(&self) -> &str {
self.raw.as_str()
}
}
impl<F: Family> From<Key<F>> for HandlerKey {
fn from(key: Key<F>) -> Self {
key.raw
}
}
impl<F: Family> AsRef<HandlerKey> for Key<F> {
fn as_ref(&self) -> &HandlerKey {
&self.raw
}
}
impl<F: Family> Clone for Key<F> {
fn clone(&self) -> Self {
Self::new_unchecked(self.raw.clone())
}
}
impl<F: Family> PartialEq for Key<F> {
fn eq(&self, other: &Self) -> bool {
self.raw == other.raw
}
}
impl<F: Family> Eq for Key<F> {}
impl<F: Family> PartialOrd for Key<F> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<F: Family> Ord for Key<F> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.raw.cmp(&other.raw)
}
}
impl<F: Family> Hash for Key<F> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.raw.hash(state);
}
}
impl<F: Family> fmt::Debug for Key<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Key<{}>({:?})", F::FAMILY, self.raw.as_str())
}
}
impl<F: Family> fmt::Display for Key<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.raw, f)
}
}
const _: () = {
const fn assert_send_sync<T: Send + Sync + 'static>() {}
assert_send_sync::<Key<super::family::Completion>>();
};