ichen_openprotocol/
key_value_pair.rs

1use serde::{Deserialize, Serialize};
2
3/// A general data structure holding a key and value pair.
4///
5#[derive(Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "camelCase")]
7pub struct KeyValuePair<K, V> {
8    key: K,
9    value: V,
10}
11
12impl<K: Copy, V> KeyValuePair<K, V> {
13    /// Get the key.
14    ///
15    /// # Examples
16    ///
17    /// ~~~
18    /// # use ichen_openprotocol::*;
19    /// let kv = KeyValuePair::new("TheKey", 42.0);
20    /// assert_eq!("TheKey", kv.key());
21    /// ~~~
22    pub fn key(&self) -> K {
23        self.key
24    }
25}
26
27impl<K, V: Copy> KeyValuePair<K, V> {
28    /// Get the value.
29    ///
30    /// # Examples
31    ///
32    /// ~~~
33    /// # use ichen_openprotocol::*;
34    /// let kv = KeyValuePair::new("TheKey", 42.0);
35    /// assert_eq!(42.0, kv.value());
36    /// ~~~
37    pub fn value(&self) -> V {
38        self.value
39    }
40}
41
42impl<K, V> KeyValuePair<K, V> {
43    /// Get the key.
44    ///
45    /// # Examples
46    ///
47    /// ~~~
48    /// # use ichen_openprotocol::*;
49    /// let kv = KeyValuePair::new("TheKey", 42.0);
50    /// assert_eq!("TheKey", *kv.key_ref());
51    /// ~~~
52    pub fn key_ref(&self) -> &K {
53        &self.key
54    }
55
56    /// Get the value.
57    ///
58    /// # Examples
59    ///
60    /// ~~~
61    /// # use ichen_openprotocol::*;
62    /// let kv = KeyValuePair::new("TheKey", 42.0);
63    /// assert_eq!(42.0, *kv.value_ref());
64    /// ~~~
65    pub fn value_ref(&self) -> &V {
66        &self.value
67    }
68
69    /// Create a `KewValuePair`.
70    ///
71    /// # Examples
72    ///
73    /// ~~~
74    /// # use ichen_openprotocol::*;
75    /// let kv = KeyValuePair::new("TheKey", 42.0);
76    /// assert_eq!("TheKey", kv.key());
77    /// assert_eq!(42.0, kv.value());
78    /// ~~~
79    pub fn new(key: K, value: V) -> Self {
80        Self { key, value }
81    }
82}