Skip to content

Commit 9b0ee5d

Browse files
committed
Add LinkedHashMap::to_front and LinkedHashMap::to_back entries.
1 parent c59f5c1 commit 9b0ee5d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/linked_hash_map.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,40 @@ where
441441
}
442442
}
443443
}
444+
445+
/// If an entry with this key exists, move it to the front of the list and return a reference to
446+
/// the value.
447+
#[inline]
448+
pub fn to_front<Q>(&mut self, k: &Q) -> Option<&mut V>
449+
where
450+
K: Borrow<Q>,
451+
Q: Hash + Eq + ?Sized,
452+
{
453+
match self.raw_entry_mut().from_key(k) {
454+
RawEntryMut::Occupied(mut occupied) => {
455+
occupied.to_front();
456+
Some(occupied.into_mut())
457+
}
458+
RawEntryMut::Vacant(_) => None,
459+
}
460+
}
461+
462+
/// If an entry with this key exists, move it to the back of the list and return a reference to
463+
/// the value.
464+
#[inline]
465+
pub fn to_back<Q>(&mut self, k: &Q) -> Option<&mut V>
466+
where
467+
K: Borrow<Q>,
468+
Q: Hash + Eq + ?Sized,
469+
{
470+
match self.raw_entry_mut().from_key(k) {
471+
RawEntryMut::Occupied(mut occupied) => {
472+
occupied.to_back();
473+
Some(occupied.into_mut())
474+
}
475+
RawEntryMut::Vacant(_) => None,
476+
}
477+
}
444478
}
445479

446480
impl<K, V, S> LinkedHashMap<K, V, S>

0 commit comments

Comments
 (0)