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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use crate::Meta;
use std::{
	borrow,
	collections::{BTreeSet, HashSet},
	fmt,
	hash::Hash,
	ops,
};

mod eq;
mod hash;
mod ord;
mod partial_eq;
mod partial_ord;

pub use eq::*;
pub use hash::*;
pub use ord::*;
pub use partial_eq::*;
pub use partial_ord::*;

/// Type that can be stripped of its metadata.
pub trait Strip {
	type Stripped;

	fn strip(self) -> Self::Stripped;
}

impl<T: Strip, M> Strip for Meta<T, M> {
	type Stripped = T::Stripped;

	fn strip(self) -> Self::Stripped {
		self.0.strip()
	}
}

impl<T: Strip> Strip for Box<T> {
	type Stripped = Box<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		Box::new((*self).strip())
	}
}

impl<T: Strip> Strip for Option<T> {
	type Stripped = Option<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.map(T::strip)
	}
}

impl<T: Strip> Strip for Vec<T> {
	type Stripped = Vec<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.into_iter().map(T::strip).collect()
	}
}

impl<T: Strip> Strip for HashSet<T>
where
	T::Stripped: Hash + Eq,
{
	type Stripped = HashSet<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.into_iter().map(T::strip).collect()
	}
}

#[cfg(feature = "hashbrown")]
impl<T: Strip> Strip for hashbrown::HashSet<T>
where
	T::Stripped: Hash + Eq,
{
	type Stripped = hashbrown::HashSet<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.into_iter().map(T::strip).collect()
	}
}

#[cfg(feature = "indexmap")]
impl<T: Strip> Strip for indexmap::IndexSet<T>
where
	T::Stripped: Hash + Eq,
{
	type Stripped = indexmap::IndexSet<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.into_iter().map(T::strip).collect()
	}
}

impl<T: Strip> Strip for BTreeSet<T>
where
	T::Stripped: Ord,
{
	type Stripped = BTreeSet<T::Stripped>;

	fn strip(self) -> Self::Stripped {
		self.into_iter().map(T::strip).collect()
	}
}

pub trait BorrowStripped {
	fn stripped(&self) -> &Stripped<Self>;
}

impl<T> BorrowStripped for T {
	fn stripped(&self) -> &Stripped<Self> {
		unsafe { core::mem::transmute(self) }
	}
}

/// Wrapper to consider values without metadata.
///
/// This wrapper can be used in combination with the `Stripped*` traits such
/// as `StrippedPartialEq` to access and compare values ignoring code mapping
/// metadata.
///
/// An owned value can directly be wrapped.
/// Any reference `&T` can be safely converted into `&Stripped<T>` using the
/// [`BorrowStripped::stripped`] method.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Stripped<T: ?Sized>(pub T);

impl<T> Stripped<T> {
	#[inline]
	pub fn unwrap(self) -> T {
		self.0
	}
}

impl<T: fmt::Display> fmt::Display for Stripped<T> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl<T> ops::Deref for Stripped<T> {
	type Target = T;

	fn deref(&self) -> &T {
		&self.0
	}
}

impl<T> ops::DerefMut for Stripped<T> {
	fn deref_mut(&mut self) -> &mut T {
		&mut self.0
	}
}

impl<T> borrow::Borrow<T> for Stripped<T> {
	fn borrow(&self) -> &T {
		&self.0
	}
}

impl<T> borrow::BorrowMut<T> for Stripped<T> {
	fn borrow_mut(&mut self) -> &mut T {
		&mut self.0
	}
}

impl<T> AsRef<T> for Stripped<T> {
	fn as_ref(&self) -> &T {
		&self.0
	}
}

impl<T> AsMut<T> for Stripped<T> {
	fn as_mut(&mut self) -> &mut T {
		&mut self.0
	}
}

macro_rules! primitive {
	($($id:ident),*) => {
		$(
			impl Strip for $id {
				type Stripped = Self;

				fn strip(self) -> Self::Stripped {
					self
				}
			}

			impl<U> StrippedPartialEq<U> for $id where $id: PartialEq<U> {
				fn stripped_eq(&self, other: &U) -> bool {
					self == other
				}
			}

			impl StrippedEq for $id {}

			impl<U> StrippedPartialOrd<U> for $id where $id: PartialOrd<U> {
				fn stripped_partial_cmp(&self, other: &U) -> Option<std::cmp::Ordering> {
					self.partial_cmp(other)
				}
			}

			impl StrippedOrd for $id {
				fn stripped_cmp(&self, other: &Self) -> std::cmp::Ordering {
					self.cmp(other)
				}
			}

			impl StrippedHash for $id {
				fn stripped_hash<H: std::hash::Hasher>(&self, state: &mut H) {
					self.hash(state)
				}
			}
		)*
	};
}

primitive! {
	bool,
	u8,
	u16,
	u32,
	u64,
	i8,
	i16,
	i32,
	i64,
	usize,
	isize,
	char,
	String
}

macro_rules! float {
	($($id:ident),*) => {
		$(
			impl Strip for $id {
				type Stripped = Self;

				fn strip(self) -> Self::Stripped {
					self
				}
			}

			impl<U> StrippedPartialEq<U> for $id where $id: PartialEq<U> {
				fn stripped_eq(&self, other: &U) -> bool {
					self == other
				}
			}

			impl<U> StrippedPartialOrd<U> for $id where $id: PartialOrd<U> {
				fn stripped_partial_cmp(&self, other: &U) -> Option<std::cmp::Ordering> {
					self.partial_cmp(other)
				}
			}
		)*
	};
}

float! {
	f32,
	f64
}