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
83
use std::fmt::Debug;

pub trait Entry<K, V> {
    fn key(&self) -> &K;
    fn value(&self) -> &V;
}

pub trait EntryMut<K, V> {
    fn key_mut(&mut self) -> &mut K;
    fn value_mut(&mut self) -> &mut V;
}

pub trait IntoEntry<K, V> {
    fn into_key(self) -> K;
    fn into_value(self) -> V;
}

pub trait EntrySet<'a, K, V>: Entry<K, V> {
    fn set(&'a self) -> &'a (K, V);
}

pub trait IntoEntrySet<K, V>: IntoEntry<K, V> {
    fn into_set(self) -> (K, V);
}

/** Basic `Entry` for each `HashMap` implementation */
#[derive(Debug, Default)]
pub struct RawEntry<K, V>((K, V));

impl<K, V> RawEntry<K, V> {
    /** Create a new `Entry` with the specified `key` and `value` */
    pub fn new(key: K, value: V) -> Self {
        RawEntry((key, value))
    }
}

impl<K, V> Entry<K, V> for RawEntry<K, V> {
    /** Return a reference of the key */
    fn key(&self) -> &K {
        &self.0 .0
    }

    /** Return a reference of the value */
    fn value(&self) -> &V {
        &self.0 .1
    }
}

impl<K, V> EntryMut<K, V> for RawEntry<K, V> {
    /** Return a mutable reference of the key */
    fn key_mut(&mut self) -> &mut K {
        &mut self.0 .0
    }

    /** Return a mutable reference of the value */
    fn value_mut(&mut self) -> &mut V {
        &mut self.0 .1
    }
}

impl<'a, K, V> EntrySet<'a, K, V> for RawEntry<K, V> {
    fn set(&'a self) -> &'a (K, V) {
        &self.0
    }
}

impl<K, V> IntoEntry<K, V> for RawEntry<K, V> {
    /** Return the original key, consuming the entry */
    fn into_key(self) -> K {
        self.0 .0
    }

    /** Return the original value, consuming the entry */
    fn into_value(self) -> V {
        self.0 .1
    }
}

impl<K, V> IntoEntrySet<K, V> for RawEntry<K, V> {
    fn into_set(self) -> (K, V) {
        (self.0 .0, self.0 .1)
    }
}