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
use std::{borrow::Borrow, cmp::Ordering};

#[derive(Debug)]
pub struct Item<K, V> {
	pub key: K,
	pub value: V,
}

impl<K, V> Item<K, V> {
	pub fn new(key: K, value: V) -> Self {
		Self { key, value }
	}

	pub fn key_cmp<Q>(&self, key: &Q) -> Ordering
	where
		K: Borrow<Q> + Ord,
		Q: Ord + ?Sized,
	{
		self.key.borrow().cmp(key)
	}
}

impl<K: PartialEq, V> PartialEq for Item<K, V> {
	fn eq(&self, other: &Self) -> bool {
		self.key == other.key
	}
}

impl<K: Eq, V> Eq for Item<K, V> {}

impl<K: PartialOrd, V> PartialOrd for Item<K, V> {
	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
		self.key.partial_cmp(&other.key)
	}
}

impl<K: Ord, V> Ord for Item<K, V> {
	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
		self.key.cmp(&other.key)
	}
}

impl<K: std::fmt::Display, V: std::fmt::Display> std::fmt::Display for Item<K, V> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "({}, {})", self.key, self.value)
	}
}