rust/src/libcollections
bors 6c03efd8f3 Auto merge of #41009 - scottmcm:toowned-clone-into, r=alexcrichton
Add a resource-reusing method to `ToOwned`

`ToOwned::to_owned` generalizes `Clone::clone`, but `ToOwned` doesn't have an equivalent to `Clone::clone_from`.  This PR adds such a method as `clone_into` under a new unstable feature `toowned_clone_into`.

Analogous to `clone_from`, this has the obvious default implementation in terms of `to_owned`.  I've updated the `libcollections` impls: for `T:Clone` it uses `clone_from`, for `[T]` I moved the code from `Vec::clone_from` and implemented that in terms of this, and for `str` it's a predictable implementation in terms of `[u8]`.

Used it in `Cow::clone_from` to reuse resources when both are `Cow::Owned`, and added a test that `Cow<str>` thus keeps capacity in `clone_from` in that situation.

The obvious question: is this the right place for the method?
- It's here so it lives next to `to_owned`, making the default implementation reasonable, and avoiding another trait.  But allowing method syntax forces a name like `clone_into`, rather than something more consistent like `owned_from`.
- Another trait would allow `owned_from` and could support multiple owning types per borrow type.  But it'd be another single-method trait that generalizes `Clone`, and I don't know how to give it a default impl in terms of `ToOwned::to_owned`, since a blanket would mean overlapping impls problems.

I did it this way as it's simpler and many of the `Borrow`s/`AsRef`s don't make sense with `owned_from` anyway (`[T;1]:Borrow<[T]>`, `Arc<T>:Borrow<T>`, `String:AsRef<OsStr>`...).  I'd be happy to re-do it the other way, though, if someone has a good solution for the default handling.

(I can also update with `CStr`, `OsStr`, and `Path` once a direction is decided.)
2017-04-13 06:46:29 +00:00
..
benches Implement feature sort_unstable 2017-03-21 20:46:20 +01:00
btree Apply clippy's doc_markdown improvements to libcollections 2017-04-10 05:53:10 +02:00
tests Add ToOwned::clone_into (unstable as toowned_clone_into) 2017-04-12 17:21:15 -07:00
binary_heap.rs Various fixes to wording consistency in the docs 2017-03-22 17:19:52 +01:00
borrow.rs Add ToOwned::clone_into (unstable as toowned_clone_into) 2017-04-12 17:21:15 -07:00
Cargo.toml Move libXtest into libX/tests 2017-04-03 20:49:39 +02:00
enum_set.rs Apply clippy's doc_markdown improvements to libcollections 2017-04-10 05:53:10 +02:00
fmt.rs Rollup merge of #40241 - Sawyer47:fix-39997, r=alexcrichton 2017-03-19 20:51:06 -04:00
lib.rs Rollup merge of #40559 - nagisa:manually-drop, r=alexcrichton 2017-04-11 18:36:12 -04:00
linked_list.rs Fix links 2017-04-04 15:39:44 -04:00
macros.rs Panic on errors in format! or <T: Display>::to_string 2017-03-01 23:47:59 +01:00
range.rs Apply clippy's doc_markdown improvements to libcollections 2017-04-10 05:53:10 +02:00
slice.rs Auto merge of #41009 - scottmcm:toowned-clone-into, r=alexcrichton 2017-04-13 06:46:29 +00:00
str.rs Auto merge of #41009 - scottmcm:toowned-clone-into, r=alexcrichton 2017-04-13 06:46:29 +00:00
string.rs Reduce str transmutes, add mut versions of methods. 2017-04-09 19:13:54 -04:00
vec.rs Auto merge of #41009 - scottmcm:toowned-clone-into, r=alexcrichton 2017-04-13 06:46:29 +00:00
vec_deque.rs Apply clippy's doc_markdown improvements to libcollections 2017-04-10 05:53:10 +02:00