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

libs: make Cow usable, improve documentation #19157

Closed
wants to merge 1 commit into from

Conversation

aturon
Copy link
Member

@aturon aturon commented Nov 20, 2014

This commit makes Cow more usable by allowing it to be applied to
unsized types (as was intended) and providing some basic ToOwned
implementations on slice types. It also corrects the documentation for
Cow to no longer mention DerefMut, and adds an example.

Closes #19123

This commit makes `Cow` more usable by allowing it to be applied to
unsized types (as was intended) and providing some basic `ToOwned`
implementations on slice types. It also corrects the documentation for
`Cow` to no longer mention `DerefMut`, and adds an example.
//! methods directly on the data it encloses. The first time a mutable reference
//! is required, the data will be cloned (via `to_owned`) if it is not
//! already owned.
//! `Cow` implements both `Deref`, which means that you can call
Copy link
Contributor

Choose a reason for hiding this comment

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

Extraneous "both"

@bstrie
Copy link
Contributor

bstrie commented Nov 20, 2014

This is actually really cool, I didn't know we had this 🐄

@@ -609,6 +609,11 @@ impl BorrowFrom<String> for str {
fn borrow_from(owned: &String) -> &str { owned[] }
}

#[unstable = "trait is unstable"]
impl ToOwned<String> for str {
fn to_owned(&self) -> String { self.to_string() }
Copy link
Member

Choose a reason for hiding this comment

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

This should use into_string. to_string goes through the formatting infrastructure, and tends to over-allocate space in the heap. See this playpen example, and also see #16415.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Nov 22, 2014
This commit makes `Cow` more usable by allowing it to be applied to
unsized types (as was intended) and providing some basic `ToOwned`
implementations on slice types. It also corrects the documentation for
`Cow` to no longer mention `DerefMut`, and adds an example.
bors added a commit that referenced this pull request Nov 23, 2014
This commit makes `Cow` more usable by allowing it to be applied to
unsized types (as was intended) and providing some basic `ToOwned`
implementations on slice types. It also corrects the documentation for
`Cow` to no longer mention `DerefMut`, and adds an example.

Closes #19123
@bors bors closed this Nov 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The borrow module docs claim Cow implements DerefMut but it doesn't
5 participants