-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,10 +37,10 @@ | |
//! data lazily when mutation or ownership is required. The type is designed to | ||
//! work with general borrowed data via the `BorrowFrom` trait. | ||
//! | ||
//! `Cow` implements both `Deref` and `DerefMut`, which means that you can call | ||
//! 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 commentThe reason will be displayed to describe this comment to others. Learn more. Extraneous "both" |
||
//! non-mutating methods directly on the data it encloses. If mutation | ||
//! is desired, `to_mut` will obtain a mutable references to an owned | ||
//! value, cloning if necessary. | ||
|
||
#![unstable = "recently added as part of collections reform"] | ||
|
||
|
@@ -84,15 +84,31 @@ impl<T> ToOwned<T> for T where T: Clone { | |
} | ||
|
||
/// A clone-on-write smart pointer. | ||
pub enum Cow<'a, T, B: 'a> where B: ToOwned<T> { | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::borrow::Cow; | ||
/// | ||
/// fn abs_all(input: &mut Cow<Vec<int>, [int]>) { | ||
/// for i in range(0, input.len()) { | ||
/// let v = input[i]; | ||
/// if v < 0 { | ||
/// // clones into a vector the first time (if not already owned) | ||
/// input.to_mut()[i] = -v; | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
pub enum Cow<'a, T, Sized? B: 'a> where B: ToOwned<T> { | ||
/// Borrowed data. | ||
Borrowed(&'a B), | ||
|
||
/// Owned data. | ||
Owned(T) | ||
} | ||
|
||
impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> { | ||
impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> { | ||
/// Acquire a mutable reference to the owned form of the data. | ||
/// | ||
/// Copies the data if it is not already owned. | ||
|
@@ -117,7 +133,7 @@ impl<'a, T, B> Cow<'a, T, B> where B: ToOwned<T> { | |
} | ||
} | ||
|
||
impl<'a, T, B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> { | ||
impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> { | ||
fn deref(&self) -> &B { | ||
match *self { | ||
Borrowed(borrowed) => borrowed, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.