about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-02-15 10:22:34 +0000
committerbors <bors@rust-lang.org>2017-02-15 10:22:34 +0000
commite0044bd3896456afb346d06e91a97ac515930ccf (patch)
tree98fda7ab7d4516750b9bb9c1219f3a93f11ae841
parentea8c62919e5f0c7e511717f672406536ef94cab1 (diff)
parent963843b1b346278fcf6f7f065cabdaaae775a0a1 (diff)
downloadrust-e0044bd3896456afb346d06e91a97ac515930ccf.tar.gz
rust-e0044bd3896456afb346d06e91a97ac515930ccf.zip
Auto merge of #39594 - clarcharr:cstr_box, r=aturon
Conversions between CStr, OsStr, Path and boxes

This closes a bit of the inconsistencies between `CStr`, `OsStr`, `Path`, and `str`, allowing people to create boxed versions of DSTs other than `str` and `[T]`.

Full list of additions:
* `Default` for `Box<str>`, `Box<CStr>`, `Box<OsStr>`, and `Box<Path>` (note: `Default` for `PathBuf` is already implemented)
* `CString::into_boxed_c_str` (feature gated)
* `OsString::into_boxed_os_str` (feature gated)
* `Path::into_boxed_path` (feature gated)
* `From<&CStr> for Box<CStr>`
* `From<&OsStr> for Box<OsStr>`
* `From<&Path> for Box<Path>`

This also includes adding the internal methods:
* `sys::*::os_str::Buf::into_box`
* `sys::*::os_str::Slice::{into_box, empty_box}`
* `sys_common::wtf8::Wtf8Buf::into_box`
* `sys_common::wtf8::Wtf8::{into_box, empty_box}`
-rw-r--r--src/liballoc/boxed.rs8
-rw-r--r--src/libstd/ffi/c_str.rs42
-rw-r--r--src/libstd/ffi/os_str.rs38
-rw-r--r--src/libstd/path.rs40
-rw-r--r--src/libstd/sys/redox/os_str.rs16
-rw-r--r--src/libstd/sys/unix/os_str.rs16
-rw-r--r--src/libstd/sys/windows/os_str.rs14
-rw-r--r--src/libstd/sys_common/wtf8.rs19
8 files changed, 192 insertions, 1 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index ac9439974a4..d0fce706128 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -315,6 +315,14 @@ impl<T> Default for Box<[T]> {
     }
 }
 
