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

Implement std::ascii::AsciiExt for Cow<str>. #42872

Closed
wants to merge 6 commits into from

Conversation

frewsxcv
Copy link
Member

@frewsxcv frewsxcv commented Jun 24, 2017

I was in a situation where I wanted to call make_ascii_uppercase on a Cow<str> and noticed the only way to do that is to clone the data in the Cow (I think?).

Opening this a little prematurely to make sure this eligible consideration. If it is, i'll need to clean it up a little, fix a but where it doesn't process a byte (see the FIXME), and add some tests. Also, if this is desired, I can extend this to work on Cow<[u8]>.

@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.

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

I was in a situation where I wanted to call make_ascii_uppercase on a Cow and noticed the only way to do that is to clone the data in the Cow (I think?).

That seems sensible? make_ascii_uppercase can mutate the data in the string, so if you have a Cow<str>, the only thing you can do is make sure it's a Cow::Owned. Which seems like what you're doing in this PR anyway, so I'm not sure what this would gain you?

This example shows how to call make_ascii_uppercase on a Cow<str>: https://is.gd/zQNWgb --- Is there something I'm missing?

@frewsxcv
Copy link
Member Author

frewsxcv commented Jun 24, 2017

That seems sensible? make_ascii_uppercase can mutate the data in the string, so if you have a Cow<str>, the only thing you can do is make sure it's a Cow::Owned. Which seems like what you're doing in this PR anyway, so I'm not sure what this would gain you?

Here's a scenario mutating the underlying str in a Cow<str>:

let mut x = Cow::Borrowed("hello world");
x.to_mut().make_ascii_lowercase();
assert_eq!(Cow::Owned("hello world"), x);

The gain with my changes here would be:

let mut x = Cow::Borrowed("hello world");
x.make_ascii_lowercase();
assert_eq!(Cow::Borrowed("hello world"), x);

Notice that the Cow is still a Cow::Borrowed

@@ -947,6 +948,119 @@ impl AsciiExt for char {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> AsciiExt for Cow<'a, str> {
type Owned = Cow<'a, str>;
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure this is a breaking change. Cow::Borrowed("Hello, world!").to_ascii_uppercase() currently returns a String. It's a shame as to_ascii_{upper,lower}case could benifit from the same optimization as make_ascii_{upper,lower}case.


#[inline]
fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
self.to_ascii_lowercase() == other.to_ascii_lowercase()
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 be (**self).eq_ignore_ascii_case(**other) to avoid allocations.

Some(p) => p,
None => return,
};
let mut bytes = Vec::with_capacity(self.len());
Copy link
Member

Choose a reason for hiding this comment

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

If self is a Cow::Owned already then this shouldn't create a new Vec.

@frewsxcv
Copy link
Member Author

Ugh. As per this comment, this seems like a breaking change. I'm going to close this unless we're overlooking something.

@ollie27
Copy link
Member

ollie27 commented Jun 24, 2017

You might be able to use type Owned = String.

@ollie27
Copy link
Member

ollie27 commented Jun 24, 2017

Although Cow::Borrowed("hello").eq_ignore_ascii_case("HELLO") would still break which has been a problem in the past: #32074. So yeah, I don't think this impl is possible unfortunately.

@Mark-Simulacrum Mark-Simulacrum added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 24, 2017
@Mark-Simulacrum
Copy link
Member

I'm going to close this since it unfortunately doesn't seem possible. Thanks, though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

5 participants