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}`
This commit is contained in:
bors 2017-02-15 10:22:34 +00:00
commit e0044bd389
8 changed files with 192 additions and 1 deletions

View File

@ -315,6 +315,14 @@ fn default() -> 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.

View File

@ -303,6 +303,12 @@ pub fn as_bytes_with_nul(&self) -> &[u8] {
&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 @@ fn from_bytes_with_nul_interior() {
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]);
}
}

View File

@ -204,6 +204,12 @@ pub fn reserve(&mut self, additional: usize) {
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 @@ fn bytes(&self) -> &[u8] {
}
}
#[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 @@ fn test_os_str_default() {
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());
}
}

View File

@ -1194,6 +1194,28 @@ fn _set_extension(&mut self, extension: &OsStr) -> bool {
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 @@ fn test_iter_debug() {
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());
}
}

View File

@ -94,6 +94,11 @@ pub fn into_string(self) -> Result<String, 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 @@ pub fn to_string_lossy(&self) -> Cow<str> {
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) }
}
}

View File

@ -94,6 +94,11 @@ pub fn into_string(self) -> Result<String, 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 @@ pub fn to_string_lossy(&self) -> Cow<str> {
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) }
}
}

View File

@ -88,6 +88,11 @@ pub fn reserve(&mut self, additional: usize) {
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 @@ pub fn to_owned(&self) -> Buf {
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()) }
}
}

View File

@ -340,6 +340,12 @@ pub fn into_string_lossy(mut self) -> String {
}
}
}
/// 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 @@ fn initial_trail_surrogate(&self) -> Option<u16> {
_ => 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) }
}
}