From 84b897b91eb8e6c6c9dbd97520849af1f553235d Mon Sep 17 00:00:00 2001 From: arcnmx Date: Mon, 11 Jan 2016 18:36:50 -0500 Subject: [PATCH 1/2] get_or_default --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1c690ee..29535ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,6 +139,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 { From c68d900b3c151087c98cf7f628dc34efdd3519e4 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Mon, 11 Jan 2016 18:36:59 -0500 Subject: [PATCH 2/2] Expose Implements --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 29535ae..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. ///