about summary refs log tree commit diff
path: root/src/libstd/sys
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 /src/libstd/sys
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}`
Diffstat (limited to 'src/libstd/sys')
-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
3 files changed, 46 insertions, 0 deletions
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()) }
+    }
 }