Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When possible without changing semantics, implement Iterator::last in terms of DoubleEndedIterator::next_back for types in liballoc and libcore. #62316

Merged
merged 1 commit into from
Jul 4, 2019

Commits on Jul 2, 2019

  1. When possible without changing semantics, implement Iterator::last in…

    … terms of DoubleEndedIterator::next_back for types in liballoc and libcore.
    
    Provided that the iterator has finite length and does not trigger user-provided code, this is safe.
    
    What follows is a full list of the DoubleEndedIterators in liballoc/libcore and whether this optimization is safe, and if not, why not.
    
    src/liballoc/boxed.rs
    Box: Pass through to avoid defeating optimization of the underlying DoubleIterator implementation. This has no correctness impact.
    
    src/liballoc/collections/binary_heap.rs
    Iter: Pass through to avoid defeating optimizations on slice::Iter
    IntoIter: Not safe, changes Drop order
    Drain: Not safe, changes Drop order
    
    src/liballoc/collections/btree/map.rs
    Iter: Safe to call next_back, invokes no user defined code.
    IterMut: ditto
    IntoIter: Not safe, changes Drop order
    Keys: Safe to call next_back, invokes no user defined code.
    Values: ditto
    ValuesMut: ditto
    Range: ditto
    RangeMut: ditto
    
    src/liballoc/collections/btree/set.rs
    Iter: Safe to call next_back, invokes no user defined code.
    IntoIter: Not safe, changes Drop order
    Range: Safe to call next_back, invokes no user defined code.
    
    src/liballoc/collections/linked_list.rs
    Iter: Safe to call next_back, invokes no user defined code.
    IterMut: ditto
    IntoIter: Not safe, changes Drop order
    
    src/liballoc/collections/vec_deque.rs
    Iter: Safe to call next_back, invokes no user defined code.
    IterMut: ditto
    IntoIter: Not safe, changes Drop order
    Drain: ditto
    
    src/liballoc/string.rs
    Drain: Safe because return type is a primitive (char)
    
    src/liballoc/vec.rs
    IntoIter: Not safe, changes Drop order
    Drain: ditto
    Splice: ditto
    
    src/libcore/ascii.rs
    EscapeDefault: Safe because return type is a primitive (u8)
    
    src/libcore/iter/adapters/chain.rs
    Chain: Not safe, invokes user defined code (Iterator impl)
    
    src/libcore/iter/adapters/flatten.rs
    FlatMap: Not safe, invokes user defined code (Iterator impl)
    Flatten: ditto
    FlattenCompat: ditto
    
    src/libcore/iter/adapters/mod.rs
    Rev: Not safe, invokes user defined code (Iterator impl)
    Copied: ditto
    Cloned: Not safe, invokes user defined code (Iterator impl and T::clone)
    Map: Not safe, invokes user defined code (Iterator impl + closure)
    Filter: ditto
    FilterMap: ditto
    Enumerate: Not safe, invokes user defined code (Iterator impl)
    Skip: ditto
    Fuse: ditto
    Inspect: ditto
    
    src/libcore/iter/adapters/zip.rs
    Zip: Not safe, invokes user defined code (Iterator impl)
    
    src/libcore/iter/range.rs
    ops::Range: Not safe, changes Drop order, but ALREADY HAS SPECIALIZATION
    ops::RangeInclusive: ditto
    
    src/libcore/iter/sources.rs
    Repeat: Not safe, calling last should iloop.
    Empty: No point, iterator is at most one item long.
    Once: ditto
    OnceWith: ditto
    
    src/libcore/option.rs
    Item: No point, iterator is at most one item long.
    Iter: ditto
    IterMut: ditto
    IntoIter: ditto
    
    src/libcore/result.rs
    Iter: No point, iterator is at most one item long
    IterMut: ditto
    IntoIter: ditto
    
    src/libcore/slice/mod.rs
    Split: Not safe, invokes user defined closure
    SplitMut: ditto
    RSplit: ditto
    RSplitMut: ditto
    Windows: Safe, already has specialization
    Chunks: ditto
    ChunksMut: ditto
    ChunksExact: ditto
    ChunksExactMut: ditto
    RChunks: ditto
    RChunksMut: ditto
    RChunksExact: ditto
    RChunksExactMut: ditto
    
    src/libcore/str/mod.rs
    Chars: Safe, already has specialization
    CharIndices: ditto
    Bytes: ditto
    Lines: Safe to call next_back, invokes no user defined code.
    LinesAny: Deprecated
    Everything that is generic over P: Pattern: Not safe because Pattern invokes user defined code.
    SplitWhitespace: Safe to call next_back, invokes no user defined code.
    SplitAsciiWhitespace: ditto
    khuey committed Jul 2, 2019
    Configuration menu
    Copy the full SHA
    db16e17 View commit details
    Browse the repository at this point in the history