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

Deprecate some unsafe functions in str::raw and remove OwnedStr trait #15807

Closed
wants to merge 8 commits into from
Closed

Conversation

aochagavia
Copy link
Contributor

Removed the OwnedStr trait

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 (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 of string::raw::from_buf_len
  • from_c_str in favor of string::raw::from_buf
  • from_utf8_owned in favor of string::raw::from_utf8
  • from_byte in favor of string::raw::from_utf8 (make a new vector with a single byte and call the function)
  • String::from_raw_parts in favor of string::raw::from_parts

[breaking-change]

@aochagavia aochagavia changed the title Deprecate unsafe functions in str::raw and OwnedStr trait Deprecate unsafe functions in str::raw and remove OwnedStr trait Jul 19, 2014
@aochagavia
Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@aochagavia
Copy link
Contributor Author

This is now ready to merge! :)

@alexcrichton
Copy link
Member

I'm personally not a huge fan of CString::new(foo, false).as_str().unwrap().to_string() vs String::from_c_str(foo), but I agree that it is more consistent.

@aochagavia
Copy link
Contributor Author

In #15859 I implement Show for CString. If that gets merged you could actually use CString::new(foo, false).to_string()

@aturon
Copy link
Member

aturon commented Jul 21, 2014

@aochagavia Could you spell out in more detail the motivation for deprecating/moving the functionality in raw? As a general rule, APIs shy away from exporting unsafe functions except in very explicit ways, and raw submodules are a common way to do this. (See for example the draft gudelines on opt-out of dynamic enforcement)

@aochagavia
Copy link
Contributor Author

@aturon I don't pretend to deprecate all functions in raw, but only the ones returning a String. I have replaced them by methods of String, because it seems to me that it is the right place for them.

@aturon
Copy link
Member

aturon commented Jul 21, 2014

@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 raw submodule instead, to make it very clear that they're meant for working directly with the underlying representation of the type. This is partly a way of discouraging their use except for the lowest-level code. I think we're generally happy with that convention.

@aochagavia
Copy link
Contributor Author

String::from_utf8_unchecked looks much better to me than str::raw::from_utf8_owned, which looks as if it returned a &str.

Maybe an alternative is to create a raw module inside of string.

@aturon
Copy link
Member

aturon commented Jul 21, 2014

@aochagavia Ah, yes, I would definitely be in favor of providing these functions as string::raw.

@aochagavia
Copy link
Contributor Author

Then I will make the necessary changes and post here when I am done. Thanks for reviewing!

@aturon
Copy link
Member

aturon commented Jul 21, 2014

@aochagavia My pleasure -- and thanks for continuing this work!

@aochagavia
Copy link
Contributor Author

Should I also move the from_raw_parts unsafe constructor already present in String?

@aturon
Copy link
Member

aturon commented Jul 21, 2014

@aochagavia Yes, please move from_raw_parts as well.

@aochagavia aochagavia changed the title Deprecate unsafe functions in str::raw and remove OwnedStr trait Deprecate some unsafe functions in str::raw and remove OwnedStr trait Jul 21, 2014
@aochagavia
Copy link
Contributor Author

I have moved the functions to string::raw. I renamed String::from_raw_parts to string::raw::from_parts because I thought it was better than having string::raw::from_raw_parts (two times raw).

Now we have to wait for Travis...

@alexcrichton
Copy link
Member

r? @aturon

Also, for comparison, the std::str::raw module seems like it should closely mirror the std::string::raw module, and the std::str modules contains c_str_to_static_slice which can be construed as analgous to from_c_str.

@aturon
Copy link
Member

aturon commented Jul 22, 2014

I agree that we should align std::str::raw.

@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 string::raw::from_buf, as a replacement for from_c_str (and analogous to string::raw::from_buf_len? If we're offering from_buf_len, it seems to me we could also give the convenience of from_buf. That would resolve the ergonomic regression here, and I think still give a consistent API.

Thoughts?

@aochagavia
Copy link
Contributor Author

I think it is a good idea! Let's do it.

@aochagavia
Copy link
Contributor Author

I have added the from_buf function.

Additionaly, I have reordered the arguments of from_parts to match the order of from_buf and from_buf_len: first the pointer, then the lenght, then the capacity.

@aturon
Copy link
Member

aturon commented Jul 22, 2014

@aochagavia Thanks! One more request: there were a few places in the patch that were previously using from_c_str and are now going through CString somewhat verbosely. Can those go through from_buf instead?

@@ -1376,16 +1320,6 @@ mod tests {
}

#[test]
fn test_raw_from_c_str() {
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 now test from_buf.

Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@aochagavia
Copy link
Contributor Author

I have already catched those 😉

EDIT: it seems that I didn't catch all of them!

@aturon
Copy link
Member

aturon commented Jul 22, 2014

@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]
@aochagavia
Copy link
Contributor Author

This should be ok now

@aochagavia aochagavia deleted the str branch October 13, 2014 18:16
lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 3, 2024
…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
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.

None yet

6 participants