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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
//! Wrapper type(s) where their value is ignored in some trait impls .
use std::{
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
fmt::{self, Debug, Display},
hash::{Hash, Hasher},
ops::{Deref, DerefMut},
};
/// Wrapper type used to ignore its contents in comparisons.
///
/// Use this if you want to derive trait while ignoring the contents of fields in the
/// `PartialEq`/`Eq`/`PartialOrd`/`Ord`/`Hash` traits.
///
/// It also replaces the hash of T with the hash of `()`.
///
/// # Example
///
/// This example defines a struct with a `CmpIgnored` field.
///
/// ```
/// use abi_stable::sabi_types::CmpIgnored;
///
/// use std::collections::HashSet;
///
/// #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// pub struct User {
/// name: String,
/// surname: String,
/// alt_name: CmpIgnored<String>,
/// }
///
/// let a = User {
/// name: "J__n".to_string(),
/// surname: "E____t".to_string(),
/// alt_name: "Z______l P______d".to_string().into(),
/// };
///
/// let b = User {
/// name: "J__n".to_string(),
/// surname: "E____t".to_string(),
/// alt_name: "H___ of B_____".to_string().into(),
/// };
///
/// assert_eq!(a, b);
///
/// let mut map = HashSet::new();
///
/// map.replace(a.clone());
/// assert_eq!(
/// map.replace(b.clone()).unwrap().alt_name.as_str(),
/// "Z______l P______d"
/// );
///
/// assert_eq!(map.len(), 1);
/// assert_eq!(map.get(&a).unwrap().alt_name.as_str(), "H___ of B_____");
///
/// ```
///
#[repr(transparent)]
#[derive(Default, Copy, Clone, StableAbi)]
pub struct CmpIgnored<T> {
///
pub value: T,
}
impl<T> CmpIgnored<T> {
/// Constructs a CmpIgnored.
///
/// # Example
///
/// ```
/// use abi_stable::sabi_types::CmpIgnored;
///
/// let val = CmpIgnored::new(100);
///
/// ```
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,
{
Unit.hash(state)
}
}
#[derive(Hash)]
struct Unit;