Skip to content

Commit

Permalink
feat(collections/blazemap): add must_use attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Apr 23, 2024
1 parent 1e13118 commit 81f2267
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/collections/blazemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl<K, V> BlazeMap<K, V> {
/// policy.
#[inline]
pub fn shrink_to_fit(&mut self) {
let prev_len = self.inner.iter().filter_map(Option::as_ref).count();
debug_assert_eq!(prev_len, self.len);
if !self.is_empty() {
let elems_to_crop = self
.inner
Expand All @@ -86,10 +88,9 @@ impl<K, V> BlazeMap<K, V> {
self.inner.truncate(self.inner.len() - elems_to_crop);
}
self.inner.shrink_to_fit();
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
self.len
);
let current_len = self.inner.iter().filter_map(Option::as_ref).count();
debug_assert_eq!(prev_len, current_len);
debug_assert_eq!(current_len, self.len);
}

/// Clears the map, returning all key-value pairs as an iterator.
Expand All @@ -100,6 +101,7 @@ impl<K, V> BlazeMap<K, V> {
/// The returned iterator keeps a mutable borrow on the map to optimize its
/// implementation.
#[inline]
#[must_use]
pub fn drain(&mut self) -> Drain<'_, K, V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand Down Expand Up @@ -136,6 +138,7 @@ where
/// An iterator visiting all key-value pairs, with mutable references to the
/// values. The iterator element type is `(K, &mut V)`.
#[inline]
#[must_use]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand Down Expand Up @@ -174,6 +177,7 @@ where
/// An iterator visiting all values mutably. The iterator element type is
/// `&mut V`.
#[inline]
#[must_use]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand Down Expand Up @@ -211,6 +215,7 @@ where
{
/// Returns `true` if the map contains a value for the specified key.
#[inline]
#[must_use]
pub fn contains_key(&self, key: K) -> bool {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand All @@ -224,6 +229,7 @@ where

/// Returns a reference to the value corresponding to the key.
#[inline]
#[must_use]
pub fn get(&self, key: K) -> Option<&V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand All @@ -234,6 +240,7 @@ where

/// Returns a mutable reference to the value corresponding to the key.
#[inline]
#[must_use]
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand Down Expand Up @@ -289,6 +296,7 @@ where
/// Gets the given key’s corresponding entry in the map for in-place
/// manipulation.
#[inline]
#[must_use]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
debug_assert_eq!(
self.inner.iter().filter_map(Option::as_ref).count(),
Expand Down Expand Up @@ -427,6 +435,7 @@ where
K: BlazeMapId,
{
#[inline]
#[must_use]
fn default() -> Self {
Self::new()
}
Expand Down
6 changes: 6 additions & 0 deletions src/collections/blazemap/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ where

/// Returns this entry’s key.
#[inline]
#[must_use]
pub fn key(&self) -> K {
match self {
Entry::Occupied(entry) => entry.key(),
Expand Down Expand Up @@ -125,6 +126,7 @@ where
{
/// Gets the key in the entry.
#[inline]
#[must_use]
pub fn key(&self) -> K {
self.key
}
Expand All @@ -140,6 +142,7 @@ where

/// Gets a reference to the value in the entry.
#[inline]
#[must_use]
pub fn get(&self) -> &V {
unsafe { self.value.as_ref().unwrap_unchecked() }
}
Expand All @@ -152,6 +155,7 @@ where
///
/// [`into_mut`]: Self::into_mut
#[inline]
#[must_use]
pub fn get_mut(&mut self) -> &mut V {
unsafe { self.value.as_mut().unwrap_unchecked() }
}
Expand All @@ -164,6 +168,7 @@ where
///
/// [`get_mut`]: Self::get_mut
#[inline]
#[must_use]
pub fn into_mut(self) -> &'a mut V {
unsafe { self.value.as_mut().unwrap_unchecked() }
}
Expand All @@ -190,6 +195,7 @@ where
/// Gets the key that would be used when inserting a value through the
/// [`VacantEntry`].
#[inline]
#[must_use]
pub fn key(&self) -> K {
self.key
}
Expand Down

0 comments on commit 81f2267

Please sign in to comment.