@@ -961,6 +961,7 @@ impl<T> HeaderMap<T> {
961961 let entries = & mut self . entries [ ..] as * mut _ ;
962962 let extra_values = & mut self . extra_values as * mut _ ;
963963 let len = self . entries . len ( ) ;
964+ // SAFETY: see comment above
964965 unsafe { self . entries . set_len ( 0 ) ; }
965966
966967 Drain {
@@ -2070,6 +2071,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
20702071 fn next ( & mut self ) -> Option < Self :: Item > {
20712072 self . inner
20722073 . next_unsafe ( )
2074+ // SAFETY: Iter invariant: only valid pointers
20732075 . map ( |( key, ptr) | ( key, unsafe { & * ptr } ) )
20742076 }
20752077
@@ -2090,6 +2092,7 @@ impl<'a, T> IterMut<'a, T> {
20902092 use self :: Cursor :: * ;
20912093
20922094 if self . cursor . is_none ( ) {
2095+ // SAFETY: invariant dereferencing the self.map pointer is always safe
20932096 if ( self . entry + 1 ) >= unsafe { & * self . map } . entries . len ( ) {
20942097 return None ;
20952098 }
@@ -2098,6 +2101,7 @@ impl<'a, T> IterMut<'a, T> {
20982101 self . cursor = Some ( Cursor :: Head ) ;
20992102 }
21002103
2104+ // SAFETY: invariant dereferencing the self.map pointer is always safe
21012105 let entry = unsafe { & ( * self . map ) . entries [ self . entry ] } ;
21022106
21032107 match self . cursor . unwrap ( ) {
@@ -2106,6 +2110,7 @@ impl<'a, T> IterMut<'a, T> {
21062110 Some ( ( & entry. key , & entry. value as * const _ as * mut _ ) )
21072111 }
21082112 Values ( idx) => {
2113+ // SAFETY: invariant dereferencing the self.map pointer is always safe
21092114 let extra = unsafe { & ( * self . map ) . extra_values [ idx] } ;
21102115
21112116 match extra. next {
@@ -2128,6 +2133,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
21282133 }
21292134
21302135 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2136+ // SAFETY: invariant dereferencing the self.map pointer is always safe
21312137 let map = unsafe { & * self . map } ;
21322138 debug_assert ! ( map. entries. len( ) >= self . entry) ;
21332139
@@ -2204,9 +2210,8 @@ impl<'a, T> Iterator for Drain<'a, T> {
22042210 // Remove the extra value
22052211
22062212 let raw_links = RawLinks ( self . entries ) ;
2207- let extra = unsafe {
2208- remove_extra_value ( raw_links, & mut * self . extra_values , next)
2209- } ;
2213+ // SAFETY: dereferencing self.extra_values valid as long as self is alive.
2214+ let extra = remove_extra_value ( raw_links, unsafe { & mut * self . extra_values } , next) ;
22102215
22112216 match extra. next {
22122217 Link :: Extra ( idx) => self . next = Some ( idx) ,
@@ -2224,6 +2229,8 @@ impl<'a, T> Iterator for Drain<'a, T> {
22242229
22252230 self . idx += 1 ;
22262231
2232+ // SAFETY: pointer operation always valid, as `self` cannot outlive the HeaderMap it is
2233+ // referencing.
22272234 unsafe {
22282235 let entry = & ( * self . entries ) [ idx] ;
22292236
@@ -2243,6 +2250,7 @@ impl<'a, T> Iterator for Drain<'a, T> {
22432250 // For instance, extending a new `HeaderMap` wouldn't need to
22442251 // reserve the upper-bound in `entries`, only the lower-bound.
22452252 let lower = self . len - self . idx ;
2253+ // SAFETY: dereferencing self.extra_values valid as long as self is alive.
22462254 let upper = unsafe { ( * self . extra_values ) . len ( ) } + lower;
22472255 ( lower, Some ( upper) )
22482256 }
@@ -2606,6 +2614,7 @@ impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
26062614 fn next ( & mut self ) -> Option < Self :: Item > {
26072615 use self :: Cursor :: * ;
26082616
2617+ // SAFETY: dereferencing self.map valid as long as self is alive.
26092618 let entry = unsafe { & mut ( * self . map ) . entries [ self . index ] } ;
26102619
26112620 match self . front {
@@ -2626,6 +2635,7 @@ impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
26262635 Some ( & mut entry. value )
26272636 }
26282637 Some ( Values ( idx) ) => {
2638+ // SAFETY: dereferencing self.map valid as long as self is alive.
26292639 let extra = unsafe { & mut ( * self . map ) . extra_values [ idx] } ;
26302640
26312641 if self . front == self . back {
@@ -2649,6 +2659,7 @@ impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
26492659 fn next_back ( & mut self ) -> Option < Self :: Item > {
26502660 use self :: Cursor :: * ;
26512661
2662+ // SAFETY: dereferencing self.map valid as long as self is alive.
26522663 let entry = unsafe { & mut ( * self . map ) . entries [ self . index ] } ;
26532664
26542665 match self . back {
@@ -2658,6 +2669,7 @@ impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
26582669 Some ( & mut entry. value )
26592670 }
26602671 Some ( Values ( idx) ) => {
2672+ // SAFETY: dereferencing self.map valid as long as self is alive.
26612673 let extra = unsafe { & mut ( * self . map ) . extra_values [ idx] } ;
26622674
26632675 if self . front == self . back {
@@ -2726,7 +2738,7 @@ impl<T> Drop for IntoIter<T> {
27262738 // Ensure the iterator is consumed
27272739 for _ in self . by_ref ( ) { }
27282740
2729- // All the values have already been yielded out.
2741+ // SAFETY: All the values have already been yielded out, once dropped .
27302742 unsafe {
27312743 self . extra_values . set_len ( 0 ) ;
27322744 }
0 commit comments