Skip to content

Commit 84b897b

Browse files
committed
get_or_default
1 parent 6de0005 commit 84b897b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ impl<A: UnsafeAnyExt + ?Sized> TypeMap<A> {
139139
})
140140
}
141141

142+
/// Find a value in the map and get a reference to it if it exists, or
143+
/// insert a default value first.
144+
pub fn get_or_default<K: Key>(&mut self) -> &mut K::Value
145+
where K::Value: Any + Implements<A> + Default {
146+
self.entry::<K>().or_insert_with(Default::default)
147+
}
148+
142149
/// Get the given key's corresponding entry in the map for in-place manipulation.
143150
pub fn entry<'a, K: Key>(&'a mut self) -> Entry<'a, K, A>
144151
where K::Value: Any + Implements<A> {

0 commit comments

Comments
 (0)