@@ -130,20 +130,25 @@ where
130130 A : Allocator ,
131131{
132132 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
133- let peek = if self . idx < self . end {
134- // This has to use pointer arithmetic as `self.vec[self.idx]` or
135- // `self.vec.get_unchecked(self.idx)` wouldn't work since we
136- // temporarily set the length of `self.vec` to zero.
137- //
138- // SAFETY:
139- // Since `self.idx` is smaller than `self.end` and `self.end` is
140- // smaller than `self.old_len`, `idx` is valid for indexing the
141- // buffer. Also, per the invariant of `self.idx`, this element
142- // has not been inspected/moved out yet.
143- Some ( unsafe { & * self . vec . as_ptr ( ) . add ( self . idx ) } )
144- } else {
145- None
146- } ;
147- f. debug_struct ( "ExtractIf" ) . field ( "peek" , & peek) . finish_non_exhaustive ( )
133+ // We have to use pointer arithmetics here,
134+ // because the length of `self.vec` is temporarily set to 0.
135+ let start = self . vec . as_ptr ( ) ;
136+
137+ // SAFETY: we always keep first `self.idx - self.del` elements valid.
138+ let retained = unsafe { slice:: from_raw_parts ( start, self . idx - self . del ) } ;
139+
140+ // SAFETY: we have not yet touched elements starting at `self.idx`.
141+ let valid_tail =
142+ unsafe { slice:: from_raw_parts ( start. add ( self . idx ) , self . old_len - self . idx ) } ;
143+
144+ // SAFETY: `end - idx <= old_len - idx`, because `end <= old_len`. Also `idx <= end` by invariant.
145+ let ( remainder, skipped_tail) =
146+ unsafe { valid_tail. split_at_unchecked ( self . end - self . idx ) } ;
147+
148+ f. debug_struct ( "ExtractIf" )
149+ . field ( "retained" , & retained)
150+ . field ( "remainder" , & remainder)
151+ . field ( "skipped_tail" , & skipped_tail)
152+ . finish_non_exhaustive ( )
148153 }
149154}
0 commit comments