1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use serde::{Deserialize, Serialize};

/// A general data structure holding a key and value pair.
///
#[derive(Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KeyValuePair<K, V> {
    key: K,
    value: V,
}

impl<K: Copy, V> KeyValuePair<K, V> {
    /// Get the key.
    ///
    /// # Examples
    ///
    /// ~~~
    /// # use ichen_openprotocol::*;
    /// let kv = KeyValuePair::new("TheKey", 42.0);
    /// assert_eq!("TheKey", kv.key());
    /// ~~~
    pub fn key(&self) -> K {
        self.key
    }
}

impl<K, V: Copy> KeyValuePair<K, V> {
    /// Get the value.
    ///
    /// # Examples
    ///
    /// ~~~
    /// # use ichen_openprotocol::*;
    /// let kv = KeyValuePair::new("TheKey", 42.0);
    /// assert_eq!(42.0, kv.value());
    /// ~~~
    pub fn value(&self) -> V {
        self.value
    }
}

impl<K, V> KeyValuePair<K, V> {
    /// Get the key.
    ///
    /// # Examples
    ///
    /// ~~~
    /// # use ichen_openprotocol::*;
    /// let kv = KeyValuePair::new("TheKey", 42.0);
    /// assert_eq!("TheKey", *kv.key_ref());
    /// ~~~
    pub fn key_ref(&self) -> &K {
        &self.key
    }

    /// Get the value.
    ///
    /// # Examples
    ///
    /// ~~~
    /// # use ichen_openprotocol::*;
    /// let kv = KeyValuePair::new("TheKey", 42.0);
    /// assert_eq!(42.0, *kv.value_ref());
    /// ~~~
    pub fn value_ref(&self) -> &V {
        &self.value
    }

    /// Create a `KewValuePair`.
    ///
    /// # Examples
    ///
    /// ~~~
    /// # use ichen_openprotocol::*;
    /// let kv = KeyValuePair::new("TheKey", 42.0);
    /// assert_eq!("TheKey", kv.key());
    /// assert_eq!(42.0, kv.value());
    /// ~~~
    pub fn new(key: K, value: V) -> Self {
        Self { key, value }
    }
}