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

std::str: stop from_utf8 allocating. #10701

Merged
merged 2 commits into from
Dec 4, 2013
Merged

Conversation

huonw
Copy link
Member

@huonw huonw commented Nov 28, 2013

This function had type &[u8] -> ~str, i.e. it allocates a string
internally, even though the non-allocating version that take &[u8] ->
&str and ~[u8] -> ~str are all that is necessary in most circumstances.

@chris-morgan
Copy link
Member

So, to maintain the same semantics—where what from_utf8 did actually was what was intended—one will need to change str::from_utf8(slice) to str::from_utf8_owned(slice.to_owned()), which looks like it should be comparable in efficiency.

In cases where auto-slicing was employed (i.e. where the argument was actually ~[u8] rather than &[u8]), either pass it directly to str::from_utf8_owned or clone it before passing it in, as desired.

chris-morgan added a commit to chris-morgan/rust-http that referenced this pull request Nov 28, 2013
@alexcrichton
Copy link
Member

I'm a little concerned about this, on one hand it seems unfortunate that we're making this fairly common api more difficult to use, but on the other hand I like how this is much more explicit about storage needs.

It'd be awesome if we had the same storage benefits from using only str::from_utf8, but I'm not sure if that's possible. Regardless, I'd like this to have some more discussion before merging.

@huonw
Copy link
Member Author

huonw commented Nov 28, 2013

By more difficult to use you mean "one has to work out if one wants a ~str or &str" rather than just letting the library hand you a ~str always?

FWIW, I think it's not that bad, as one can see from this change just using from_utf8_owned and/or from_utf8_slice is a drop in replacement in most situations, with from_utf8_owned(....to_owned()) mostly only necessary due to other bugs (specifically, extra::json using @mut Writer everywhere, so one can't move out of the @mut MemWriter).

@huonw
Copy link
Member Author

huonw commented Dec 1, 2013

I just added a commit that renames what was from_utf8_slice into from_utf8, so this PR now has the effect of making from_utf8 be &[u8] -> &str and removing from_utf8_slice. (I'm happy to remove/amend that commit if that's deemed suboptimal/has a better suggestion.)

huonw added 2 commits December 4, 2013 22:35
This function had type &[u8] -> ~str, i.e. it allocates a string
internally, even though the non-allocating version that take &[u8] ->
&str and ~[u8] -> ~str are all that is necessary in most circumstances.
bors added a commit that referenced this pull request Dec 4, 2013
This function had type &[u8] -> ~str, i.e. it allocates a string
internally, even though the non-allocating version that take &[u8] ->
&str and ~[u8] -> ~str are all that is necessary in most circumstances.
@bors bors closed this Dec 4, 2013
@bors bors merged commit b0426ed into rust-lang:master Dec 4, 2013
chris-morgan added a commit to chris-morgan/rust-http that referenced this pull request Dec 5, 2013
@huonw huonw deleted the rm-from_utf8 branch February 25, 2014 05:28
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 23, 2023
…, r=llogiq

Bugfix: Ignore `impl Trait`(s) @ `let_underscore_untyped`

Fixes rust-lang#10411

changelog:[`let_underscore_untyped`]: Ignore `impl Trait`(s) that caused false positives.
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.

5 participants