diff --git a/src/lib.rs b/src/lib.rs index 1c690ee..8e0ee8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,8 +11,7 @@ use std::marker::PhantomData; use Entry::{Occupied, Vacant}; -pub use internals::{CloneAny, DebugAny}; -use internals::Implements; +pub use internals::{CloneAny, DebugAny, Implements}; /// A map keyed by types. /// @@ -139,6 +138,13 @@ impl TypeMap { }) } + /// Find a value in the map and get a reference to it if it exists, or + /// insert a default value first. + pub fn get_or_default(&mut self) -> &mut K::Value + where K::Value: Any + Implements + Default { + self.entry::().or_insert_with(Default::default) + } + /// Get the given key's corresponding entry in the map for in-place manipulation. pub fn entry<'a, K: Key>(&'a mut self) -> Entry<'a, K, A> where K::Value: Any + Implements {