1use ::core::borrow::Borrow;
2use ::core::fmt::Debug;
3use alloc::vec::Vec;
4
5use core::cmp::Ordering;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use crate::cdc::change::ChangeEvent;
10use crate::concurrent::set::BTreeSet;
11use crate::core::node::NodeLike;
12use crate::core::pair::Pair;
13
14use super::{MultiPairInsertHelper, MultiPairLike, MultiPairRemoveHelper};
15
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[derive(Debug, Default, Clone, Hash)]
18pub struct OrdMultiPair<K, V> {
19 pub key: K,
20 pub value: V,
21}
22
23impl<K, V> OrdMultiPair<K, V> {
24 pub fn new(key: K, value: V) -> Self {
25 Self { key, value }
26 }
27}
28
29impl<K: Ord, V: Ord> Eq for OrdMultiPair<K, V> {}
30
31impl<K: Ord, V: Ord> PartialEq<Self> for OrdMultiPair<K, V> {
32 fn eq(&self, other: &Self) -> bool {
33 self.key.eq(&other.key) && self.value.eq(&other.value)
34 }
35}
36
37impl<K: Ord, V: Ord> Ord for OrdMultiPair<K, V> {
38 fn cmp(&self, other: &Self) -> Ordering {
39 self.key.cmp(&other.key).then(self.value.cmp(&other.value))
40 }
41}
42
43impl<K: Ord, V: Ord> PartialOrd for OrdMultiPair<K, V> {
44 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45 Some(self.cmp(other))
46 }
47}
48
49impl<K, V> Borrow<K> for OrdMultiPair<K, V> {
50 fn borrow(&self) -> &K {
51 &self.key
52 }
53}
54
55impl<K: Ord, V: Ord> MultiPairLike<K, V> for OrdMultiPair<K, V> {
56 fn new(key: K, value: V) -> Self {
57 Self::new(key, value)
58 }
59
60 fn key(&self) -> &K {
61 &self.key
62 }
63
64 fn value(&self) -> &V {
65 &self.value
66 }
67}
68
69impl<K, V> From<Pair<K, V>> for OrdMultiPair<K, V> {
70 fn from(pair: Pair<K, V>) -> Self {
71 OrdMultiPair {
72 key: pair.key,
73 value: pair.value,
74 }
75 }
76}
77
78impl<K, V> From<OrdMultiPair<K, V>> for Pair<K, V> {
79 fn from(pair: OrdMultiPair<K, V>) -> Self {
80 Pair {
81 key: pair.key,
82 value: pair.value,
83 }
84 }
85}
86
87impl<K, V> From<OrdMultiPair<K, V>> for (K, V) {
88 fn from(pair: OrdMultiPair<K, V>) -> Self {
89 (pair.key, pair.value)
90 }
91}
92
93impl<K, V> MultiPairInsertHelper<K, V> for OrdMultiPair<K, V>
94where
95 K: Debug + Send + Ord + Clone + 'static,
96 V: Debug + Send + Ord + Clone + 'static,
97{
98 fn insert_into<Node>(set: &BTreeSet<Self, Node>, key: K, value: V) -> Option<(K, V)>
101 where
102 Self: Debug + Ord + Clone + Send + 'static,
103 Node: NodeLike<Self> + Send + 'static,
104 {
105 set.put_with(Self::new(key, value), Self::adopt_stored_identity)
106 .map(Into::into)
107 }
108
109 #[cfg(feature = "cdc")]
110 fn insert_cdc_into<Node>(set: &BTreeSet<Self, Node>, key: K, value: V) -> (Option<(K, V)>, Vec<ChangeEvent<Self>>)
111 where
112 Self: Debug + Ord + Clone + Send + 'static,
113 Node: NodeLike<Self> + Send + 'static,
114 {
115 let (replaced, events) = set.put_cdc_with(Self::new(key, value), Self::adopt_stored_identity);
116 (replaced.map(Into::into), events)
117 }
118}
119
120impl<K, V> MultiPairRemoveHelper<K, V> for OrdMultiPair<K, V>
121where
122 K: Debug + Send + Ord + Clone + 'static,
123 V: Debug + Send + Ord + Clone + 'static,
124{
125 fn remove_from<Node>(set: &BTreeSet<Self, Node>, key: &K, value: &V) -> Option<(K, V)>
126 where
127 Self: Ord + Clone + 'static,
128 Node: NodeLike<Self> + Send + 'static,
129 {
130 let pair_to_remove = OrdMultiPair::new(key.clone(), value.clone());
131 set.remove(&pair_to_remove).map(Into::into)
132 }
133
134 fn remove_cdc_from<Node>(set: &BTreeSet<Self, Node>, key: &K, value: &V) -> (Option<(K, V)>, Vec<ChangeEvent<Self>>)
135 where
136 Self: Ord + Clone + 'static,
137 Node: NodeLike<Self> + Send + 'static,
138 {
139 let pair_to_remove = OrdMultiPair::new(key.clone(), value.clone());
140 let (res, evs) = set.remove_cdc(&pair_to_remove);
141
142 (res.map(Into::into), evs)
143 }
144}