1- use super :: { Bucket , Entries , IndexMap , Iter , IterMut , Keys , Values , ValuesMut } ;
1+ use super :: {
2+ Bucket , Entries , IndexMap , IntoIter , IntoKeys , IntoValues , Iter , IterMut , Keys , Values ,
3+ ValuesMut ,
4+ } ;
25use crate :: util:: try_simplify_range;
36
7+ use alloc:: boxed:: Box ;
8+ use alloc:: vec:: Vec ;
49use core:: cmp:: Ordering ;
510use core:: fmt;
611use core:: hash:: { Hash , Hasher } ;
@@ -18,22 +23,32 @@ pub struct Slice<K, V> {
1823 pub ( crate ) entries : [ Bucket < K , V > ] ,
1924}
2025
26+ // SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
27+ // and reference lifetimes are bound together in function signatures.
2128#[ allow( unsafe_code) ]
2229impl < K , V > Slice < K , V > {
2330 pub ( super ) fn from_slice ( entries : & [ Bucket < K , V > ] ) -> & Self {
24- // SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
25- // and the lifetimes are bound together by this function's signature.
2631 unsafe { & * ( entries as * const [ Bucket < K , V > ] as * const Self ) }
2732 }
2833
2934 pub ( super ) fn from_mut_slice ( entries : & mut [ Bucket < K , V > ] ) -> & mut Self {
30- // SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
31- // and the lifetimes are bound together by this function's signature.
3235 unsafe { & mut * ( entries as * mut [ Bucket < K , V > ] as * mut Self ) }
3336 }
37+
38+ pub ( super ) fn from_boxed ( entries : Box < [ Bucket < K , V > ] > ) -> Box < Self > {
39+ unsafe { Box :: from_raw ( Box :: into_raw ( entries) as * mut Self ) }
40+ }
41+
42+ fn into_boxed ( self : Box < Self > ) -> Box < [ Bucket < K , V > ] > {
43+ unsafe { Box :: from_raw ( Box :: into_raw ( self ) as * mut [ Bucket < K , V > ] ) }
44+ }
3445}
3546
3647impl < K , V > Slice < K , V > {
48+ pub ( crate ) fn into_entries ( self : Box < Self > ) -> Vec < Bucket < K , V > > {
49+ self . into_boxed ( ) . into_vec ( )
50+ }
51+
3752 /// Return the number of key-value pairs in the map slice.
3853 #[ inline]
3954 pub fn len ( & self ) -> usize {
@@ -173,6 +188,13 @@ impl<K, V> Slice<K, V> {
173188 }
174189 }
175190
191+ /// Return an owning iterator over the keys of the map slice.
192+ pub fn into_keys ( self : Box < Self > ) -> IntoKeys < K , V > {
193+ IntoKeys {
194+ iter : self . into_entries ( ) . into_iter ( ) ,
195+ }
196+ }
197+
176198 /// Return an iterator over the values of the map slice.
177199 pub fn values ( & self ) -> Values < ' _ , K , V > {
178200 Values {
@@ -186,6 +208,13 @@ impl<K, V> Slice<K, V> {
186208 iter : self . entries . iter_mut ( ) ,
187209 }
188210 }
211+
212+ /// Return an owning iterator over the values of the map slice.
213+ pub fn into_values ( self : Box < Self > ) -> IntoValues < K , V > {
214+ IntoValues {
215+ iter : self . into_entries ( ) . into_iter ( ) ,
216+ }
217+ }
189218}
190219
191220impl < ' a , K , V > IntoIterator for & ' a Slice < K , V > {
@@ -206,6 +235,17 @@ impl<'a, K, V> IntoIterator for &'a mut Slice<K, V> {
206235 }
207236}
208237
238+ impl < K , V > IntoIterator for Box < Slice < K , V > > {
239+ type IntoIter = IntoIter < K , V > ;
240+ type Item = ( K , V ) ;
241+
242+ fn into_iter ( self ) -> Self :: IntoIter {
243+ IntoIter {
244+ iter : self . into_entries ( ) . into_iter ( ) ,
245+ }
246+ }
247+ }
248+
209249impl < K , V > Default for & ' _ Slice < K , V > {
210250 fn default ( ) -> Self {
211251 Slice :: from_slice ( & [ ] )
0 commit comments