@@ -6,7 +6,7 @@ use hashbrown::{
66 hash_table:: { Entry as RawEntry , Iter as RawIter } ,
77} ;
88
9- use crate :: { Allocator , Ephemeron , Finalize , Gc , GcRefCell , Trace , custom_trace} ;
9+ use crate :: { Allocator , Ephemeron , Finalize , Gc , GcRef , GcRefCell , Trace , custom_trace} ;
1010use std:: { fmt, hash:: BuildHasher , marker:: PhantomData } ;
1111
1212/// A map that holds weak references to its keys and is traced by the garbage collector.
@@ -21,7 +21,7 @@ unsafe impl<K: Trace + ?Sized + 'static, V: Trace + 'static> Trace for WeakMap<K
2121 } ) ;
2222}
2323
24- impl < K : Trace + ?Sized , V : Trace + Clone > WeakMap < K , V > {
24+ impl < K : Trace + ?Sized , V : Trace > WeakMap < K , V > {
2525 /// Creates a new `WeakMap`.
2626 #[ must_use]
2727 #[ inline]
@@ -35,9 +35,10 @@ impl<K: Trace + ?Sized, V: Trace + Clone> WeakMap<K, V> {
3535 self . inner . borrow_mut ( ) . insert ( key, value) ;
3636 }
3737
38- /// Removes a key from the map, returning the value at the key if the key was previously in the map.
38+ /// Removes a key from the map, returning `true` if the key was previously in
39+ /// the map.
3940 #[ inline]
40- pub fn remove ( & mut self , key : & Gc < K > ) -> Option < V > {
41+ pub fn remove ( & mut self , key : & Gc < K > ) -> bool {
4142 self . inner . borrow_mut ( ) . remove ( key)
4243 }
4344
@@ -48,11 +49,11 @@ impl<K: Trace + ?Sized, V: Trace + Clone> WeakMap<K, V> {
4849 self . inner . borrow ( ) . contains_key ( key)
4950 }
5051
51- /// Returns a reference to the value corresponding to the key.
52+ /// Returns a reference to the ephemeron corresponding to the key.
5253 #[ must_use]
5354 #[ inline]
54- pub fn get ( & self , key : & Gc < K > ) -> Option < V > {
55- self . inner . borrow ( ) . get ( key)
55+ pub fn get < ' a > ( & ' a self , key : & Gc < K > ) -> Option < GcRef < ' a , Ephemeron < K , V > > > {
56+ GcRef :: try_map ( self . inner . borrow ( ) , |inner| inner . get ( key) )
5657 }
5758}
5859
@@ -222,7 +223,7 @@ where
222223impl < K , V , S > RawWeakMap < K , V , S >
223224where
224225 K : Trace + ?Sized + ' static ,
225- V : Trace + Clone + ' static ,
226+ V : Trace + ' static ,
226227 S : BuildHasher ,
227228{
228229 /// Reserves capacity for at least `additional` more elements to be inserted
@@ -275,14 +276,13 @@ where
275276 . shrink_to ( min_capacity, make_hasher :: < _ , V , S > ( & self . hash_builder ) ) ;
276277 }
277278
278- /// Returns the value corresponding to the supplied key.
279- // TODO: make this return a reference instead of cloning.
280- pub ( crate ) fn get ( & self , k : & Gc < K > ) -> Option < V > {
279+ /// Returns the ephemeron corresponding to the supplied key.
280+ pub ( crate ) fn get ( & self , k : & Gc < K > ) -> Option < & Ephemeron < K , V > > {
281281 if self . table . is_empty ( ) {
282282 None
283283 } else {
284284 let hash = make_hash_from_gc ( & self . hash_builder , k) ;
285- self . table . find ( hash, equivalent_key ( k) ) ? . value ( )
285+ self . table . find ( hash, equivalent_key ( k) )
286286 }
287287 }
288288
@@ -313,16 +313,16 @@ where
313313 old
314314 }
315315
316- /// Removes a key from the map, returning the value at the key if the key
316+ /// Removes a key from the map, returning `true` if the key
317317 /// was previously in the map. Keeps the allocated memory for reuse.
318- pub ( crate ) fn remove ( & mut self , k : & Gc < K > ) -> Option < V > {
318+ pub ( crate ) fn remove ( & mut self , k : & Gc < K > ) -> bool {
319319 let hash = make_hash_from_gc ( & self . hash_builder , k) ;
320- self . table
321- . find_entry ( hash , equivalent_key ( k ) )
322- . ok ( ) ?
323- . remove ( )
324- . 0
325- . value ( )
320+ if let Ok ( entry ) = self . table . find_entry ( hash , equivalent_key ( k ) ) {
321+ entry . remove ( ) ;
322+ true
323+ } else {
324+ false
325+ }
326326 }
327327
328328 /// Clears all the expired keys in the map.
0 commit comments