diff options
| author | bors <bors@rust-lang.org> | 2014-03-25 23:41:57 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-03-25 23:41:57 -0700 |
| commit | 6bac5607c963c61d488a0d832458341589a560b3 (patch) | |
| tree | 09044418836d5cd7ff22f8fb78ae892c8eeff142 /src/rustllvm/RustWrapper.cpp | |
| parent | e28f081cc257122fed7a2fb9d3358f3ac9829245 (diff) | |
| parent | 6200e761f0ef58510ad2acc383b29de7e7a79bcd (diff) | |
| download | rust-6bac5607c963c61d488a0d832458341589a560b3.tar.gz rust-6bac5607c963c61d488a0d832458341589a560b3.zip | |
auto merge of #13039 : Kimundi/rust/iter_by_value_extend, r=alexcrichton
# Summary Changed `iter::Extendable` and `iter::FromIterator` to take a `Iterator` by value. These functions always exhaust the passed `Iterator`, and are often used for transferring the values of a new `Iterator` directly into a data structure, so using them usually require the use of the `&mut` operator: ``` foo.extend(&mut bar.move_iter()); // Transfer content from bar into foo let mut iter = ...; foo.extend(&mut iter); // iter is now empty ``` This patch changes both the `FromIterator` and `Extendable` traits to take the iterator by value instead, which makes the common case of using these traits less heavy: ``` foo.extend(bar.move_iter()); // Transfer content from bar into foo let iter = ...; foo.extend(iter); // iter is now inaccessible if it moved // or unchanged if it was Pod and copied. ``` # Composability This technically makes the traits less flexible from a type system pov, because they now require ownership. However, because `Iterator` provides the `ByRef` adapter, there is no loss of functionality: ``` foo.extend(iter.by_ref()); // Same semantic as today, for the few situations where you need it. ``` # Motivation This change makes it less painful to use iterators for shuffling values around between collections, which makes it more acceptable to always use them for this, enabling more flexibility. For example, `foo.extend(bar.move_iter())` can generally be the fastest way to append an collections content to another one, without both needing to have the same type. Making this easy to use would allow the removal of special cased methods like `push_all()` on vectors. (See https://github.com/mozilla/rust/issues/12456) I opened https://github.com/mozilla/rust/issues/13038 as well, to discuss this change in general if people object to it. # Further work This didn't change the `collect()` method to take by value `self`, nor any of the other adapters that also exhaust their iterator argument. For consistency this should probably happen in the long term, but for now this is too much trouble, as every use of them would need to be checked for accidentally changed semantic by going `&mut self -> self`. (which allows for the possibility that a `Pod` iterator got copied instead of exhausted without generating a type error by the change)
Diffstat (limited to 'src/rustllvm/RustWrapper.cpp')
0 files changed, 0 insertions, 0 deletions
