diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-03-16 21:56:52 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-16 21:56:52 -0400 |
| commit | bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8 (patch) | |
| tree | ba624a9e66a33fec8bcc8bc93734d03acbdd6688 /src/libstd/ffi | |
| parent | 6adbbfc6ba8786ea91e1051ea14d64a91839f5b5 (diff) | |
| parent | 0aeb9c12979e6da753701a798d04105b6b1a8c28 (diff) | |
| download | rust-bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8.tar.gz rust-bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8.zip | |
Merge branch 'master' into frewsxcv-osstr
Diffstat (limited to 'src/libstd/ffi')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 33 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 34 |
2 files changed, 53 insertions, 14 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index bfb0aa6e1a1..2d14bb66bf4 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -325,7 +325,7 @@ impl CString { } /// Converts this `CString` into a boxed `CStr`. - #[unstable(feature = "into_boxed_c_str", issue = "0")] + #[unstable(feature = "into_boxed_c_str", issue = "40380")] pub fn into_boxed_c_str(self) -> Box<CStr> { unsafe { mem::transmute(self.into_inner()) } } @@ -415,6 +415,20 @@ impl<'a> From<&'a CStr> for Box<CStr> { } } +#[stable(feature = "c_string_from_box", since = "1.17.0")] +impl From<Box<CStr>> for CString { + fn from(s: Box<CStr>) -> CString { + s.into_c_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into<Box<CStr>> for CString { + fn into(self) -> Box<CStr> { + self.into_boxed_c_str() + } +} + #[stable(feature = "default_box_extra", since = "1.17.0")] impl Default for Box<CStr> { fn default() -> Box<CStr> { @@ -728,6 +742,12 @@ impl CStr { pub fn to_string_lossy(&self) -> Cow<str> { String::from_utf8_lossy(self.to_bytes()) } + + /// Converts a `Box<CStr>` into a `CString` without copying or allocating. + #[unstable(feature = "into_boxed_c_str", issue = "40380")] + pub fn into_c_string(self: Box<CStr>) -> CString { + unsafe { mem::transmute(self) } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -922,12 +942,11 @@ mod tests { fn into_boxed() { let orig: &[u8] = b"Hello, world!\0"; let cstr = CStr::from_bytes_with_nul(orig).unwrap(); - let cstring = cstr.to_owned(); - let box1: Box<CStr> = Box::from(cstr); - let box2 = cstring.into_boxed_c_str(); - assert_eq!(cstr, &*box1); - assert_eq!(box1, box2); - assert_eq!(&*box2, cstr); + let boxed: Box<CStr> = Box::from(cstr); + let cstring = cstr.to_owned().into_boxed_c_str().into_c_string(); + assert_eq!(cstr, &*boxed); + assert_eq!(&*boxed, &*cstring); + assert_eq!(&*cstring, cstr); } #[test] diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index b0f79f9a395..bf3f41b13c1 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -260,7 +260,7 @@ impl OsString { /// /// let b: Box<OsStr> = s.into_boxed_os_str(); /// ``` - #[unstable(feature = "into_boxed_os_str", issue = "0")] + #[unstable(feature = "into_boxed_os_str", issue = "40380")] pub fn into_boxed_os_str(self) -> Box<OsStr> { unsafe { mem::transmute(self.inner.into_box()) } } @@ -506,6 +506,13 @@ impl OsStr { self.inner.inner.len() } + /// Converts a `Box<OsStr>` into an `OsString` without copying or allocating. + #[unstable(feature = "into_boxed_os_str", issue = "40380")] + pub fn into_os_string(self: Box<OsStr>) -> OsString { + let inner: Box<Slice> = unsafe { mem::transmute(self) }; + OsString { inner: Buf::from_box(inner) } + } + /// Gets the underlying byte representation. /// /// Note: it is *crucial* that this API is private, to avoid @@ -522,6 +529,20 @@ impl<'a> From<&'a OsStr> for Box<OsStr> { } } +#[stable(feature = "os_string_from_box", since = "1.17.0")] +impl<'a> From<Box<OsStr>> for OsString { + fn from(boxed: Box<OsStr>) -> OsString { + boxed.into_os_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into<Box<OsStr>> for OsString { + fn into(self) -> Box<OsStr> { + self.into_boxed_os_str() + } +} + #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box<OsStr> { fn default() -> Box<OsStr> { @@ -830,12 +851,11 @@ mod tests { fn into_boxed() { let orig = "Hello, world!"; let os_str = OsStr::new(orig); - let os_string = os_str.to_owned(); - let box1: Box<OsStr> = Box::from(os_str); - let box2 = os_string.into_boxed_os_str(); - assert_eq!(os_str, &*box1); - assert_eq!(box1, box2); - assert_eq!(&*box2, os_str); + let boxed: Box<OsStr> = Box::from(os_str); + let os_string = os_str.to_owned().into_boxed_os_str().into_os_string(); + assert_eq!(os_str, &*boxed); + assert_eq!(&*boxed, &*os_string); + assert_eq!(&*os_string, os_str); } #[test] |
