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

Add a resource-reusing method to ToOwned #41009

Merged
merged 1 commit into from
Apr 13, 2017

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Apr 2, 2017

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 Borrows/AsRefs 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.)

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 2, 2017

Odd, that error didn't show up from python x.py test src\tools\tidy locally. Hopefully this fixes it...

@frewsxcv
Copy link
Member

frewsxcv commented Apr 2, 2017

Odd, that error didn't show up from python x.py test src\tools\tidy locally. Hopefully this fixes it...

That tidy lint landed recently, so your local git might not have it yet.

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 2, 2017
@BurntSushi
Copy link
Member

cc @rust-lang/libs

@bors
Copy link
Contributor

bors commented Apr 6, 2017

☔ The latest upstream changes (presumably #41102) made this pull request unmergeable. Please resolve the merge conflicts.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 7, 2017

Rebased; looks like it's applying cleanly again.

@alexcrichton
Copy link
Member

Seems like a reasonable addition to me!

/// let mut v: Vec<i32> = Vec::new();
/// [1, 2][..].clone_into(&mut v);
/// ```
#[unstable(feature = "toowned_clone_into", issue = "123456789")] // FIXME
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the FIXME here for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue # I'm assuming

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remind me to put a tracking issue in. If this has a plausible chance of acceptance, I can go create a placeholder now and put a real number.

@alexcrichton
Copy link
Member

Discussed at libs triage the conclusion was to merge! @scottmcm want to update the tracking issue and I'll r+?

to_owned generalizes clone; this generalizes clone_from.  Use to_owned to
give it a default impl.  Customize the impl for [T], str, and T:Clone.

Use it in Cow::clone_from to reuse resources when cloning Owned into Owned.
@scottmcm
Copy link
Member Author

Added tracking issue #41263 and squashed down to a single commit.

@alexcrichton
Copy link
Member

@bors: r+

Thanks @scottmcm!

@bors
Copy link
Contributor

bors commented Apr 13, 2017

📌 Commit 7ec27ae has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Apr 13, 2017

⌛ Testing commit 7ec27ae with merge 6c03efd...

bors added a commit that referenced this pull request Apr 13, 2017
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.)
@bors
Copy link
Contributor

bors commented Apr 13, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 6c03efd to master...

@bors bors merged commit 7ec27ae into rust-lang:master Apr 13, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Apr 19, 2017
…crichton

Override ToOwned::clone_into for Path and OsStr

The only non-overridden one remaining is the CStr impl, which cannot
be optimized as doing so would break CString's second invariant.

Follow-up to 7ec27ae (PR rust-lang#41009).

r? @alexcrichton
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Apr 20, 2017
…crichton

Override ToOwned::clone_into for Path and OsStr

The only non-overridden one remaining is the CStr impl, which cannot
be optimized as doing so would break CString's second invariant.

Follow-up to 7ec27ae (PR rust-lang#41009).

r? @alexcrichton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants