1
Fork 0

std: Implement CString-related RFCs

This commit is an implementation of [RFC 592][r592] and [RFC 840][r840]. These
two RFCs tweak the behavior of `CString` and add a new `CStr` unsized slice type
to the module.

[r592]: https://github.com/rust-lang/rfcs/blob/master/text/0592-c-str-deref.md
[r840]: https://github.com/rust-lang/rfcs/blob/master/text/0840-no-panic-in-c-string.md

The new `CStr` type is only constructable via two methods:

1. By `deref`'ing from a `CString`
2. Unsafely via `CStr::from_ptr`

The purpose of `CStr` is to be an unsized type which is a thin pointer to a
`libc::c_char` (currently it is a fat pointer slice due to implementation
limitations). Strings from C can be safely represented with a `CStr` and an
appropriate lifetime as well. Consumers of `&CString` should now consume `&CStr`
instead to allow producers to pass in C-originating strings instead of just
Rust-allocated strings.

A new constructor was added to `CString`, `new`, which takes `T: IntoBytes`
instead of separate `from_slice` and `from_vec` methods (both have been
deprecated in favor of `new`). The `new` method returns a `Result` instead of
panicking.  The error variant contains the relevant information about where the
error happened and bytes (if present). Conversions are provided to the
`io::Error` and `old_io::IoError` types via the `FromError` trait which
translate to `InvalidInput`.

This is a breaking change due to the modification of existing `#[unstable]` APIs
and new deprecation, and more detailed information can be found in the two RFCs.
Notable breakage includes:

* All construction of `CString` now needs to use `new` and handle the outgoing
  `Result`.
* Usage of `CString` as a byte slice now explicitly needs a `.as_bytes()` call.
* The `as_slice*` methods have been removed in favor of just having the
  `as_bytes*` methods.

Closes #22469
Closes #22470
[breaking-change]
This commit is contained in:
Alex Crichton 2015-02-17 22:47:40 -08:00
parent dfc5c0f1e8
commit 1860ee521a
40 changed files with 555 additions and 290 deletions

View file

@ -33,7 +33,7 @@
use prelude::v1::*;
use ffi::{CString, OsStr, OsString};
use ffi::{CString, NulError, OsStr, OsString};
use fs::{self, Permissions, OpenOptions};
use net;
use mem;
@ -155,7 +155,7 @@ pub trait OsStrExt {
fn as_bytes(&self) -> &[u8];
/// Convert the `OsStr` slice into a `CString`.
fn to_cstring(&self) -> CString;
fn to_cstring(&self) -> Result<CString, NulError>;
}
impl OsStrExt for OsStr {
@ -166,8 +166,8 @@ impl OsStrExt for OsStr {
&self.as_inner().inner
}
fn to_cstring(&self) -> CString {
CString::from_slice(self.as_bytes())
fn to_cstring(&self) -> Result<CString, NulError> {
CString::new(self.as_bytes())
}
}
@ -249,5 +249,7 @@ impl ExitStatusExt for process::ExitStatus {
/// Includes all extension traits, and some important type definitions.
pub mod prelude {
#[doc(no_inline)]
pub use super::{Fd, AsRawFd, OsStrExt, OsStringExt, PermissionsExt, CommandExt, ExitStatusExt};
pub use super::{Fd, AsRawFd, OsStrExt, OsStringExt, PermissionsExt};
#[doc(no_inline)]
pub use super::{CommandExt, ExitStatusExt};
}