use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;
pub struct Value<T, S> {
value: T,
strategy: PhantomData<fn() -> S>,
}
impl<T, S> Value<T, S> {
#[inline(always)]
pub fn new(value: T) -> Self {
Self {
value,
strategy: PhantomData,
}
}
#[inline(always)]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T, S> Deref for Value<T, S> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T, S> DerefMut for Value<T, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
impl<T: Clone, S> Clone for Value<T, S> {
fn clone(&self) -> Self {
Self::new(self.value.clone())
}
}
impl<T: Copy, S> Copy for Value<T, S> {}
impl<T: PartialEq, S> PartialEq for Value<T, S> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T: Eq, S> Eq for Value<T, S> {}
impl<T: PartialOrd, S> PartialOrd for Value<T, S> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}
}
impl<T: Ord, S> Ord for Value<T, S> {
fn cmp(&self, other: &Self) -> Ordering {
self.value.cmp(&other.value)
}
}
impl<T: fmt::Debug, S> fmt::Debug for Value<T, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.value, f)
}
}
impl<T: fmt::Display, S> fmt::Display for Value<T, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f)
}
}
impl<T, S: HashStrategy<T>> Hash for Value<T, S> {
fn hash<H: Hasher>(&self, state: &mut H) {
S::hash(&self.value, state);
}
}
#[doc(hidden)]
pub trait HashStrategy<T> {
fn hash<H: Hasher>(value: &T, state: &mut H);
}