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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use super::Index;
use std::hash::{BuildHasher, Hash};
use std::mem;
use std::ops::{Deref, DerefMut};
use {Error, NonEmptyIndexMap};

/// An occupied entry.
pub struct Occupied<'a, K: 'a, V: 'a, S: 'a> {
    pub(super) key: K,
    pub(super) index: Index,
    pub(super) map: &'a mut NonEmptyIndexMap<K, V, S>,
}

/// A vacant (empty) entry.
pub struct Vacant<'a, K: 'a, V: 'a, S: 'a> {
    pub(super) key: K,
    pub(super) map: &'a mut NonEmptyIndexMap<K, V, S>,
}

/// A map's entry.
pub enum Entry<'a, K: 'a, V: 'a, S: 'a> {
    /// An occupied entry.
    Occupied(Occupied<'a, K, V, S>),
    /// A vacant (empty) entry.
    Vacant(Vacant<'a, K, V, S>),
}

impl<'a, K: 'a, V: 'a, S: 'a> Deref for Occupied<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    type Target = V;

    fn deref(&self) -> &V {
        self.map.get_entry(self.index)
    }
}

impl<'a, K: 'a, V: 'a, S: 'a> DerefMut for Occupied<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    fn deref_mut(&mut self) -> &mut V {
        self.map.get_entry_mut(self.index)
    }
}

impl<'a, K: 'a, V: 'a, S: 'a> Occupied<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    /// Removes the entry from the map.
    ///
    /// Will fail on an attempt to remove the last element from the map.
    pub fn remove_entry(self) -> Result<(K, V), Error> {
        let key = self.key;
        let map = self.map;
        map.remove_entry(&key)
            .map(|x| x.expect("The entry exists for sure"))
    }

    /// Replace the entry's value with a given one, returning the old one.
    pub fn replace(&mut self, new_value: V) -> V {
        let old = self.map.get_entry_mut(self.index);
        mem::replace(old, new_value)
    }

    pub fn into_value(self) -> &'a mut V {
        let idx = self.index;
        self.map.get_entry_mut(idx)
    }
}

impl<'a, K: 'a, V: 'a, S: 'a> Vacant<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    pub fn insert(self, value: V) -> &'a mut V {
        let map = self.map;
        let key = self.key;
        match map.get_rest_mut().entry(key) {
            ::indexmap::map::Entry::Occupied(_) => panic!("The entry exists for sure"),
            ::indexmap::map::Entry::Vacant(entry) => entry.insert(value),
        }
    }
}

impl<'a, K: 'a, V: 'a, S: 'a> Entry<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    /// Returns a mutable reference to a value if the entry exists, or creates a new entry using a
    /// given closure if it doesn't exist yet and returns a reference to a newly created entry's
    /// value.
    pub fn or_insert(self, value: V) -> &'a mut V {
        match self {
            Entry::Occupied(x) => x.into_value(),
            Entry::Vacant(v) => v.insert(value),
        }
    }

    /// Returns a mutable reference to a value if the entry exists, or creates a new entry with a
    /// given value if it doesn't exist yet and returns a reference to a newly created entry's
    /// value.
    pub fn or_insert_with<F>(self, f: F) -> &'a mut V
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(x) => x.into_value(),
            Entry::Vacant(v) => v.insert(f()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_occupied_one() {
        let mut map = NonEmptyIndexMap::new(1, 2);
        let entry = map.entry(1);
        let occupied = match entry {
            Entry::Occupied(x) => x,
            Entry::Vacant(_) => panic!("Expected an occupied entry"),
        };
        assert_eq!(Err(Error::EmptyCollection), occupied.remove_entry());
    }

    #[test]
    fn test_occupied() {
        let mut map = NonEmptyIndexMap::from_item_and_iterator(1, 2, vec![(3, 4), (5, 6), (7, 8)]);
        {
            let entry = map.entry(1);
            let occupied = match entry {
                Entry::Occupied(x) => x,
                Entry::Vacant(_) => panic!("Expected an occupied entry"),
            };
            assert_eq!(Ok((1, 2)), occupied.remove_entry());
        }
        assert_eq!(3, map.len());
        {
            let entry = map.entry(5);
            match entry {
                Entry::Occupied(x) => x,
                Entry::Vacant(_) => panic!("Expected an occupied entry"),
            };
        }
        assert_eq!(3, map.len());
    }

    #[test]
    fn test_vacant() {
        let mut map = NonEmptyIndexMap::new(1, 2);
        {
            let entry = map.entry(2);
            let vacant = match entry {
                Entry::Vacant(x) => x,
                Entry::Occupied(_) => panic!("Expected a vacant entry"),
            };
            assert_eq!(&3, vacant.insert(3));
        }
        assert_eq!(Some(&3), map.get(&2));
    }

    #[test]
    fn test_or_insert() {
        let mut map = NonEmptyIndexMap::new(1, 2);
        {
            let entry = map.entry(1);
            assert_eq!(&2, entry.or_insert(3));
        }
        assert_eq!(Some(&2), map.get(&1));
        assert_eq!(1, map.len());
        {
            let entry = map.entry(2);
            assert_eq!(&3, entry.or_insert(3));
        }
        assert_eq!(Some(&3), map.get(&2));
        assert_eq!(2, map.len());
    }

    #[test]
    fn test_or_insert_with() {
        let mut map = NonEmptyIndexMap::new(1, 2);
        {
            let entry = map.entry(1);
            assert_eq!(&2, entry.or_insert_with(|| 3));
        }
        assert_eq!(Some(&2), map.get(&1));
        assert_eq!(1, map.len());
        {
            let entry = map.entry(2);
            assert_eq!(&3, entry.or_insert_with(|| 3));
        }
        assert_eq!(Some(&3), map.get(&2));
        assert_eq!(2, map.len());
    }
}