-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Conversation
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extraneous "both"
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() } |
There was a problem hiding this comment.
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.
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.
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
doc: move dev docs to manual
This commit makes
Cow
more usable by allowing it to be applied tounsized types (as was intended) and providing some basic
ToOwned
implementations on slice types. It also corrects the documentation for
Cow
to no longer mentionDerefMut
, and adds an example.Closes #19123