11use crate :: WithId ;
22use core:: marker:: PhantomData ;
3- use std:: { collections:: HashMap , iter:: FromIterator } ;
3+ use std:: {
4+ collections:: { hash_map, HashMap } ,
5+ iter:: FromIterator ,
6+ } ;
47
58/// Typed Id over a [Collection]
69#[ derive( Derivative , Serialize , Deserialize ) ]
@@ -90,6 +93,26 @@ impl<T> Collection<T> {
9093 pub fn len ( & self ) -> usize {
9194 self . 0 . len ( )
9295 }
96+
97+ // Return true if the collection has no objects.
98+ pub fn is_empty ( & self ) -> bool {
99+ self . 0 . is_empty ( )
100+ }
101+
102+ /// Iterates over the ([Id]<T>, &T) of the [Collection].
103+ pub fn iter ( & self ) -> hash_map:: Iter < Id < T > , T > {
104+ self . 0 . iter ( )
105+ }
106+
107+ /// Iterates over the &T of the [Collection].
108+ pub fn values ( & self ) -> hash_map:: Values < ' _ , Id < T > , T > {
109+ self . 0 . values ( )
110+ }
111+
112+ /// Iterates over the &mut T of the [Collection].
113+ pub fn values_mut ( & mut self ) -> hash_map:: ValuesMut < ' _ , Id < T > , T > {
114+ self . 0 . values_mut ( )
115+ }
93116}
94117
95118// Implements FromIterator to be able to easily build a [Collection] if we know how to associate an object with its [Id]
@@ -104,3 +127,21 @@ impl<T: WithId> FromIterator<T> for Collection<T> {
104127 c
105128 }
106129}
130+
131+ impl < ' a , T > IntoIterator for & ' a Collection < T > {
132+ type Item = ( & ' a Id < T > , & ' a T ) ;
133+ type IntoIter = hash_map:: Iter < ' a , Id < T > , T > ;
134+
135+ fn into_iter ( self ) -> Self :: IntoIter {
136+ self . iter ( )
137+ }
138+ }
139+
140+ impl < T > IntoIterator for Collection < T > {
141+ type Item = ( Id < T > , T ) ;
142+ type IntoIter = hash_map:: IntoIter < Id < T > , T > ;
143+
144+ fn into_iter ( self ) -> Self :: IntoIter {
145+ self . 0 . into_iter ( )
146+ }
147+ }
0 commit comments