concision_neural/types/
key_value.rs

1/*
2    appellation: key_value <module>
3    authors: @FL03
4*/
5
6/// The [`KeyValue`] type is used to generically represent a simple key-value pair within a
7/// store.
8#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10pub struct KeyValue<K = String, V = f64> {
11    pub key: K,
12    pub value: V,
13}
14
15impl<K, V> KeyValue<K, V> {
16    pub const fn new(key: K, value: V) -> Self {
17        Self { key, value }
18    }
19    /// returns a new [`KeyValue`] from the given key, using the logical default for the value
20    pub fn from_key(key: K) -> Self
21    where
22        V: Default,
23    {
24        Self {
25            key,
26            value: V::default(),
27        }
28    }
29    /// returns a new [`KeyValue`] from the given value, using the logical default for the key
30    pub fn from_value(value: V) -> Self
31    where
32        K: Default,
33    {
34        Self {
35            key: K::default(),
36            value,
37        }
38    }
39    /// returns an immutable reference to the key
40    pub const fn key(&self) -> &K {
41        &self.key
42    }
43    /// returns a mutable reference to the key
44    pub const fn key_mut(&mut self) -> &mut K {
45        &mut self.key
46    }
47    /// returns an immutable reference to the value
48    pub const fn value(&self) -> &V {
49        &self.value
50    }
51    /// returns a mutable reference to the value
52    pub const fn value_mut(&mut self) -> &mut V {
53        &mut self.value
54    }
55    /// update the current key and return a mutable reference to self
56    pub fn set_key(&mut self, key: K) -> &mut Self {
57        self.key = key;
58        self
59    }
60    /// update the current value and return a mutable reference to self
61    pub fn set_value(&mut self, value: V) -> &mut Self {
62        self.value = value;
63        self
64    }
65    /// consumes the current instance to create another with the given key
66    pub fn with_key<K2>(self, key: K2) -> KeyValue<K2, V> {
67        KeyValue {
68            key,
69            value: self.value,
70        }
71    }
72    /// consumes the current instance to create another with the given value
73    pub fn with_value<V2>(self, value: V2) -> KeyValue<K, V2> {
74        KeyValue {
75            key: self.key,
76            value,
77        }
78    }
79    /// [`replace`](core::mem::replace) the current value and return the old value
80    pub const fn replace_value(&mut self, value: V) -> V {
81        core::mem::replace(self.value_mut(), value)
82    }
83    /// [`swap`](core::mem::swap) the current value with another in the given instance
84    pub const fn swap_value(&mut self, other: &mut KeyValue<K, V>) {
85        core::mem::swap(self.value_mut(), other.value_mut())
86    }
87    /// [`take`](core::mem::take) the current value and return it, replacing it with the
88    /// logical default
89    pub fn take_value(&mut self) -> V
90    where
91        V: Default,
92    {
93        core::mem::take(self.value_mut())
94    }
95    /// returns a new instance of the [`KeyValue`] with mutable references to the value and a
96    /// reference to the key
97    pub fn entry(&mut self) -> KeyValue<&K, &mut V> {
98        KeyValue {
99            key: &self.key,
100            value: &mut self.value,
101        }
102    }
103    /// returns a new instance of the [`KeyValue`] with references to the key and value
104    pub const fn view(&self) -> KeyValue<&K, &V> {
105        KeyValue {
106            key: self.key(),
107            value: self.value(),
108        }
109    }
110    /// returns a new instance of the [`KeyValue`] with mutable references to the current key
111    /// and value
112    pub const fn view_mut(&mut self) -> KeyValue<&mut K, &mut V> {
113        KeyValue {
114            key: &mut self.key,
115            value: &mut self.value,
116        }
117    }
118}
119
120impl<K, V> KeyValue<&K, &V> {
121    /// returns a new [`KeyValue`] instance with clones of the current key and value
122    pub fn cloned(&self) -> KeyValue<K, V>
123    where
124        K: Clone,
125        V: Clone,
126    {
127        KeyValue {
128            key: self.key.clone(),
129            value: self.value.clone(),
130        }
131    }
132    /// returns a new [`KeyValue`] instance with copies of the current key and value
133    pub fn copied(&self) -> KeyValue<K, V>
134    where
135        K: Copy,
136        V: Copy,
137    {
138        KeyValue {
139            key: *self.key,
140            value: *self.value,
141        }
142    }
143}
144
145impl<K, V> KeyValue<&K, &mut V> {
146    /// returns a new [`KeyValue`] instance with clones of the current key and value
147    pub fn cloned(&self) -> KeyValue<K, V>
148    where
149        K: Clone,
150        V: Clone,
151    {
152        KeyValue {
153            key: self.key.clone(),
154            value: self.value.clone(),
155        }
156    }
157    /// returns a new [`KeyValue`] instance with copies of the current key and value
158    pub fn copied(&self) -> KeyValue<K, V>
159    where
160        K: Copy,
161        V: Copy,
162    {
163        KeyValue {
164            key: *self.key,
165            value: *self.value,
166        }
167    }
168}
169
170impl<K, V> KeyValue<&mut K, &mut V> {
171    /// returns a new [`KeyValue`] instance with clones of the current key and value
172    pub fn cloned(&self) -> KeyValue<K, V>
173    where
174        K: Clone,
175        V: Clone,
176    {
177        KeyValue {
178            key: self.key.clone(),
179            value: self.value.clone(),
180        }
181    }
182    /// returns a new [`KeyValue`] instance with copies of the current key and value
183    pub fn copied(&self) -> KeyValue<K, V>
184    where
185        K: Copy,
186        V: Copy,
187    {
188        KeyValue {
189            key: *self.key,
190            value: *self.value,
191        }
192    }
193}
194
195impl<K, V> core::fmt::Display for KeyValue<K, V>
196where
197    K: core::fmt::Display,
198    V: core::fmt::Display,
199{
200    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
201        write!(f, "{k}: {v}", k = self.key(), v = self.value())
202    }
203}