-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Deprecate some unsafe functions in str::raw
and remove OwnedStr
trait
#15807
Conversation
str::raw
and OwnedStr
traitstr::raw
and remove OwnedStr
trait
cc @aturon |
/// the utf-8-ness of the vector has already been validated. | ||
#[inline] | ||
pub unsafe fn from_bytes(bytes: Vec<u8>) -> String { | ||
mem::transmute(bytes) |
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.
Now that this is in the same module as String, the transmute could be removed to just be String { vec: bytes }
, yeah?
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.
Good idea!
On Jul 19, 2014 8:37 PM, "Steven Sheldon" notifications@github.com wrote:
In src/libcollections/string.rs:
@@ -386,6 +400,14 @@ impl String {
}
}
- /// Converts a vector of bytes to a new
String
without checking if- /// it contains valid UTF-8. This is unsafe because it assumes that
- /// the utf-8-ness of the vector has already been validated.
- #[inline]
- pub unsafe fn from_bytes(bytes: Vec) -> String {
mem::transmute(bytes)
Now that this is in the same module as String, the transmute could be
removed to just be String { vec: bytes }, yeah?—
Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust/pull/15807/files#r15145056.
This is now ready to merge! :) |
I'm personally not a huge fan of |
In #15859 I implement |
@aochagavia Could you spell out in more detail the motivation for deprecating/moving the functionality in |
@aturon I don't pretend to deprecate all functions in |
@aochagavia Right, I think I understand the general thinking here that constructors for a given type live directly on that type. But for unsafe constructors/operations in particular, we have a widespread convention of placing them in a |
Maybe an alternative is to create a |
@aochagavia Ah, yes, I would definitely be in favor of providing these functions as |
Then I will make the necessary changes and post here when I am done. Thanks for reviewing! |
@aochagavia My pleasure -- and thanks for continuing this work! |
Should I also move the |
@aochagavia Yes, please move |
str::raw
and remove OwnedStr
traitstr::raw
and remove OwnedStr
trait
I have moved the functions to Now we have to wait for Travis... |
r? @aturon Also, for comparison, the |
I agree that we should align @aochagavia Looks like this is passing Travis now, and the code looks good to me, but before I sign off, I wanted to ask how you'd feel about a Thoughts? |
I think it is a good idea! Let's do it. |
I have added the Additionaly, I have reordered the arguments of |
@aochagavia Thanks! One more request: there were a few places in the patch that were previously using |
@@ -1376,16 +1320,6 @@ mod tests { | |||
} | |||
|
|||
#[test] | |||
fn test_raw_from_c_str() { |
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 now test from_buf
.
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.
(and move to string
to do so)
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.
done!
I have already catched those 😉 EDIT: it seems that I didn't catch all of them! |
@aochagavia Note Travis error. |
This trait was only implemented by `String`. It provided the methods `into_bytes` and `append`, both of which **are already implemented as normal methods** of `String` (not as trait methods). This change improves the consistency of strings. This shouldn't break any code, except if somebody has implemented `OwnedStr` for a user-defined type.
Use `string::raw::from_buf` instead [breaking-change]
Replaced by `string::raw::from_utf8` [breaking-change]
Use `string:raw::from_utf8` instead [breaking-change]
Replaced by `string::raw::from_buf_len` [breaking-change]
Replaced by `string::raw::from_parts` [breaking-change]
This should be ok now |
…eykril fix: assists panic when trying to edit usage inside macro When we try to make a syntax node mutable inside a macro to edit it, it seems like the edits aren't properly reflected and will cause a panic when trying to make another syntax node mutable. This PR changes `bool_to_enum` and `promote_local_to_const` to use the original syntax range instead to edit the original file instead of the macro file. I'm not sure how to do it for `inline_call` with the example I mentioned in the issue, so I've left it out for now. Fixes rust-lang#15807
Removed the
OwnedStr
traitThis trait was only implemented by
String
. It provided the methodsinto_bytes
andappend
, both of which are already implemented as normal methods ofString
(they were duplicated). This change improves the consistency of strings.Its removal shouldn't break any code, except if somebody has implemented
OwnedStr
for his own types.Deprecated some unsafe functions
from_buf_len
in favor ofstring::raw::from_buf_len
from_c_str
in favor ofstring::raw::from_buf
from_utf8_owned
in favor ofstring::raw::from_utf8
from_byte
in favor ofstring::raw::from_utf8
(make a new vector with a single byte and call the function)String::from_raw_parts
in favor ofstring::raw::from_parts
[breaking-change]