Commit Graph

103 Commits

Author SHA1 Message Date
Mazdak Farrokhzad 0f92eb8a4a Rollup merge of #62123 - jeremystucki:needless_lifetimes_std, r=alexcrichton
Remove needless lifetimes (std)

Split from #62039
2019-07-05 13:52:58 +02:00
Kyle Huey db16e17212 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
2019-07-02 13:45:29 -07:00
Jeremy Stucki 47ea8ae022 Remove needless lifetimes 2019-07-01 12:15:27 +02:00
Steven Fackler 8a22bc3b30 Revert "Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators."
This reverts commit 3e86cf36b5.
2019-05-22 14:09:34 -07:00
Mazdak Farrokhzad f08c5bbc85 Rollup merge of #59825 - jsgf:from-ref-string, r=sfackler
string: implement From<&String> for String

Allow Strings to be created from borrowed Strings. This is mostly
to make things like passing `&String` to an `impl Into<String>`
parameter frictionless.

Fixes #59827.
2019-05-16 10:43:24 +02:00
Mazdak Farrokhzad bab03cecfe Rollup merge of #60130 - khuey:efficient_last, r=sfackler
Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators

Provided a `DoubleEndedIterator` has finite length, `Iterator::last` is equivalent to `DoubleEndedIterator::next_back`. But searching forwards through the iterator when it's unnecessary is obviously not good for performance. I ran into this on one of the collection iterators.

I tried adding appropriate overloads for a bunch of the iterator adapters like filter, map, etc, but I ran into a lot of type inference failures after doing so.

The other interesting case is what to do with `Repeat`. Do we consider it part of the contract that `Iterator::last` will loop forever on it? The docs do say that the iterator will be evaluated until it returns None. This is also relevant for the adapters, it's trivially easy to observe whether a `Map` adapter invoked its closure a zillion times or just once for the last element.
2019-05-14 22:00:09 +02:00
Josh Stone 0545375ca6 Add examples of ordered retain 2019-05-10 18:01:50 -07:00
Josh Stone 9b3583375d Document the order of {Vec,VecDeque,String}::retain
It's natural for `retain` to work in order from beginning to end, but
this wasn't actually documented to be the case. If we actually promise
this, then the caller can do useful things like track the index of each
element being tested, as [discussed in the forum][1]. This is now
documented for `Vec`, `VecDeque`, and `String`.

[1]: https://users.rust-lang.org/t/vec-retain-by-index/27697

`HashMap` and `HashSet` also have `retain`, and the `hashbrown`
implementation does happen to use a plain `iter()` order too, but it's
not certain that this should always be the case for these types.
2019-04-29 18:32:05 -07:00
Kyle Huey 3e86cf36b5 Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators.
r?Manishearth
2019-04-19 21:52:43 -07:00
Jeremy Fitzhardinge 08b0aca05e string: implement From<&String> for String
Allow Strings to be created from borrowed Strings. This is mostly
to make things like passing &String to an `impl Into<String>`
parameter frictionless.
2019-04-09 16:52:05 -07:00
Scott McMurray df4ea90b39 Use lifetime contravariance to elide more lifetimes in core+alloc+std 2019-03-09 19:10:28 -08:00
Simon Sapin 85f13f0d42 Add a convert::Infallible empty enum, make string::ParseError an alias 2019-02-13 18:00:18 +01:00
Alexander Regueiro 99ed06eb88 libs: doc comments 2019-02-10 23:57:25 +00:00
Mazdak Farrokhzad 2396780cda liballoc: revert nested imports style changes. 2019-02-03 08:27:44 +01:00
Mazdak Farrokhzad 857530cef1 liballoc: fix some idiom lints. 2019-02-02 12:48:12 +01:00
Mazdak Farrokhzad e70c2fbd5c liballoc: elide some lifetimes. 2019-02-02 12:23:15 +01:00
Mazdak Farrokhzad 3bfa0a35f6 liballoc: prefer imports of borrow from libcore. 2019-02-02 10:53:27 +01:00
Mazdak Farrokhzad f09f62f62c liballoc: adjust abolute imports + more import fixes. 2019-02-02 10:34:36 +01:00
Mazdak Farrokhzad 7693e3e666 liballoc: refactor & fix some imports. 2019-02-02 10:14:40 +01:00
Mazdak Farrokhzad e6e27924e1 liballoc: cargo check passes on 2018 2019-02-02 08:36:45 +01:00
Mark Rousskov 2a663555dd Remove licenses 2018-12-25 21:08:33 -07:00
BeatButton 6f288ea337 Fix typo 2018-12-09 14:10:20 -07:00
Pietro Albini e9e92d53ad Rollup merge of #56548 - Lucretiel:string-extend-optimize, r=sfackler
Optimized string FromIterator + Extend impls

I noticed that there was a lost opportunity to reuse string buffers in `FromIterator<String>` and `FromIterator<Cow<str>>`; updated the implementations to use these. In practice this translates to at least one fewer allocation when using these APIs.

