Skip to content

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

Merged
merged 1 commit into from
May 23, 2015

Conversation

lilyball
Copy link
Contributor

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() };

@rust-highfive
Copy link
Contributor

r? @alexcrichton

(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")]
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 19, 2015
@alexcrichton
Copy link
Member

I'm personally ok with to_str and to_string_lossy, especially because they mirror what OsStr already has.

@alexcrichton
Copy link
Member

Thanks for the PR @kballard!

@lilyball lilyball force-pushed the ffi-cstr-to-str-convenience branch from 1114b89 to ff3b5df Compare May 19, 2015 18:40
@lilyball
Copy link
Contributor Author

@alexcrichton I've updated the PR to reflect the requested changes.

@alexcrichton
Copy link
Member

@bors: r+ ff3b5df

Thanks @kballard!

@bors
Copy link
Collaborator

bors commented May 20, 2015

⌛ Testing commit ff3b5df with merge a4600d2...

@bors
Copy link
Collaborator

bors commented May 20, 2015

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Collaborator

bors commented May 20, 2015

⌛ Testing commit ff3b5df with merge 0637fa5...

@bors
Copy link
Collaborator

bors commented May 20, 2015

💔 Test failed - auto-mac-64-opt

@lilyball lilyball force-pushed the ffi-cstr-to-str-convenience branch from ff3b5df to 5a3421e Compare May 20, 2015 23:23
@lilyball
Copy link
Contributor Author

@bors: r=alexcrichton

@bors
Copy link
Collaborator

bors commented May 20, 2015

📌 Commit 5a3421e has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented May 20, 2015

⌛ Testing commit 5a3421e with merge 832839c...

@bors
Copy link
Collaborator

bors commented May 21, 2015

💔 Test failed - auto-mac-64-opt

@lilyball lilyball force-pushed the ffi-cstr-to-str-convenience branch from 5a3421e to d0b5eb3 Compare May 22, 2015 18:46
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() };
@lilyball
Copy link
Contributor Author

@bors: r=alexcrichton

@bors
Copy link
Collaborator

bors commented May 22, 2015

📌 Commit d0b5eb3 has been approved by alexcrichton

oli-obk pushed a commit to oli-obk/rust that referenced this pull request May 23, 2015
…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() };
bors added a commit that referenced this pull request May 23, 2015
…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() };
@bors
Copy link
Collaborator

bors commented May 23, 2015

⌛ Testing commit d0b5eb3 with merge 4c2ebc3...

@bors bors merged commit d0b5eb3 into rust-lang:master May 23, 2015
@lilyball lilyball deleted the ffi-cstr-to-str-convenience branch July 26, 2015 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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