diff --git a/configure b/configure index 35b376d5f27b8..d6dded6dc5f7b 100755 --- a/configure +++ b/configure @@ -445,6 +445,7 @@ opt dist-host-only 0 "only install bins for the host architecture" opt inject-std-version 1 "inject the current compiler version of libstd into programs" opt llvm-version-check 1 "check if the LLVM version is supported, build anyway" opt codegen-tests 1 "run the src/test/codegen tests" +opt save-analysis 0 "save API analysis data" opt option-checking 1 "complain about unrecognized options in this configure script" opt ninja 0 "build LLVM using the Ninja generator (for MSVC, requires building in the correct environment)" opt locked-deps 0 "force Cargo.lock to be up to date" diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index dcd49c51e3a99..b1d1d79b9eaa3 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -74,6 +74,7 @@ pub struct Config { pub rustc_default_ar: Option, 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, debuginfo_tests: Option, codegen_tests: Option, + save_analysis: Option, } /// 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 5a00e90f370b5..76fedae0f03eb 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 7c463bec5ff7c..289c1e1c3a99c 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 ba8442ebd8c37..249f241a151bb 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 72aec15e532b5..2d2be531e628c 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 19bea9ced0642..d8b317a46c31e 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 43323676ab459..13c99a2d59bed 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> for String { + fn from(s: Box) -> String { + s.into_string() + } +} + +#[stable(feature = "box_from_str", since = "1.17.0")] +impl Into> for String { + fn into(self) -> Box { + self.into_boxed_str() + } +} + #[stable(feature = "string_from_cow_str", since = "1.14.0")] impl<'a> From> for String { fn from(s: Cow<'a, str>) -> String { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index d38c9f6e1cf80..e4a6af33409e1 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1897,6 +1897,22 @@ impl<'a, T> From> for Vec where [T]: ToOwned> { } } +// note: test pulls in libstd, which causes errors here +#[cfg(not(test))] +#[stable(feature = "vec_from_box", since = "1.17.0")] +impl From> for Vec { + fn from(s: Box<[T]>) -> Vec { + s.into_vec() + } +} + +#[stable(feature = "box_from_vec", since = "1.17.0")] +impl Into> for Vec { + fn into(self) -> Box<[T]> { + self.into_boxed_slice() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a> From<&'a str> for Vec { fn from(s: &'a str) -> Vec { diff --git a/src/libcollectionstest/lib.rs b/src/libcollectionstest/lib.rs index d97d9b8ab83f6..98d0b1c8e1565 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 8071c7e8c20d5..c9b7104fec4f0 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -540,6 +540,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 diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 52e3301631052..2919adc1cbc63 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, } 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 { + 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 bfb0aa6e1a122..2d14bb66bf4f9 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 { unsafe { mem::transmute(self.into_inner()) } } @@ -415,6 +415,20 @@ impl<'a> From<&'a CStr> for Box { } } +#[stable(feature = "c_string_from_box", since = "1.17.0")] +impl From> for CString { + fn from(s: Box) -> CString { + s.into_c_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into> for CString { + fn into(self) -> Box { + self.into_boxed_c_str() + } +} + #[stable(feature = "default_box_extra", since = "1.17.0")] impl Default for Box { fn default() -> Box { @@ -728,6 +742,12 @@ impl CStr { pub fn to_string_lossy(&self) -> Cow { String::from_utf8_lossy(self.to_bytes()) } + + /// Converts a `Box` into a `CString` without copying or allocating. + #[unstable(feature = "into_boxed_c_str", issue = "40380")] + pub fn into_c_string(self: Box) -> 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 = 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 = 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 b0f79f9a395a6..bf3f41b13c184 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -260,7 +260,7 @@ impl OsString { /// /// let b: Box = 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 { unsafe { mem::transmute(self.inner.into_box()) } } @@ -506,6 +506,13 @@ impl OsStr { self.inner.inner.len() } + /// Converts a `Box` into an `OsString` without copying or allocating. + #[unstable(feature = "into_boxed_os_str", issue = "40380")] + pub fn into_os_string(self: Box) -> OsString { + let inner: Box = 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 { } } +#[stable(feature = "os_string_from_box", since = "1.17.0")] +impl<'a> From> for OsString { + fn from(boxed: Box) -> OsString { + boxed.into_os_string() + } +} + +#[stable(feature = "box_from_c_string", since = "1.17.0")] +impl Into> for OsString { + fn into(self) -> Box { + self.into_boxed_os_str() + } +} + #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box { fn default() -> Box { @@ -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 = 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 = 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 245a6d945b5a3..49b01bc085373 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 { unsafe { mem::transmute(self.inner.into_boxed_os_str()) } } @@ -1210,6 +1210,20 @@ impl<'a> From<&'a Path> for Box { } } +#[stable(feature = "path_buf_from_box", since = "1.17.0")] +impl<'a> From> for PathBuf { + fn from(boxed: Box) -> PathBuf { + boxed.into_path_buf() + } +} + +#[stable(feature = "box_from_path_buf", since = "1.17.0")] +impl Into> for PathBuf { + fn into(self) -> Box { + self.into_boxed_path() + } +} + #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box { fn default() -> Box { @@ -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` into a `PathBuf` without copying or allocating. + #[unstable(feature = "into_boxed_path", issue = "40380")] + pub fn into_path_buf(self: Box) -> PathBuf { + let inner: Box = 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 = 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 = 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 474d59eed83d1..c2bba07f68ce0 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 { unsafe { mem::transmute(self.inner.into_boxed_slice()) } } + + #[inline] + pub fn from_box(boxed: Box) -> 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 c27599ec0206f..f5b942d3343dc 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 { unsafe { mem::transmute(self.inner.into_boxed_slice()) } } + + #[inline] + pub fn from_box(boxed: Box) -> 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 b02b06e1ef2e1..f401e7b35c8d8 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 { unsafe { mem::transmute(self.inner.into_box()) } } + + #[inline] + pub fn from_box(boxed: Box) -> Buf { + let inner: Box = 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 b486d4ffda3fd..79aaf34ce2e0f 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 { unsafe { mem::transmute(self.bytes.into_boxed_slice()) } } + + /// Converts a `Box` into a `Wtf8Buf`. + pub fn from_box(boxed: Box) -> 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.