5757{
5858 /// Returns true if both maps contain the same key value pairs
5959 fn eq ( & self , other : & Self ) -> bool {
60- self . len ( ) == other. len ( ) && self . iter ( ) . all ( |( k, v) | & T :: value ( other, k ) == v )
60+ self . len ( ) == other. len ( ) && self . iter ( ) . all ( |( k, v) | other. get ( k ) . as_ref ( ) == Some ( v ) )
6161 }
6262}
6363
8282 T :: contains ( self , key)
8383 }
8484
85+ /// Returns the value associated with the key if it exists.
86+ pub fn get ( & self , key : & T :: Key ) -> Option < T :: Value > {
87+ if self . contains ( key) {
88+ Some ( T :: get_or_default ( self , key) )
89+ } else {
90+ None
91+ }
92+ }
93+
94+ /// Returns the value associated with the key or a default value.
95+ pub fn get_or_default ( & self , key : & T :: Key ) -> T :: Value {
96+ T :: get_or_default ( self , key)
97+ }
98+
8599 /// Inserts a new item with the key and a value of value.
86100 ///
87101 /// The key and value are references here so they can be opaque or trivial.
@@ -113,11 +127,6 @@ where
113127 pub fn remove ( & mut self , key : & T :: Key ) -> bool {
114128 T :: remove ( self , key)
115129 }
116-
117- /// Returns the value associated with the key.
118- pub fn value ( & self , key : & T :: Key ) -> T :: Value {
119- T :: value ( self , key)
120- }
121130}
122131
123132impl < T > QMap < T >
@@ -195,6 +204,7 @@ pub trait QMapPair: Sized {
195204 fn contains ( map : & QMap < Self > , key : & Self :: Key ) -> bool ;
196205 fn default ( ) -> QMap < Self > ;
197206 fn drop ( map : & mut QMap < Self > ) ;
207+ fn get_or_default ( map : & QMap < Self > , key : & Self :: Key ) -> Self :: Value ;
198208 /// # Safety
199209 ///
200210 /// Calling this method with an out-of-bounds index is undefined behavior
@@ -212,7 +222,6 @@ pub trait QMapPair: Sized {
212222 fn insert_clone ( map : & mut QMap < Self > , key : & Self :: Key , value : & Self :: Value ) ;
213223 fn len ( map : & QMap < Self > ) -> isize ;
214224 fn remove ( map : & mut QMap < Self > , key : & Self :: Key ) -> bool ;
215- fn value ( map : & QMap < Self > , key : & Self :: Key ) -> Self :: Value ;
216225}
217226
218227macro_rules! impl_qmap_pair {
@@ -242,6 +251,10 @@ macro_rules! impl_qmap_pair {
242251 $module:: drop( map) ;
243252 }
244253
254+ fn get_or_default( map: & QMap <Self >, key: & $keyTypeName) -> $valueTypeName {
255+ $module:: get_or_default( map, key)
256+ }
257+
245258 unsafe fn get_unchecked_key( map: & QMap <Self >, pos: isize ) -> & $keyTypeName {
246259 $module:: get_unchecked_key( map, pos)
247260 }
@@ -265,10 +278,6 @@ macro_rules! impl_qmap_pair {
265278 fn remove( map: & mut QMap <Self >, key: & $keyTypeName) -> bool {
266279 $module:: remove( map, key)
267280 }
268-
269- fn value( map: & QMap <Self >, key: & $keyTypeName) -> $valueTypeName {
270- $module:: value( map, key)
271- }
272281 }
273282 } ;
274283}
0 commit comments