use std::{any, fmt, hash, marker::PhantomData};
pub struct Typed<T, U> {
value: T,
_type: PhantomData<U>,
}
impl<T, U> Typed<T, U> {
pub(crate) fn new(value: T) -> Self {
Self {
value,
_type: PhantomData,
}
}
pub fn value(&self) -> &T {
&self.value
}
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: AsRef<V>, U, V: ?Sized> AsRef<V> for Typed<T, U> {
fn as_ref(&self) -> &V {
self.value.as_ref()
}
}
impl<T: Clone, U> Clone for Typed<T, U> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
_type: self._type,
}
}
}
impl<T: Copy, U> Copy for Typed<T, U> {}
impl<T: fmt::Debug, U> fmt::Debug for Typed<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(any::type_name::<U>())
.field(&self.value)
.finish()
}
}
impl<T: fmt::Display, U> fmt::Display for Typed<T, U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f)
}
}
impl<T: PartialEq, U> PartialEq for Typed<T, U> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T: Eq, U> Eq for Typed<T, U> {}
impl<T: hash::Hash, U> hash::Hash for Typed<T, U> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
pub type Anchor<T> = Typed<u32, T>;