Why IntoIter::next() casts a const ptr to mut ptr then back to const ptr

https://github.com/rust-lang/rust/blob/74500b99783d126e330184f0392a78f8b93b73ef/library/alloc/src/vec/into_iter.rs#L122-L130
Line 129:

self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T };

self.ptr is *const T and arithm_offset() returns *const i8.
Why is the *const i8 cast to *mut T instead of *const T?

It has been changed here:

https://github.com/rust-lang/rust/commit/01a766e5210157546a2b6c673700b9959289eff9

so I guess as_mut_slice needs *mut T, so the rest of the code had to adjust. But end hasn't been changed to *mut, which is why it's cast back and forth.

Wow, thx. That is a pretty old commit. as_mut_slice now takes &mut self instead of &self and ptr becomes const ptr again. So the original casting to *mut T becomes unnecessary. Guess developers forget changing it back. Nevertheless, it just looks odd and does not hurt anything.
https://github.com/rust-lang/rust/blob/c6bc46227ab57a844fc7a9ed3a6c9efb35c725a9/library/alloc/src/vec/into_iter.rs#L73
https://github.com/rust-lang/rust/blob/c6bc46227ab57a844fc7a9ed3a6c9efb35c725a9/library/alloc/src/vec/into_iter.rs#L31

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.