Skip to content

Commit 27af6e3

Browse files
authored
Merge pull request rust-lang#41 from garro95/master
index() method for Entry and VacantEntry plus test coverage for Entry API
2 parents d74b294 + 32ce516 commit 27af6e3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ impl<'a, K, V, S> Entry<'a, K, V, S> {
484484
Entry::Vacant(ref entry) => entry.key(),
485485
}
486486
}
487+
488+
pub fn index(&self) -> usize {
489+
match *self {
490+
Entry::Occupied(ref entry) => entry.index(),
491+
Entry::Vacant(ref entry) => entry.index(),
492+
}
493+
}
487494
}
488495

489496
pub struct OccupiedEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
@@ -534,6 +541,7 @@ pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
534541
impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
535542
pub fn key(&self) -> &K { &self.key }
536543
pub fn into_key(self) -> K { self.key }
544+
pub fn index(&self) -> usize { self.map.len() }
537545
pub fn insert(self, value: V) -> &'a mut V {
538546
if self.map.size_class_is_64bit() {
539547
self.insert_impl::<u64>(value)
@@ -1733,4 +1741,27 @@ mod tests {
17331741
map.extend(vec![(5, 6)]);
17341742
assert_eq!(map.into_iter().collect::<Vec<_>>(), vec![(1, 2), (3, 4), (5, 6)]);
17351743
}
1744+
1745+
#[test]
1746+
fn entry() {
1747+
let mut map = OrderMap::new();
1748+
1749+
map.insert(1, "1");
1750+
map.insert(2, "2");
1751+
{
1752+
let e = map.entry(3);
1753+
assert_eq!(e.index(), 2);
1754+
let e = e.or_insert("3");
1755+
assert_eq!(e, &"3");
1756+
}
1757+
1758+
let e = map.entry(2);
1759+
assert_eq!(e.index(), 1);
1760+
assert_eq!(e.key(), &2);
1761+
match e {
1762+
Entry::Occupied(ref e) => assert_eq!(e.get(), &"2"),
1763+
Entry::Vacant(_) => panic!()
1764+
}
1765+
assert_eq!(e.or_insert("4"), &"2");
1766+
}
17361767
}

0 commit comments

Comments
 (0)