1use std::{borrow::Borrow, cmp::Ordering};
2
3#[derive(Debug, Clone)]
4pub struct Item<K, V> {
5 pub key: K,
6 pub value: V,
7}
8
9impl<K, V> Item<K, V> {
10 pub fn new(key: K, value: V) -> Self {
11 Self { key, value }
12 }
13
14 pub fn key_cmp<Q>(&self, key: &Q) -> Ordering
15 where
16 K: Borrow<Q> + Ord,
17 Q: Ord + ?Sized,
18 {
19 self.key.borrow().cmp(key)
20 }
21}
22
23impl<K: PartialEq, V> PartialEq for Item<K, V> {
24 fn eq(&self, other: &Self) -> bool {
25 self.key == other.key
26 }
27}
28
29impl<K: Eq, V> Eq for Item<K, V> {}
30
31impl<K: PartialOrd, V> PartialOrd for Item<K, V> {
32 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
33 self.key.partial_cmp(&other.key)
34 }
35}
36
37impl<K: Ord, V> Ord for Item<K, V> {
38 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
39 self.key.cmp(&other.key)
40 }
41}
42
43impl<K: std::fmt::Display, V: std::fmt::Display> std::fmt::Display for Item<K, V> {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "({}, {})", self.key, self.value)
46 }
47}