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: Redesign c_str and c_vec #20507

Merged
merged 1 commit into from
Jan 6, 2015
Merged

Conversation

alexcrichton
Copy link
Member

This commit is an implementation of RFC 494 which removes the entire
std::c_vec module and redesigns the std::c_str module as std::ffi.

The interface of the new CString is outlined in the linked RFC, the primary
changes being:

  • The ToCStr trait is gone, meaning the with_c_str and to_c_str methods
    are now gone. These two methods are replaced with a CString::from_slice
    method.
  • The CString type is now just a wrapper around Vec<u8> with a static
    guarantee that there is a trailing nul byte with no internal nul bytes. This
    means that CString now implements Deref<Target = [c_char]>, which is where
    it gains most of its methods from. A few helper methods are added to acquire a
    slice of u8 instead of c_char, as well as including a slice with the
    trailing nul byte if necessary.
  • All usage of non-owned CString values is now done via two functions inside
    of std::ffi, called c_str_to_bytes and c_str_to_bytes_with_nul. These
    functions are now the one method used to convert a *const c_char to a Rust
    slice of u8.

Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:

[breaking-change]
Closes #20444

@rust-highfive
Copy link
Collaborator

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member Author

r? @aturon

@alexcrichton
Copy link
Member Author

I should also point out that the entire std::ffi module is currently #[unstable] due to the fairly large refactorings that just happened. I'd think that this could become #[stable] pretty quickly after alpha is released or continue to be iterated upon.

@alexcrichton
Copy link
Member Author

cc #20475 a use case for another free function, but not sure if we want a huge array of free functions here.

@@ -343,7 +343,8 @@ impl String {
/// This function is unsafe because we dereference memory until we find the
/// NUL character, which is not guaranteed to be present. Additionally, the
/// slice is not checked to see whether it contains valid UTF-8
#[unstable = "just renamed from `mod raw`"]
#[deprecated = "use ffi::c_str_to_bytes + str::from_utf8 + .to_string()"]
Copy link
Member

Choose a reason for hiding this comment

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

I think we should offer this convenience in ffi, something like ffi::c_str_to_string.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm somewhat worried about a large array of functions popping up in "view this C string as a rust type":

I think I'd be more in favor of sticking to the bare bones for now, and perhaps expanding later? After thinking a bit, I think that #20475 may be able to be handled by slice::from_raw_buf plus a search for 0, and the to_str variant may not actually come up all that often depending on the application. I suppose I'm always a fan of conservativeness, and we do tend to avoid providing too many convenience methods that are just compositions of some others.

@aturon
Copy link
Member

aturon commented Jan 4, 2015

Looks great overall! I think std::ffi is going to work out really nicely. A few nits, a rebase, and then r=me.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 5, 2015
This commit is an implementation of [RFC 494][rfc] which removes the entire
`std::c_vec` module and redesigns the `std::c_str` module as `std::ffi`.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0494-c_str-and-c_vec-stability.md

The interface of the new `CString` is outlined in the linked RFC, the primary
changes being:

* The `ToCStr` trait is gone, meaning the `with_c_str` and `to_c_str` methods
  are now gone. These two methods are replaced with a `CString::from_slice`
  method.
* The `CString` type is now just a wrapper around `Vec<u8>` with a static
  guarantee that there is a trailing nul byte with no internal nul bytes. This
  means that `CString` now implements `Deref<Target = [c_char]>`, which is where
  it gains most of its methods from. A few helper methods are added to acquire a
  slice of `u8` instead of `c_char`, as well as including a slice with the
  trailing nul byte if necessary.
* All usage of non-owned `CString` values is now done via two functions inside
  of `std::ffi`, called `c_str_to_bytes` and `c_str_to_bytes_with_nul`. These
  functions are now the one method used to convert a `*const c_char` to a Rust
  slice of `u8`.

Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:

[breaking-change]
Closes rust-lang#20444
This commit is an implementation of [RFC 494][rfc] which removes the entire
`std::c_vec` module and redesigns the `std::c_str` module as `std::ffi`.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0494-c_str-and-c_vec-stability.md

The interface of the new `CString` is outlined in the linked RFC, the primary
changes being:

* The `ToCStr` trait is gone, meaning the `with_c_str` and `to_c_str` methods
  are now gone. These two methods are replaced with a `CString::from_slice`
  method.
* The `CString` type is now just a wrapper around `Vec<u8>` with a static
  guarantee that there is a trailing nul byte with no internal nul bytes. This
  means that `CString` now implements `Deref<Target = [c_char]>`, which is where
  it gains most of its methods from. A few helper methods are added to acquire a
  slice of `u8` instead of `c_char`, as well as including a slice with the
  trailing nul byte if necessary.
* All usage of non-owned `CString` values is now done via two functions inside
  of `std::ffi`, called `c_str_to_bytes` and `c_str_to_bytes_with_nul`. These
  functions are now the one method used to convert a `*const c_char` to a Rust
  slice of `u8`.

Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:

[breaking-change]
Closes rust-lang#20444
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 6, 2015
This commit is an implementation of [RFC 494][rfc] which removes the entire
`std::c_vec` module and redesigns the `std::c_str` module as `std::ffi`.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0494-c_str-and-c_vec-stability.md

The interface of the new `CString` is outlined in the linked RFC, the primary
changes being:

* The `ToCStr` trait is gone, meaning the `with_c_str` and `to_c_str` methods
  are now gone. These two methods are replaced with a `CString::from_slice`
  method.
* The `CString` type is now just a wrapper around `Vec<u8>` with a static
  guarantee that there is a trailing nul byte with no internal nul bytes. This
  means that `CString` now implements `Deref<Target = [c_char]>`, which is where
  it gains most of its methods from. A few helper methods are added to acquire a
  slice of `u8` instead of `c_char`, as well as including a slice with the
  trailing nul byte if necessary.
* All usage of non-owned `CString` values is now done via two functions inside
  of `std::ffi`, called `c_str_to_bytes` and `c_str_to_bytes_with_nul`. These
  functions are now the one method used to convert a `*const c_char` to a Rust
  slice of `u8`.

Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:

[breaking-change]
Closes rust-lang#20444
@bors bors merged commit ec7a50d into rust-lang:master Jan 6, 2015
@alexcrichton alexcrichton deleted the issue-20444 branch January 6, 2015 15:20
dead10ck added a commit to dead10ck/inotify-rs that referenced this pull request Jan 9, 2015
The `c_str` module has been redesigned:

rust-lang/rust#20507

This change fixes the code to align with the redesign.

Fixes hannobraun#9
zmbush added a commit to zmbush/git2-rs that referenced this pull request Jan 10, 2015
Mostly fixing issues related to rust-lang/rust#20507

The examples still aren't compiling, but I figured this might be a good starting
point for finishing the upgrade.
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.

Tracking issue for stabilize c_str, c_vec (RFC 494)
5 participants