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
use super::{Stripped, StrippedPartialEq};
use crate::Loc;
use std::cmp::{Ordering, PartialOrd};

/// Defines the partial ordering of located values
/// without considering locations.
pub trait StrippedPartialOrd<U: ?Sized = Self>: StrippedPartialEq<U> {
	fn stripped_partial_cmp(&self, other: &U) -> Option<Ordering>;
}

impl<T: StrippedPartialOrd<U>, U> PartialOrd<Stripped<U>> for Stripped<T> {
	fn partial_cmp(&self, other: &Stripped<U>) -> Option<Ordering> {
		self.0.stripped_partial_cmp(&other.0)
	}
}

impl<'u, 't, U, T: StrippedPartialOrd<U>> StrippedPartialOrd<&'u U> for &'t T {
	fn stripped_partial_cmp(&self, other: &&'u U) -> Option<Ordering> {
		T::stripped_partial_cmp(*self, *other)
	}
}

impl<U, G, P, T: StrippedPartialOrd<U>, F, S> StrippedPartialOrd<Loc<U, G, P>> for Loc<T, F, S> {
	fn stripped_partial_cmp(&self, other: &Loc<U, G, P>) -> Option<Ordering> {
		self.value().stripped_partial_cmp(other.value())
	}
}

impl<T: StrippedPartialOrd<U>, U> StrippedPartialOrd<Box<U>> for Box<T> {
	fn stripped_partial_cmp(&self, other: &Box<U>) -> Option<Ordering> {
		(**self).stripped_partial_cmp(&**other)
	}
}

impl<T: StrippedPartialOrd<U>, U> StrippedPartialOrd<Option<U>> for Option<T> {
	fn stripped_partial_cmp(&self, other: &Option<U>) -> Option<Ordering> {
		match (self, other) {
			(None, None) => Some(Ordering::Equal),
			(None, Some(_)) => Some(Ordering::Less),
			(Some(_), None) => Some(Ordering::Greater),
			(Some(a), Some(b)) => a.stripped_partial_cmp(b),
		}
	}
}

impl<T: StrippedPartialOrd<U>, U> StrippedPartialOrd<Vec<U>> for Vec<T> {
	fn stripped_partial_cmp(&self, other: &Vec<U>) -> Option<Ordering> {
		let mut self_iter = self.iter();
		let mut other_iter = other.iter();

		loop {
			match (self_iter.next(), other_iter.next()) {
				(Some(a), Some(b)) => match a.stripped_partial_cmp(b) {
					Some(Ordering::Equal) => (),
					cmp => break cmp,
				},
				(None, Some(_)) => break Some(Ordering::Less),
				(Some(_), None) => break Some(Ordering::Greater),
				(None, None) => break Some(Ordering::Equal),
			}
		}
	}
}