abi_stable_derive/
ignored_wrapper.rs

1//! Wrapper type(s) where their value is ignored in comparisons .
2
3use std::{
4    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
5    fmt::{self, Debug, Display},
6    hash::{Hash, Hasher},
7    ops::{Deref, DerefMut},
8};
9
10/// Wrapper type used to ignore its contents in comparisons.
11#[repr(transparent)]
12#[derive(Default, Copy, Clone)]
13pub struct Ignored<T> {
14    pub value: T,
15}
16
17impl<T> Ignored<T> {
18    pub fn new(value: T) -> Self {
19        Self { value }
20    }
21}
22
23impl<T> From<T> for Ignored<T> {
24    fn from(value: T) -> Self {
25        Self { value }
26    }
27}
28
29impl<T> Deref for Ignored<T> {
30    type Target = T;
31
32    fn deref(&self) -> &Self::Target {
33        &self.value
34    }
35}
36
37impl<T> DerefMut for Ignored<T> {
38    fn deref_mut(&mut self) -> &mut Self::Target {
39        &mut self.value
40    }
41}
42
43impl<T> Display for Ignored<T>
44where
45    T: Display,
46{
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        Display::fmt(&**self, f)
49    }
50}
51
52impl<T> Debug for Ignored<T>
53where
54    T: Debug,
55{
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        Debug::fmt(&**self, f)
58    }
59}
60
61impl<T> Eq for Ignored<T> {}
62
63impl<T> PartialEq for Ignored<T> {
64    fn eq(&self, _other: &Self) -> bool {
65        true
66    }
67}
68
69impl<T> Ord for Ignored<T> {
70    fn cmp(&self, _other: &Self) -> Ordering {
71        Ordering::Equal
72    }
73}
74
75impl<T> PartialOrd for Ignored<T> {
76    fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
77        Some(Ordering::Equal)
78    }
79}
80
81impl<T> Hash for Ignored<T> {
82    fn hash<H>(&self, state: &mut H)
83    where
84        H: Hasher,
85    {
86        UnitType.hash(state)
87    }
88}
89
90#[derive(Hash)]
91struct UnitType;