+#[stable(feature = "default_box_extra", since = "1.17.0")]
+impl Default for Box<str> {
+    fn default() -> Box<str> {
+        let default: Box<[u8]> = Default::default();
+        unsafe { mem::transmute(default) }
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: Clone> Clone for Box<T> {
     /// Returns a new box with a `clone()` of this box's contents.
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index d1b8fcd7440..dc3855367ae 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -303,6 +303,12 @@ impl CString {
         &self.inner
     }
 
+    /// Converts this `CString` into a boxed `CStr`.
+    #[unstable(feature = "into_boxed_c_str", issue = "0")]
+    pub fn into_boxed_c_str(self) -> Box<CStr> {
+        unsafe { mem::transmute(self.into_inner()) }
+    }
+
     // Bypass "move out of struct which implements `Drop` trait" restriction.
     fn into_inner(self) -> Box<[u8]> {
         unsafe {
@@ -380,6 +386,22 @@ impl Borrow<CStr> for CString {
     fn borrow(&self) -> &CStr { self }
 }
 
+#[stable(feature = "box_from_c_str", since = "1.17.0")]
+impl<'a> From<&'a CStr> for Box<CStr> {
+    fn from(s: &'a CStr) -> Box<CStr> {
+        let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
+        unsafe { mem::transmute(boxed) }
+    }
+}
+
+#[stable(feature = "default_box_extra", since = "1.17.0")]
+impl Default for Box<CStr> {
+    fn default() -> Box<CStr> {
+        let boxed: Box<[u8]> = Box::from([0]);
+        unsafe { mem::transmute(boxed) }
+    }
+}
+
 impl NulError {
     /// Returns the position of the nul byte in the slice that was provided to
     /// `CString::new`.
@@ -686,7 +708,7 @@ impl ToOwned for CStr {
     type Owned = CString;
 
     fn to_owned(&self) -> CString {
-        CString { inner: self.to_bytes_with_nul().to_vec().into_boxed_slice() }
+        CString { inner: self.to_bytes_with_nul().into() }
     }
 }
 
@@ -847,4 +869,22 @@ mod tests {
         let cstr = CStr::from_bytes_with_nul(data);
         assert!(cstr.is_err());
     }
+
+    #[test]
+    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);
+    }
+
+    #[test]
+    fn boxed_default() {
+        let boxed = <Box<CStr>>::default();
+        assert_eq!(boxed.to_bytes_with_nul(), &[0]);
+    }
 }
diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs
index 273b717f467..7b8bf42e0a7 100644
--- a/src/libstd/ffi/os_str.rs
+++ b/src/libstd/ffi/os_str.rs
@@ -204,6 +204,12 @@ impl OsString {
     pub fn reserve_exact(&mut self, additional: usize) {
         self.inner.reserve_exact(additional)
     }
+
+    /// Converts this `OsString` into a boxed `OsStr`.
+    #[unstable(feature = "into_boxed_os_str", issue = "0")]
+    pub fn into_boxed_os_str(self) -> Box<OsStr> {
+        unsafe { mem::transmute(self.inner.into_box()) }
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -445,6 +451,20 @@ impl OsStr {
     }
 }
 
+#[stable(feature = "box_from_os_str", since = "1.17.0")]
+impl<'a> From<&'a OsStr> for Box<OsStr> {
+    fn from(s: &'a OsStr) -> Box<OsStr> {
+        unsafe { mem::transmute(s.inner.into_box()) }
+    }
+}
+
+#[stable(feature = "box_default_extra", since = "1.17.0")]
+impl Default for Box<OsStr> {
+    fn default() -> Box<OsStr> {
+        unsafe { mem::transmute(Slice::empty_box()) }
+    }
+}
+
 #[stable(feature = "osstring_default", since = "1.9.0")]
 impl<'a> Default for &'a OsStr {
     /// Creates an empty `OsStr`.
@@ -741,4 +761,22 @@ mod tests {
         let os_str: &OsStr = Default::default();
         assert_eq!("", os_str);
     }
+
+    #[test]
+    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);
+    }
+
+    #[test]
+    fn boxed_default() {
+        let boxed = <Box<OsStr>>::default();
+        assert!(boxed.is_empty());
+    }
 }
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 07b43cd89ac..245a6d945b5 100644
--- a/src/libstd/path.rs
+++ b/src/libstd/path.rs
@@ -1194,6 +1194,28 @@ impl PathBuf {
     pub fn into_os_string(self) -> OsString {
         self.inner
     }
+
+    /// Converts this `PathBuf` into a boxed `Path`.
+    #[unstable(feature = "into_boxed_path", issue = "0")]
+    pub fn into_boxed_path(self) -> Box<Path> {
+        unsafe { mem::transmute(self.inner.into_boxed_os_str()) }
+    }
+}
+
+#[stable(feature = "box_from_path", since = "1.17.0")]
+impl<'a> From<&'a Path> for Box<Path> {
+    fn from(path: &'a Path) -> Box<Path> {
+        let boxed: Box<OsStr> = path.inner.into();
+        unsafe { mem::transmute(boxed) }
+    }
+}
+
+#[stable(feature = "box_default_extra", since = "1.17.0")]
+impl Default for Box<Path> {
+    fn default() -> Box<Path> {
+        let boxed: Box<OsStr> = Default::default();
+        unsafe { mem::transmute(boxed) }
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -3676,4 +3698,22 @@ mod tests {
         let actual = format!("{:?}", iter);
         assert_eq!(expected, actual);
     }
+
+    #[test]
+    fn into_boxed() {
+        let orig: &str = "some/sort/of/path";
+        let path = Path::new(orig);
+        let path_buf = path.to_owned();
+        let box1: Box<Path> = Box::from(path);
+        let box2 = path_buf.into_boxed_path();
+        assert_eq!(path, &*box1);
+        assert_eq!(box1, box2);
+        assert_eq!(&*box2, path);
+    }
+
+    #[test]
+    fn boxed_default() {
+        let boxed = <Box<Path>>::default();
+        assert!(boxed.as_os_str().is_empty());
+    }
 }
diff --git a/src/libstd/sys/redox/os_str.rs b/src/libstd/sys/redox/os_str.rs
index 8922bf04f56..0f967863899 100644
--- a/src/libstd/sys/redox/os_str.rs
+++ b/src/libstd/sys/redox/os_str.rs
@@ -94,6 +94,11 @@ impl Buf {
     pub fn push_slice(&mut self, s: &Slice) {
         self.inner.extend_from_slice(&s.inner)
     }
+
+    #[inline]
+    pub fn into_box(self) -> Box<Slice> {
+        unsafe { mem::transmute(self.inner.into_boxed_slice()) }
+    }
 }
 
 impl Slice {
@@ -116,4 +121,15 @@ impl Slice {
     pub fn to_owned(&self) -> Buf {
         Buf { inner: self.inner.to_vec() }
     }
+
+    #[inline]
+    pub fn into_box(&self) -> Box<Slice> {
+        let boxed: Box<[u8]> = self.inner.into();
+        unsafe { mem::transmute(boxed) }
+    }
+
+    pub fn empty_box() -> Box<Slice> {
+        let boxed: Box<[u8]> = Default::default();
+        unsafe { mem::transmute(boxed) }
+    }
 }
diff --git a/src/libstd/sys/unix/os_str.rs b/src/libstd/sys/unix/os_str.rs
index 5a733c0cb87..938bcfc6d16 100644
--- a/src/libstd/sys/unix/os_str.rs
+++ b/src/libstd/sys/unix/os_str.rs
@@ -94,6 +94,11 @@ impl Buf {
     pub fn push_slice(&mut self, s: &Slice) {
         self.inner.extend_from_slice(&s.inner)
     }
+
+    #[inline]
+    pub fn into_box(self) -> Box<Slice> {
+        unsafe { mem::transmute(self.inner.into_boxed_slice()) }
+    }
 }
 
 impl Slice {
@@ -116,4 +121,15 @@ impl Slice {
     pub fn to_owned(&self) -> Buf {
         Buf { inner: self.inner.to_vec() }
     }
+
+    #[inline]
+    pub fn into_box(&self) -> Box<Slice> {
+        let boxed: Box<[u8]> = self.inner.into();
+        unsafe { mem::transmute(boxed) }
+    }
+
+    pub fn empty_box() -> Box<Slice> {
+        let boxed: Box<[u8]> = Default::default();
+        unsafe { mem::transmute(boxed) }
+    }
 }
diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs
index a065c7a7fd0..04e45dcf549 100644
--- a/src/libstd/sys/windows/os_str.rs
+++ b/src/libstd/sys/windows/os_str.rs
@@ -88,6 +88,11 @@ impl Buf {
     pub fn reserve_exact(&mut self, additional: usize) {
         self.inner.reserve_exact(additional)
     }
+
+    #[inline]
+    pub fn into_box(self) -> Box<Slice> {
+        unsafe { mem::transmute(self.inner.into_box()) }
+    }
 }
 
 impl Slice {
@@ -108,4 +113,13 @@ impl Slice {
         buf.push_wtf8(&self.inner);
         Buf { inner: buf }
     }
+
+    #[inline]
+    pub fn into_box(&self) -> Box<Slice> {
+        unsafe { mem::transmute(self.inner.into_box()) }
+    }
+
+    pub fn empty_box() -> Box<Slice> {
+        unsafe { mem::transmute(Wtf8::empty_box()) }
+    }
 }
diff --git a/src/libstd/sys_common/wtf8.rs b/src/libstd/sys_common/wtf8.rs
index 0a94ff1e958..1d61181a4ee 100644
--- a/src/libstd/sys_common/wtf8.rs
+++ b/src/libstd/sys_common/wtf8.rs
@@ -340,6 +340,12 @@ impl Wtf8Buf {
             }
         }
     }
+
+    /// Converts this `Wtf8Buf` into a boxed `Wtf8`.
+    #[inline]
+    pub fn into_box(self) -> Box<Wtf8> {
+        unsafe { mem::transmute(self.bytes.into_boxed_slice()) }
+    }
 }
 
 /// Create a new WTF-8 string from an iterator of code points.
@@ -583,6 +589,19 @@ impl Wtf8 {
             _ => None
         }
     }
+
+    /// Boxes this `Wtf8`.
+    #[inline]
+    pub fn into_box(&self) -> Box<Wtf8> {
+        let boxed: Box<[u8]> = self.bytes.into();
+        unsafe { mem::transmute(boxed) }
+    }
+
+    /// Creates a boxed, empty `Wtf8`.
+    pub fn empty_box() -> Box<Wtf8> {
+        let boxed: Box<[u8]> = Default::default();
+        unsafe { mem::transmute(boxed) }
+    }
 }