Skip to content

Conversions between CStr, OsStr, Path and boxes #39594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 41 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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() }
}
}

Expand Down Expand Up @@ -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]);
}
}
38 changes: 38 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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());
}
}
40 changes: 40 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions src/libstd/sys/redox/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) }
}
}
16 changes: 16 additions & 0 deletions src/libstd/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) }
}
}
14 changes: 14 additions & 0 deletions src/libstd/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()) }
}
}
19 changes: 19 additions & 0 deletions src/libstd/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) }
}
}


Expand Down