use std::convert::TryFrom;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Default, Hash, Serialize, Deserialize)]
#[serde(into = "String", try_from = "String")]
pub struct CacheId<T>(T)
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display;
impl<T> CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
pub fn value(self) -> T {
self.0
}
}
impl<T> AsRef<T> for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Deref for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> fmt::Display for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T> From<CacheId<T>> for String
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
fn from(id: CacheId<T>) -> Self {
id.to_string()
}
}
impl<T> TryFrom<String> for CacheId<T>
where
T: fmt::Display + FromStr + Clone,
T::Err: fmt::Display,
{
type Error = T::Err;
fn try_from(s: String) -> Result<Self, Self::Error> {
Ok(CacheId(s.parse()?))
}
}