Skip to main content

raw_btree/
item.rs

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	pub fn as_pair(&self) -> (&K, &V) {
23		(&self.key, &self.value)
24	}
25
26	pub fn into_pair(self) -> (K, V) {
27		(self.key, self.value)
28	}
29}
30
31impl<K, V> AsRef<Item<K, V>> for Item<K, V> {
32	fn as_ref(&self) -> &Self {
33		self
34	}
35}
36
37impl<K: PartialEq, V> PartialEq for Item<K, V> {
38	fn eq(&self, other: &Self) -> bool {
39		self.key == other.key
40	}
41}
42
43impl<K: Eq, V> Eq for Item<K, V> {}
44
45impl<K: PartialOrd, V> PartialOrd for Item<K, V> {
46	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
47		self.key.partial_cmp(&other.key)
48	}
49}
50
51impl<K: Ord, V> Ord for Item<K, V> {
52	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
53		self.key.cmp(&other.key)
54	}
55}
56
57impl<K: std::fmt::Display, V: std::fmt::Display> std::fmt::Display for Item<K, V> {
58	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59		write!(f, "({}, {})", self.key, self.value)
60	}
61}