Conversation
|
Just realised that if |
| /// Calls to `self.nth_back(n)` are counted as `n + 1` calls to `self.next_back()`, and | ||
| /// calls to `self.advance_back_by(n)` as `n` calls. |
There was a problem hiding this comment.
While this is true, I'm not sure what it's doing here.
There was a problem hiding this comment.
Point 3 of TrustedRandomAccess's safety requirements says (paraphrased) that you can't use next_back() to move the tail to an element that has already been returned by __iterator_get_unchecked(). This extends that requirement to calls to nth_back() and advance_back_by().
| iter4.next_back(); | ||
| assert_eq!(&log.take(), &[0, 1, 1, 1, 0, 1]); | ||
| } | ||
|
|
There was a problem hiding this comment.
This tests for the ordering of calls to the constituent iterators of a Zip, but I don't think that ordering is specified, nor do I think it should be...
There was a problem hiding this comment.
I didn't want to break anything, but if the call order is unspecified then we can get cleaner code here, and faster code for nth() and nth_back(), so I've removed the test.
|
I think this is fine, but |
Fix #147704 by replacing
next_back()calls in a loop with one call tonth_back().This also modifies the safety conditions for the (unstable)
TrustedRandomAccessNoCoercetrait by allowing calls toself.nth_back(n)andself.advance_back_by(n)after a call toself.__iterator_get_unchecked()provided they don't move the tail too far.A grep found that the
TrustedRandomAccessNoCoercetrait is implemented by the following structs:alloc::collections::vec_deque::{Iter<T>, IterMut<T>}alloc::vec::IntoIter<T>core::array::IntoIter<T, N>core::char::{CaseMappingIter, ToLowercase, ToUppercase}core::iter::{Cloned<I>, Copied<I>, Enumerate<I>, Fused<I>, Map<I, F>, Skip<I>, Zip<A, B>}core::ops::Range<_>core::range::IterRange<_>core::slice::{Chunks<T>, ChunksExact<T>, ChunksExactMut<T>, ChunksMut<T>, Iter<T>, IterMut<T>, RChunks<T>, RChunksExact<T>, RChunksExactMut<T>, RChunksMut<T>, Windows<T>}core::str::BytesI believe this is sound, as the implementations of
advance_back_by()for the above do essentially the same thing as repeatednext_back()calls, but better optimized.If there is an issue, a partial fix could be implemented by using the new version as the default and specializing
TrustedRandomAccessNoCoerceto use the current implementation.