-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add some convenience methods to go from CStr -> str #25416
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
/// > after a 0-cost cast, but it is planned to alter its definition in the | ||
/// > future to perform the length calculation in addition to the UTF-8 | ||
/// > check whenever this method is called. | ||
#[unstable(feature = "cstr", reason = "recently added")] |
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.
we've been trying to use more fine-grained feature names than just the module, maybe 'cstr_convenience' or something?
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.
I actually chose the name based on the type, not the module (using the new "duration"
feature as a precedent).
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.
I agree with @steveklabnik that we're trying to cut down on feature names. In this case cstr
is somewhat ok because there's not an already existing blanket cstr
feature, but I'd personally prefer that these new APIs be added as cstr_to_str
for example.
I'm personally ok with |
Thanks for the PR @kballard! |
1114b89
to
ff3b5df
Compare
@alexcrichton I've updated the PR to reflect the requested changes. |
⌛ Testing commit ff3b5df with merge a4600d2... |
⛄ The build was interrupted to prioritize another pull request. |
⌛ Testing commit ff3b5df with merge 0637fa5... |
💔 Test failed - auto-mac-64-opt |
ff3b5df
to
5a3421e
Compare
@bors: r=alexcrichton |
📌 Commit 5a3421e has been approved by |
⌛ Testing commit 5a3421e with merge 832839c... |
💔 Test failed - auto-mac-64-opt |
5a3421e
to
d0b5eb3
Compare
A common problem when working with FFI right now is converting from raw C strings into `&str` or `String`. Right now you're required to say something like let cstr = unsafe { CStr::from_ptr(ptr) }; let result = str::from_utf8(cstr.to_bytes()); This is slightly awkward, and is not particularly intuitive for people who haven't used the ffi module before. We can do a bit better by providing some convenience methods on CStr: fn to_str(&self) -> Result<&str, str::Utf8Error> fn to_string_lossy(&self) -> Cow<str> This will make it immediately apparent to new users of CStr how to get a string from a raw C string, so they can say: let s = unsafe { CStr::from_ptr(ptr).to_string_lossy() };
@bors: r=alexcrichton |
📌 Commit d0b5eb3 has been approved by |
…e, r=alexcrichton This was motivated by http://www.evanmiller.org/a-taste-of-rust.html. A common problem when working with FFI right now is converting from raw C strings into `&str` or `String`. Right now you're required to say something like let cstr = unsafe { CStr::from_ptr(ptr) }; let result = str::from_utf8(cstr.to_bytes()); This is slightly awkward, and is not particularly intuitive for people who haven't used the ffi module before. We can do a bit better by providing some convenience methods on CStr: fn to_str(&self) -> Result<&str, str::Utf8Error> fn to_string_lossy(&self) -> Cow<str> This will make it immediately apparent to new users of CStr how to get a string from a raw C string, so they can say: let s = unsafe { CStr::from_ptr(ptr).to_string_lossy() };
…ichton This was motivated by http://www.evanmiller.org/a-taste-of-rust.html. A common problem when working with FFI right now is converting from raw C strings into `&str` or `String`. Right now you're required to say something like let cstr = unsafe { CStr::from_ptr(ptr) }; let result = str::from_utf8(cstr.to_bytes()); This is slightly awkward, and is not particularly intuitive for people who haven't used the ffi module before. We can do a bit better by providing some convenience methods on CStr: fn to_str(&self) -> Result<&str, str::Utf8Error> fn to_string_lossy(&self) -> Cow<str> This will make it immediately apparent to new users of CStr how to get a string from a raw C string, so they can say: let s = unsafe { CStr::from_ptr(ptr).to_string_lossy() };
This was motivated by http://www.evanmiller.org/a-taste-of-rust.html.
A common problem when working with FFI right now is converting from raw
C strings into
&str
orString
. Right now you're required to saysomething like
This is slightly awkward, and is not particularly intuitive for people
who haven't used the ffi module before. We can do a bit better by
providing some convenience methods on CStr:
This will make it immediately apparent to new users of CStr how to get a
string from a raw C string, so they can say: