-
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
std: Redesign c_str and c_vec #20507
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
r? @aturon |
I should also point out that the entire |
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()"] |
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 think we should offer this convenience in ffi
, something like ffi::c_str_to_string
.
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'm somewhat worried about a large array of functions popping up in "view this C string as a rust type":
c_str_to_bytes
x 2 for_with_nul
c_str_to_bytes_with_max_size
(e.g. CString should have a "within max length" constructor #20475) x 2 for_with_nul
c_str_to_str
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.
Looks great overall! I think |
5bd16eb
to
5deef52
Compare
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
5deef52
to
ec7a50d
Compare
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
The `c_str` module has been redesigned: rust-lang/rust#20507 This change fixes the code to align with the redesign. Fixes hannobraun#9
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.
This commit is an implementation of RFC 494 which removes the entire
std::c_vec
module and redesigns thestd::c_str
module asstd::ffi
.The interface of the new
CString
is outlined in the linked RFC, the primarychanges being:
ToCStr
trait is gone, meaning thewith_c_str
andto_c_str
methodsare now gone. These two methods are replaced with a
CString::from_slice
method.
CString
type is now just a wrapper aroundVec<u8>
with a staticguarantee that there is a trailing nul byte with no internal nul bytes. This
means that
CString
now implementsDeref<Target = [c_char]>
, which is whereit gains most of its methods from. A few helper methods are added to acquire a
slice of
u8
instead ofc_char
, as well as including a slice with thetrailing nul byte if necessary.
CString
values is now done via two functions insideof
std::ffi
, calledc_str_to_bytes
andc_str_to_bytes_with_nul
. Thesefunctions are now the one method used to convert a
*const c_char
to a Rustslice of
u8
.Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:
[breaking-change]
Closes #20444