Skip to content

Commit 3307a2f

Browse files
committed
feat(map): add to_front & to_back methods
1 parent a2f5f27 commit 3307a2f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/map.rs

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

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

0 commit comments

Comments
 (0)