reduce nesting in the sparse_set
module (#17066)
# 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>
This commit is contained in:
parent
afed4e27d1
commit
2931e350b6
@ -50,7 +50,7 @@ macro_rules! impl_sparse_array {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn contains(&self, index: I) -> bool {
|
pub fn contains(&self, index: I) -> bool {
|
||||||
let index = index.sparse_set_index();
|
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`.
|
/// Returns a reference to the value at `index`.
|
||||||
@ -59,7 +59,7 @@ macro_rules! impl_sparse_array {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn get(&self, index: I) -> Option<&V> {
|
pub fn get(&self, index: I) -> Option<&V> {
|
||||||
let index = index.sparse_set_index();
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -87,10 +87,7 @@ impl<I: SparseSetIndex, V> SparseArray<I, V> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_mut(&mut self, index: I) -> Option<&mut V> {
|
pub fn get_mut(&mut self, index: I) -> Option<&mut V> {
|
||||||
let index = index.sparse_set_index();
|
let index = index.sparse_set_index();
|
||||||
self.values
|
self.values.get_mut(index).and_then(Option::as_mut)
|
||||||
.get_mut(index)
|
|
||||||
.map(|v| v.as_mut())
|
|
||||||
.unwrap_or(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes and returns the value stored at `index`.
|
/// Removes and returns the value stored at `index`.
|
||||||
|
Loading…
Reference in New Issue
Block a user