Skip to content

Commit

Permalink
reduce nesting in the sparse_set module (#17066)
Browse files Browse the repository at this point in the history
# Objective

Just being fussy but I hate this `.map(|v|
v.is_some()).unwrap_or(false)` stuff.

## Solution
Reduce it using `and_then`.

---------

Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
  • Loading branch information
ickshonpe and Jondolf authored Jan 1, 2025
1 parent afed4e2 commit 2931e35
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions crates/bevy_ecs/src/storage/sparse_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! impl_sparse_array {
#[inline]
pub fn contains(&self, index: I) -> bool {
let index = index.sparse_set_index();
self.values.get(index).map(|v| v.is_some()).unwrap_or(false)
self.values.get(index).is_some_and(Option::is_some)
}

/// Returns a reference to the value at `index`.
Expand All @@ -59,7 +59,7 @@ macro_rules! impl_sparse_array {
#[inline]
pub fn get(&self, index: I) -> Option<&V> {
let index = index.sparse_set_index();
self.values.get(index).map(|v| v.as_ref()).unwrap_or(None)
self.values.get(index).and_then(Option::as_ref)
}
}
};
Expand Down Expand Up @@ -87,10 +87,7 @@ impl<I: SparseSetIndex, V> SparseArray<I, V> {
#[inline]
pub fn get_mut(&mut self, index: I) -> Option<&mut V> {
let index = index.sparse_set_index();
self.values
.get_mut(index)
.map(|v| v.as_mut())
.unwrap_or(None)
self.values.get_mut(index).and_then(Option::as_mut)
}

/// Removes and returns the value stored at `index`.
Expand Down

0 comments on commit 2931e35

Please sign in to comment.