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. |
src/libcollections/string.rs
Outdated
There was a problem hiding this comment.
I think we should offer this convenience in ffi, something like ffi::c_str_to_string.
There was a problem hiding this comment.
I'm somewhat worried about a large array of functions popping up in "view this C string as a rust type":
c_str_to_bytesx 2 for_with_nulc_str_to_bytes_with_max_size(e.g. CString should have a "within max length" constructor #20475) x 2 for_with_nulc_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
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_vecmodule and redesigns thestd::c_strmodule asstd::ffi.The interface of the new
CStringis outlined in the linked RFC, the primarychanges being:
ToCStrtrait is gone, meaning thewith_c_strandto_c_strmethodsare now gone. These two methods are replaced with a
CString::from_slicemethod.
CStringtype 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
CStringnow implementsDeref<Target = [c_char]>, which is whereit gains most of its methods from. A few helper methods are added to acquire a
slice of
u8instead ofc_char, as well as including a slice with thetrailing nul byte if necessary.
CStringvalues is now done via two functions insideof
std::ffi, calledc_str_to_bytesandc_str_to_bytes_with_nul. Thesefunctions are now the one method used to convert a
*const c_charto 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