about summary refs log tree commit diff
path: root/src/libstd/sys_common
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_common
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_common')
-rw-r--r--src/libstd/sys_common/wtf8.rs19
1 files changed, 19 insertions, 0 deletions
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) }
+    }
 }