diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-03-16 21:56:52 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-16 21:56:52 -0400 |
| commit | bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8 (patch) | |
| tree | ba624a9e66a33fec8bcc8bc93734d03acbdd6688 /src | |
| parent | 6adbbfc6ba8786ea91e1051ea14d64a91839f5b5 (diff) | |
| parent | 0aeb9c12979e6da753701a798d04105b6b1a8c28 (diff) | |
| download | rust-bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8.tar.gz rust-bc6eecd0c2ed7854d52fc823be0b093f3bc76ba8.zip | |
Merge branch 'master' into frewsxcv-osstr
Diffstat (limited to 'src')
| -rw-r--r-- | src/bootstrap/config.rs | 4 | ||||
| -rw-r--r-- | src/bootstrap/config.toml.example | 3 | ||||
| -rw-r--r-- | src/bootstrap/dist.rs | 14 | ||||
| -rw-r--r-- | src/bootstrap/install.rs | 4 | ||||
| -rw-r--r-- | src/bootstrap/lib.rs | 2 | ||||
| -rwxr-xr-x | src/ci/run.sh | 1 | ||||
| -rw-r--r-- | src/libcollections/string.rs | 16 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 16 | ||||
| -rw-r--r-- | src/libcollectionstest/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcollectionstest/str.rs | 30 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 80 | ||||
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 33 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 34 | ||||
| -rw-r--r-- | src/libstd/path.rs | 34 | ||||
| -rw-r--r-- | src/libstd/sys/redox/os_str.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/unix/os_str.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/windows/os_str.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys_common/wtf8.rs | 6 |
18 files changed, 243 insertions, 53 deletions
diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index dcd49c51e3a..b1d1d79b9ea 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -74,6 +74,7 @@ pub struct Config { pub rustc_default_ar: Option<String>, pub rust_optimize_tests: bool, pub rust_debuginfo_tests: bool, + pub rust_save_analysis: bool, pub rust_dist_src: bool, pub build: String, @@ -225,6 +226,7 @@ struct Rust { optimize_tests: Option<bool>, debuginfo_tests: Option<bool>, codegen_tests: Option<bool>, + save_analysis: Option<bool>, } /// TOML representation of how each build target is configured. @@ -350,6 +352,7 @@ impl Config { set(&mut config.rust_optimize_tests, rust.optimize_tests); set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests); set(&mut config.codegen_tests, rust.codegen_tests); + set(&mut config.rust_save_analysis, rust.save_analysis); set(&mut config.rust_rpath, rust.rpath); set(&mut config.debug_jemalloc, rust.debug_jemalloc); set(&mut config.use_jemalloc, rust.use_jemalloc); @@ -457,6 +460,7 @@ impl Config { ("LOCAL_REBUILD", self.local_rebuild), ("NINJA", self.ninja), ("CODEGEN_TESTS", self.codegen_tests), + ("SAVE_ANALYSIS", self.rust_save_analysis), ("LOCKED_DEPS", self.locked_deps), ("VENDOR", self.vendor), ("FULL_BOOTSTRAP", self.full_bootstrap), diff --git a/src/bootstrap/config.toml.example b/src/bootstrap/config.toml.example index 5a00e90f370..76fedae0f03 100644 --- a/src/bootstrap/config.toml.example +++ b/src/bootstrap/config.toml.example @@ -234,6 +234,9 @@ # saying that the FileCheck executable is missing, you may want to disable this. #codegen-tests = true +# Flag indicating whether the API analysis data should be saved. +#save-analysis = false + # ============================================================================= # Options for specific targets # diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 7c463bec5ff..289c1e1c3a9 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -311,18 +311,14 @@ pub fn rust_src_location(build: &Build) -> PathBuf { /// Creates a tarball of save-analysis metadata, if available. pub fn analysis(build: &Build, compiler: &Compiler, target: &str) { + if !build.config.rust_save_analysis { + return + } + println!("Dist analysis"); - if build.config.channel != "nightly" { - println!("\tskipping - not on nightly channel"); - return; - } if compiler.host != build.config.build { - println!("\tskipping - not a build host"); - return - } - if compiler.stage != 2 { - println!("\tskipping - not stage2"); + println!("\tskipping, not a build host"); return } diff --git a/src/bootstrap/install.rs b/src/bootstrap/install.rs index ba8442ebd8c..249f241a151 100644 --- a/src/bootstrap/install.rs +++ b/src/bootstrap/install.rs @@ -49,6 +49,10 @@ pub fn install(build: &Build, stage: u32, host: &str) { install_sh(&build, "docs", "rust-docs", stage, host, &prefix, &docdir, &libdir, &mandir, &empty_dir); } + if build.config.rust_save_analysis { + install_sh(&build, "analysis", "rust-analysis", stage, host, &prefix, + &docdir, &libdir, &mandir, &empty_dir); + } install_sh(&build, "std", "rust-std", stage, host, &prefix, &docdir, &libdir, &mandir, &empty_dir); install_sh(&build, "rustc", "rustc", stage, host, &prefix, diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 72aec15e532..2d2be531e62 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -524,7 +524,7 @@ impl Build { .env(format!("CFLAGS_{}", target), self.cflags(target).join(" ")); } - if self.config.channel == "nightly" && compiler.is_final_stage(self) { + if self.config.rust_save_analysis && compiler.is_final_stage(self) { cargo.env("RUSTC_SAVE_ANALYSIS", "api".to_string()); } diff --git a/src/ci/run.sh b/src/ci/run.sh index 19bea9ced06..d8b317a46c3 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -43,6 +43,7 @@ fi if [ "$DEPLOY$DEPLOY_ALT" != "" ]; then RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --release-channel=nightly" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-llvm-static-stdcpp" + RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-save-analysis" if [ "$NO_LLVM_ASSERTIONS" = "1" ]; then RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-llvm-assertions" diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 43323676ab4..13c99a2d59b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1974,6 +1974,22 @@ impl<'a> From<&'a str> for String { } } +// note: test pulls in libstd, which causes errors here +#[cfg(not(test))] +#[stable(feature = "string_from_box", since = "1.17.0")] +impl From<Box<str>> for String { + fn from(s: Box<str>) -> String { + s.into_string() + } +} + +#[stable(feature = "box_from_str", since = "1.17.0")] +impl Into<Box<str>> for String { + fn into(self) -> Box<str> { + self.into_boxed_str() + } +} + #[stable(feature = "string_from_cow_str", since = "1.14.0")] impl<'a> From<Cow<'a, str>> for String { fn from(s: Cow<'a, str>) -> String { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index d38c9f6e1cf..e4a6af33409 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1897,6 +1897,22 @@ impl<'a, T> From<Cow<'a, [T]>> for Vec<T> where [T]: ToOwned<Owned=Vec<T>> { } } +// note: test pulls in libstd, which causes errors here +#[cfg(not(test))] +#[stable(feature = "vec_from_box", since = "1.17.0")] +impl<T> From<Box<[T]>> for Vec<T> { + fn from(s: Box<[T]>) -> Vec<T> { + s.into_vec() + } +} + +#[stable(feature = "box_from_vec", since = "1.17.0")] +impl<T> Into<Box<[T]>> for Vec<T> { + fn into(self) -> Box<[T]> { + self.into_boxed_slice() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a> From<&'a str> for Vec<u8> { fn from(s: &'a str) -> Vec<u8> { diff --git a/src/libcollectionstest/lib.rs b/src/libcollectionstest/lib.rs index d97d9b8ab83..98d0b1c8e15 100644 --- a/src/libcollectionstest/lib.rs +++ b/src/libcollectionstest/lib.rs @@ -28,6 +28,7 @@ #![feature(test)] #![feature(unboxed_closures)] #![feature(unicode)] +#![feature(utf8_error_error_len)] extern crate collections; extern crate test; diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index 8071c7e8c20..c9b7104fec4 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -541,6 +541,36 @@ fn from_utf8_mostly_ascii() { } #[test] +fn from_utf8_error() { + macro_rules! test { + ($input: expr, $expected_valid_up_to: expr, $expected_error_len: expr) => { + let error = from_utf8($input).unwrap_err(); + assert_eq!(error.valid_up_to(), $expected_valid_up_to); + assert_eq!(error.error_len(), $expected_error_len); + } + } + test!(b"A\xC3\xA9 \xFF ", 4, Some(1)); + test!(b"A\xC3\xA9 \x80 ", 4, Some(1)); + test!(b"A\xC3\xA9 \xC1 ", 4, Some(1)); + test!(b"A\xC3\xA9 \xC1", 4, Some(1)); + test!(b"A\xC3\xA9 \xC2", 4, None); + test!(b"A\xC3\xA9 \xC2 ", 4, Some(1)); + test!(b"A\xC3\xA9 \xC2\xC0", 4, Some(1)); + test!(b"A\xC3\xA9 \xE0", 4, None); + test!(b"A\xC3\xA9 \xE0\x9F", 4, Some(1)); + test!(b"A\xC3\xA9 \xE0\xA0", 4, None); + test!(b"A\xC3\xA9 \xE0\xA0\xC0", 4, Some(2)); + test!(b"A\xC3\xA9 \xE0\xA0 ", 4, Some(2)); + test!(b"A\xC3\xA9 \xED\xA0\x80 ", 4, Some(1)); + test!(b"A\xC3\xA9 \xF1", 4, None); + test!(b"A\xC3\xA9 \xF1\x80", 4, None); + test!(b"A\xC3\xA9 \xF1\x80\x80", 4, None); + test!(b"A\xC3\xA9 \xF1 ", 4, Some(1)); + test!(b"A\xC3\xA9 \xF1\x80 ", 4, Some(2)); + test!(b"A\xC3\xA9 \xF1\x80\x80 ", 4, Some(3)); +} + +#[test] fn test_as_bytes() { // no null let v = [ diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 52e33016310..2919adc1cbc 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -125,13 +125,14 @@ Section: Creating a string #[stable(feature = "rust1", since = "1.0.0")] pub struct Utf8Error { valid_up_to: usize, + error_len: Option<u8>, } impl Utf8Error { /// Returns the index in the given string up to which valid UTF-8 was /// verified. /// - /// It is the maximum index such that `from_utf8(input[..index])` + /// It is the maximum index such that `from_utf8(&input[..index])` /// would return `Ok(_)`. /// /// # Examples @@ -152,6 +153,23 @@ impl Utf8Error { /// ``` #[stable(feature = "utf8_error", since = "1.5.0")] pub fn valid_up_to(&self) -> usize { self.valid_up_to } + + /// Provide more information about the failure: + /// + /// * `None`: the end of the input was reached unexpectedly. + /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input. + /// If a byte stream (such as a file or a network socket) is being decoded incrementally, + /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks. + /// + /// * `Some(len)`: an unexpected byte was encountered. + /// The length provided is that of the invalid byte sequence + /// that starts at the index given by `valid_up_to()`. + /// Decoding should resume after that sequence + /// (after inserting a U+FFFD REPLACEMENT CHARACTER) in case of lossy decoding. + #[unstable(feature = "utf8_error_error_len", reason ="new", issue = "40494")] + pub fn error_len(&self) -> Option<usize> { + self.error_len.map(|len| len as usize) + } } /// Converts a slice of bytes to a string slice. @@ -300,7 +318,12 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for Utf8Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "invalid utf-8: invalid byte near index {}", self.valid_up_to) + if let Some(error_len) = self.error_len { + write!(f, "invalid utf-8 sequence of {} bytes from index {}", + error_len, self.valid_up_to) + } else { + write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to) + } } } @@ -1241,17 +1264,20 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { while index < len { let old_offset = index; - macro_rules! err { () => {{ - return Err(Utf8Error { - valid_up_to: old_offset - }) - }}} + macro_rules! err { + ($error_len: expr) => { + return Err(Utf8Error { + valid_up_to: old_offset, + error_len: $error_len, + }) + } + } macro_rules! next { () => {{ index += 1; // we needed data, but there was none: error! if index >= len { - err!() + err!(None) } v[index] }}} @@ -1259,7 +1285,6 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { let first = v[index]; if first >= 128 { let w = UTF8_CHAR_WIDTH[first as usize]; - let second = next!(); // 2-byte encoding is for codepoints \u{0080} to \u{07ff} // first C2 80 last DF BF // 3-byte encoding is for codepoints \u{0800} to \u{ffff} @@ -1279,25 +1304,36 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) / // %xF4 %x80-8F 2( UTF8-tail ) match w { - 2 => if second & !CONT_MASK != TAG_CONT_U8 {err!()}, + 2 => if next!() & !CONT_MASK != TAG_CONT_U8 { + err!(Some(1)) + }, 3 => { - match (first, second, next!() & !CONT_MASK) { - (0xE0 , 0xA0 ... 0xBF, TAG_CONT_U8) | - (0xE1 ... 0xEC, 0x80 ... 0xBF, TAG_CONT_U8) | - (0xED , 0x80 ... 0x9F, TAG_CONT_U8) | - (0xEE ... 0xEF, 0x80 ... 0xBF, TAG_CONT_U8) => {} - _ => err!() + match (first, next!()) { + (0xE0 , 0xA0 ... 0xBF) | + (0xE1 ... 0xEC, 0x80 ... 0xBF) | + (0xED , 0x80 ... 0x9F) | + (0xEE ... 0xEF, 0x80 ... 0xBF) => {} + _ => err!(Some(1)) + } + if next!() & !CONT_MASK != TAG_CONT_U8 { + err!(Some(2)) } } 4 => { - match (first, second, next!() & !CONT_MASK, next!() & !CONT_MASK) { - (0xF0 , 0x90 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) | - (0xF1 ... 0xF3, 0x80 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) | - (0xF4 , 0x80 ... 0x8F, TAG_CONT_U8, TAG_CONT_U8) => {} - _ => err!() + match (first, next!()) { + (0xF0 , 0x90 ... 0xBF) | + (0xF1 ... 0xF3, 0x80 ... 0xBF) | + (0xF4 , 0x80 ... 0x8F) => {} + _ => err!(Some(1)) + } + if next!() & !CONT_MASK != TAG_CONT_U8 { + err!(Some(2)) + } + if next!() & !CONT_MASK != TAG_CONT_U8 { + err!(Some(3)) } } - _ => err!() + _ => err!(Some(1)) } index += 1; } else { diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index bfb0aa6e1a1..2d14bb66bf4 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -325,7 +325,7 @@ impl CString { } /// Converts this `CString` into a boxed `CStr`. - #[unstable(feature = "into_boxed_c_str", issue = "0")] + #[unstable(feature = "into_boxed_c_str", issue = "40380")] pub fn into_boxed_c_str(self) -> Box<CStr> { unsafe { mem::transmute(self.into_inner()) } } @@ -415,6 +415,20 @@ impl<'a> From<&'a CStr> for Box<CStr> { } } +#[stable(feature = "c_string_from_box", since = "1.17.0")] +impl From<Box<CStr>> for CString { + fn from(s: Box<CStr>) -> CString { + s.into_c_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into<Box<CStr>> for CString { + fn into(self) -> Box<CStr> { + self.into_boxed_c_str() + } +} + #[stable(feature = "default_box_extra", since = "1.17.0")] impl Default for Box<CStr> { fn default() -> Box<CStr> { @@ -728,6 +742,12 @@ impl CStr { pub fn to_string_lossy(&self) -> Cow<str> { String::from_utf8_lossy(self.to_bytes()) } + + /// Converts a `Box<CStr>` into a `CString` without copying or allocating. + #[unstable(feature = "into_boxed_c_str", issue = "40380")] + pub fn into_c_string(self: Box<CStr>) -> CString { + unsafe { mem::transmute(self) } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -922,12 +942,11 @@ mod tests { 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); + let boxed: Box<CStr> = Box::from(cstr); + let cstring = cstr.to_owned().into_boxed_c_str().into_c_string(); + assert_eq!(cstr, &*boxed); + assert_eq!(&*boxed, &*cstring); + assert_eq!(&*cstring, cstr); } #[test] diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index b0f79f9a395..bf3f41b13c1 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -260,7 +260,7 @@ impl OsString { /// /// let b: Box<OsStr> = s.into_boxed_os_str(); /// ``` - #[unstable(feature = "into_boxed_os_str", issue = "0")] + #[unstable(feature = "into_boxed_os_str", issue = "40380")] pub fn into_boxed_os_str(self) -> Box<OsStr> { unsafe { mem::transmute(self.inner.into_box()) } } @@ -506,6 +506,13 @@ impl OsStr { self.inner.inner.len() } + /// Converts a `Box<OsStr>` into an `OsString` without copying or allocating. + #[unstable(feature = "into_boxed_os_str", issue = "40380")] + pub fn into_os_string(self: Box<OsStr>) -> OsString { + let inner: Box<Slice> = unsafe { mem::transmute(self) }; + OsString { inner: Buf::from_box(inner) } + } + /// Gets the underlying byte representation. /// /// Note: it is *crucial* that this API is private, to avoid @@ -522,6 +529,20 @@ impl<'a> From<&'a OsStr> for Box<OsStr> { } } +#[stable(feature = "os_string_from_box", since = "1.17.0")] +impl<'a> From<Box<OsStr>> for OsString { + fn from(boxed: Box<OsStr>) -> OsString { + boxed.into_os_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into<Box<OsStr>> for OsString { + fn into(self) -> Box<OsStr> { + self.into_boxed_os_str() + } +} + #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box<OsStr> { fn default() -> Box<OsStr> { @@ -830,12 +851,11 @@ mod tests { 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); + let boxed: Box<OsStr> = Box::from(os_str); + let os_string = os_str.to_owned().into_boxed_os_str().into_os_string(); + assert_eq!(os_str, &*boxed); + assert_eq!(&*boxed, &*os_string); + assert_eq!(&*os_string, os_str); } #[test] diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 245a6d945b5..49b01bc0853 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1196,7 +1196,7 @@ impl PathBuf { } /// Converts this `PathBuf` into a boxed `Path`. - #[unstable(feature = "into_boxed_path", issue = "0")] + #[unstable(feature = "into_boxed_path", issue = "40380")] pub fn into_boxed_path(self) -> Box<Path> { unsafe { mem::transmute(self.inner.into_boxed_os_str()) } } @@ -1210,6 +1210,20 @@ impl<'a> From<&'a Path> for Box<Path> { } } +#[stable(feature = "path_buf_from_box", since = "1.17.0")] +impl<'a> From<Box<Path>> for PathBuf { + fn from(boxed: Box<Path>) -> PathBuf { + boxed.into_path_buf() + } +} + +#[stable(feature = "box_from_path_buf", since = "1.17.0")] +impl Into<Box<Path>> for PathBuf { + fn into(self) -> Box<Path> { + self.into_boxed_path() + } +} + #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box<Path> { fn default() -> Box<Path> { @@ -2089,6 +2103,13 @@ impl Path { pub fn is_dir(&self) -> bool { fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) } + + /// Converts a `Box<Path>` into a `PathBuf` without copying or allocating. + #[unstable(feature = "into_boxed_path", issue = "40380")] + pub fn into_path_buf(self: Box<Path>) -> PathBuf { + let inner: Box<OsStr> = unsafe { mem::transmute(self) }; + PathBuf { inner: OsString::from(inner) } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -3703,12 +3724,11 @@ mod tests { 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); + let boxed: Box<Path> = Box::from(path); + let path_buf = path.to_owned().into_boxed_path().into_path_buf(); + assert_eq!(path, &*boxed); + assert_eq!(&*boxed, &*path_buf); + assert_eq!(&*path_buf, path); } #[test] diff --git a/src/libstd/sys/redox/os_str.rs b/src/libstd/sys/redox/os_str.rs index 474d59eed83..c2bba07f68c 100644 --- a/src/libstd/sys/redox/os_str.rs +++ b/src/libstd/sys/redox/os_str.rs @@ -104,6 +104,12 @@ impl Buf { pub fn into_box(self) -> Box<Slice> { unsafe { mem::transmute(self.inner.into_boxed_slice()) } } + + #[inline] + pub fn from_box(boxed: Box<Slice>) -> Buf { + let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; + Buf { inner: inner.into_vec() } + } } impl Slice { diff --git a/src/libstd/sys/unix/os_str.rs b/src/libstd/sys/unix/os_str.rs index c27599ec020..f5b942d3343 100644 --- a/src/libstd/sys/unix/os_str.rs +++ b/src/libstd/sys/unix/os_str.rs @@ -104,6 +104,12 @@ impl Buf { pub fn into_box(self) -> Box<Slice> { unsafe { mem::transmute(self.inner.into_boxed_slice()) } } + + #[inline] + pub fn from_box(boxed: Box<Slice>) -> Buf { + let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; + Buf { inner: inner.into_vec() } + } } impl Slice { diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs index b02b06e1ef2..f401e7b35c8 100644 --- a/src/libstd/sys/windows/os_str.rs +++ b/src/libstd/sys/windows/os_str.rs @@ -97,6 +97,12 @@ impl Buf { pub fn into_box(self) -> Box<Slice> { unsafe { mem::transmute(self.inner.into_box()) } } + + #[inline] + pub fn from_box(boxed: Box<Slice>) -> Buf { + let inner: Box<Wtf8> = unsafe { mem::transmute(boxed) }; + Buf { inner: Wtf8Buf::from_box(inner) } + } } impl Slice { diff --git a/src/libstd/sys_common/wtf8.rs b/src/libstd/sys_common/wtf8.rs index b486d4ffda3..79aaf34ce2e 100644 --- a/src/libstd/sys_common/wtf8.rs +++ b/src/libstd/sys_common/wtf8.rs @@ -351,6 +351,12 @@ impl Wtf8Buf { pub fn into_box(self) -> Box<Wtf8> { unsafe { mem::transmute(self.bytes.into_boxed_slice()) } } + + /// Converts a `Box<Wtf8>` into a `Wtf8Buf`. + pub fn from_box(boxed: Box<Wtf8>) -> Wtf8Buf { + let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) }; + Wtf8Buf { bytes: bytes.into_vec() } + } } /// Create a new WTF-8 string from an iterator of code points. |