Additionally, rewrote `Extend` implementations to use `iter.for_each`, which (supposedly) helps the compiler optimize those loops (because iterator adapters are encouraged to provide optimized implementations of `fold` and `try_fold`.
2018-12-06 07:49:01 +01:00
Nathan West 811a2bfe53 Added explainatory comments 2018-12-05 17:46:03 -08:00
Nathan West 823dd8ca33 Fixed mutability 2018-12-05 15:11:32 -08:00
Nathan West 180dcc3118 Optimized string FromIterator impls 2018-12-05 14:51:04 -08:00
ljedrz d0c64bb296 cleanup: remove static lifetimes from consts 2018-12-04 12:46:10 +01:00
Hidehito Yabuuchi 1e18cc916f Update issue number of shrink_to methods to point the tracking issue 2018-12-02 16:08:08 +09:00
ljedrz 591607d237 String: add a FIXME to from_utf16 2018-11-21 10:17:54 +01:00
Pietro Albini 3b40434940 Rollup merge of #55530 - ljedrz:speed_up_String_from_utf16, r=SimonSapin
Speed up String::from_utf16

Collecting into a `Result` is idiomatic, but not necessarily fast due to rustc not being able to preallocate for the resulting collection. This is fine in case of an error, but IMO we should optimize for the common case, i.e. a successful conversion.

This changes the behavior of `String::from_utf16` from collecting into a `Result` to pushing to a preallocated `String` in a loop.

According to [my simple benchmark](https://gist.github.com/ljedrz/953a3fb74058806519bd4d640d6f65ae) this change makes `String::from_utf16` around **twice** as fast.
2018-11-15 11:04:31 +01:00
Denis Vasilik 6f3add34ca Minor style guide corrections. 2018-11-11 20:11:25 +01:00
Denis Vasilik dc0fd65af2 Whitespace cleanup according to Rust's style guidelines. 2018-11-11 16:58:00 +01:00
Denis Vasilik f0bfbd3e72 Added comments for trait implementations. 2018-11-11 15:44:15 +01:00
Denis Vasilik 93b5112c16 Added comment for From trait implementation: boxed string slice to String 2018-11-11 15:43:43 +01:00
kennytm 9d9146ad95 Rollup merge of #55734 - teresy:shorthand-fields, r=davidtwco
refactor: use shorthand fields

refactor: use shorthand for single fields everywhere (excluding tests).
2018-11-07 21:27:00 +08:00
teresy eca11b99a7 refactor: use shorthand fields 2018-11-06 15:05:44 -05:00
ljedrz 19aa10132c Speed up String::from_utf16 2018-10-31 10:22:22 +01:00
Hsiang-Cheng Yang 4cb611f3d1 Update string.rs
remove unused variable i in example String::with_capacity()
2018-10-27 14:33:07 +08:00
Matthias Krüger d24070bf8d liballoc: mark str.to_owned() and String::from(&str) as #[inline].
Fixes #53681
2018-09-27 12:08:03 +02:00
Guillaume Gomez c1ad1b0338 Fix invalid urls 2018-09-06 23:32:30 +02:00
Matthias Krüger 11f3918ca2 docs: std::string::String.repeat(): slightly rephrase to be more in-line with other descriptions.
add ticks around a few keywords in other descriptions.
2018-08-22 08:35:34 +02:00
Corey Farwell 993fb93464 Replace usages of ptr::offset with ptr::{add,sub}. 2018-08-20 07:28:34 -04:00
Corey Farwell ec18991492 Add links to std::char::REPLACEMENT_CHARACTER from docs.
There are a few places where we mention the replacement character in the
docs, and it could be helpful for users to utilize the constant which is
available in the standard library, so let’s link to it!
2018-08-11 15:42:35 -04:00
kennytm 67cf3ba528 Rollup merge of #51807 - newpavlov:deprecate_str_slice, r=alexcrichton
Deprecation of str::slice_unchecked(_mut)

Closes #51715

I am not sure if 1.28.0 or 1.29.0 should be used for deprecation version, for now it's 1.28.0.

Additionally I've replaced `slice_unchecked` uses with `get_unchecked`. The only places where this method is still used are `src/liballoc/tests/str.rs` and `src/liballoc/tests/str.rs`.
2018-07-22 22:10:09 +08:00
Simon Sapin b0547cea0a Move core::alloc::CollectionAllocErr to alloc::collections 2018-06-29 14:01:33 +02:00
newpavlov 7c316090eb Deprecation of str::slice_uncheked(_mut) 2018-06-26 13:34:42 +03:00
Cory Sherman 1440f300d8 stabilize RangeBounds collections_range #30877
rename RangeBounds::start() -> start_bound()
rename RangeBounds::end() -> end_bound()
2018-05-24 05:01:40 -07:00
kennytm 8366780164 Rollup merge of #50170 - burtonageo:more_cow_from, r=alexcrichton
Implement From for more types on Cow

This is basically https://github.com/rust-lang/rust/pull/48191, except that it should be implemented in a way that doesn't break third party crates.
2018-05-17 05:22:07 +08:00
George Burton 17e262880c Update features to 1.28.0 2018-05-09 07:23:02 +01:00
F001 160063aad2 make String::new() const 2018-05-05 16:38:27 +08:00