use std::{any::Any, hash::{Hash, Hasher}};
pub trait DynHash
{
fn dyn_hash(&self, state: &mut dyn Hasher);
}
pub trait DynPartialEq
{
fn dyn_eq(&self, other: &dyn Any) -> bool;
}
pub struct DynHashAdapter<T>
where T: DynHash + DynPartialEq
{
dyn_hashable_object: T
}
impl<T> DynHashAdapter<T>
where T: DynHash + DynPartialEq
{
pub fn new(dyn_hashable_object: T) -> Self
{
Self
{
dyn_hashable_object
}
}
pub fn object_ref(&self) -> &T
{
&self.dyn_hashable_object
}
pub fn object_mut(&mut self) -> &mut T
{
&mut self.dyn_hashable_object
}
pub fn take(self) -> T
{
self.dyn_hashable_object
}
}
impl<T> Hash for DynHashAdapter<T>
where T: DynHash + DynPartialEq
{
fn hash<H: Hasher>(&self, state: &mut H)
{
self.dyn_hashable_object.dyn_hash(state);
}
}
impl<T> PartialEq for DynHashAdapter<T>
where T: DynHash + DynPartialEq + 'static
{
fn eq(&self, other: &Self) -> bool
{
self.dyn_hashable_object.dyn_eq(other.object_ref())
}
}
pub struct DynPartialEqAdapter<T>
where T: DynPartialEq
{
dyn_partial_eq_object: T
}
impl<T> DynPartialEqAdapter<T>
where T: DynPartialEq
{
pub fn new(dyn_partial_eq_object: T) -> Self
{
Self
{
dyn_partial_eq_object
}
}
pub fn object_ref(&self) -> &T
{
&self.dyn_partial_eq_object
}
pub fn object_mut(&mut self) -> &mut T
{
&mut self.dyn_partial_eq_object
}
pub fn take(self) -> T
{
self.dyn_partial_eq_object
}
}
impl<T> PartialEq for DynPartialEqAdapter<T>
where T: DynPartialEq + 'static
{
fn eq(&self, other: &Self) -> bool
{
self.dyn_partial_eq_object.dyn_eq(other.object_ref())
}
}