Add an EntityMap::get_or_default() method.

This covers a common pattern for secondary entity maps: Get the value in
the map or the default value for out-of-range keys.
This commit is contained in:
Jakob Stoklund Olesen
2017-04-25 15:50:59 -07:00
parent a332c3d024
commit c36aedfd03
5 changed files with 10 additions and 18 deletions

View File

@@ -148,6 +148,11 @@ impl<K, V> EntityMap<K, V>
}
&mut self.elems[k.index()]
}
/// Get the element at `k` or the default value if `k` is out of range.
pub fn get_or_default(&self, k: K) -> V {
self.elems.get(k.index()).cloned().unwrap_or_default()
}
}
/// Immutable indexing into an `EntityMap`.