1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/*!
Wrapper type(s) where their value is ignored in some trait impls .
*/

use std::{
    ops::{Deref,DerefMut},
    fmt::{self,Debug,Display},
    cmp::{Ordering,Eq,PartialEq,Ord,PartialOrd},
    hash::{Hash,Hasher},
};

/// Wrapper type used to ignore its contents in comparisons.
///
/// It also:
///
/// - replaces the hash of T with the hash of `()`.
///
#[repr(transparent)]
#[derive(Default,Copy,Clone,StableAbi)]
#[sabi(inside_abi_stable_crate)]
pub struct CmpIgnored<T>{
    pub value:T,
}


impl<T> CmpIgnored<T>{
    pub const fn new(value:T)->Self{
        Self{value}
    }
}


impl<T> From<T> for CmpIgnored<T>{
    fn from(value:T)->Self{
        Self{value}
    }
}


impl<T> Deref for CmpIgnored<T> {
    type Target=T;

    fn deref(&self)->&Self::Target{
        &self.value
    }
}

impl<T> DerefMut for CmpIgnored<T> {
    fn deref_mut(&mut self)->&mut Self::Target{
        &mut self.value
    }
}

impl<T> Display for CmpIgnored<T>
where
    T:Display,
{
    fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
        Display::fmt(&**self,f)
    }
}


impl<T> Debug for CmpIgnored<T>
where
    T:Debug,
{
    fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
        Debug::fmt(&**self,f)
    }
}

impl<T> Eq for CmpIgnored<T> {}


impl<T> PartialEq for CmpIgnored<T> {
    fn eq(&self, _other: &Self) -> bool{
        true
    }
}


impl<T> Ord for CmpIgnored<T>{
    fn cmp(&self, _other: &Self) -> Ordering{
        Ordering::Equal
    }
}


impl<T> PartialOrd for CmpIgnored<T>{
    fn partial_cmp(&self, _other: &Self) -> Option<Ordering>{
        Some(Ordering::Equal)
    }
}


impl<T> Hash for CmpIgnored<T>{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher
    {
        ().hash(state)
    }
}