From 963843b1b346278fcf6f7f065cabdaaae775a0a1 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Tue, 31 Jan 2017 22:46:16 -0500 Subject: [PATCH] Conversions between CStr/OsStr/Path and boxes. --- src/liballoc/boxed.rs | 8 ++++++ src/libstd/ffi/c_str.rs | 42 +++++++++++++++++++++++++++++++- src/libstd/ffi/os_str.rs | 38 +++++++++++++++++++++++++++++ src/libstd/path.rs | 40 ++++++++++++++++++++++++++++++ src/libstd/sys/redox/os_str.rs | 16 ++++++++++++ src/libstd/sys/unix/os_str.rs | 16 ++++++++++++ src/libstd/sys/windows/os_str.rs | 14 +++++++++++ src/libstd/sys_common/wtf8.rs | 19 +++++++++++++++ 8 files changed, 192 insertions(+), 1 deletion(-) 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 Default for Box<[T]> { } } +#[stable(feature = "default_box_extra", since = "1.17.0")] +impl Default for Box { + fn default() -> Box { + let default: Box<[u8]> = Default::default(); + unsafe { mem::transmute(default) } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Clone for Box { /// 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 { + 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 for CString { fn borrow(&self) -> &CStr { self } } +#[stable(feature = "box_from_c_str", since = "1.17.0")] +impl<'a> From<&'a CStr> for Box { + fn from(s: &'a CStr) -> Box { + 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 { + fn default() -> Box { + 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 = 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 = >::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 { + 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 { + fn from(s: &'a OsStr) -> Box { + unsafe { mem::transmute(s.inner.into_box()) } + } +} + +#[stable(feature = "box_default_extra", since = "1.17.0")] +impl Default for Box { + fn default() -> Box { + 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 = 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 = >::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 { + 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 { + fn from(path: &'a Path) -> Box { + let boxed: Box = path.inner.into(); + unsafe { mem::transmute(boxed) } + } +} + +#[stable(feature = "box_default_extra", since = "1.17.0")] +impl Default for Box { + fn default() -> Box { + let boxed: Box = 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 = 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 = >::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 { + 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 { + let boxed: Box<[u8]> = self.inner.into(); + unsafe { mem::transmute(boxed) } + } + + pub fn empty_box() -> Box { + 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 { + 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 { + let boxed: Box<[u8]> = self.inner.into(); + unsafe { mem::transmute(boxed) } + } + + pub fn empty_box() -> Box { + 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 { + 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 { + unsafe { mem::transmute(self.inner.into_box()) } + } + + pub fn empty_box() -> Box { + 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 { + 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 { + let boxed: Box<[u8]> = self.bytes.into(); + unsafe { mem::transmute(boxed) } + } + + /// Creates a boxed, empty `Wtf8`. + pub fn empty_box() -> Box { + let boxed: Box<[u8]> = Default::default(); + unsafe { mem::transmute(boxed) } + } }