From 5a09d7c3de6cc5daf1b9925a8e7d19edf44c27a7 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Mon, 6 Mar 2017 09:03:31 +0300 Subject: [PATCH 1/6] Add `as_bytes()` for `FromUtf8Error`. This change allows to obtain an underlying invalid UTF-8 bytes without `FromUtf8Error` destruction. Such method may be useful for example in a library that attempts to save both valid and invalid UTF-8 strings in some struct and to be able to provide immutable access to it without destruction. --- src/libcollections/string.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 7d9d7276201bd..12f582404dc7e 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1403,6 +1403,25 @@ impl String { } impl FromUtf8Error { + /// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// // some invalid bytes, in a vector + /// let bytes = vec![0, 159]; + /// + /// let value = String::from_utf8(bytes); + /// + /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes()); + /// ``` + #[unstable(feature = "from_utf8_error_as_bytes", issue = "40895")] + pub fn as_bytes(&self) -> &[u8] { + &self.bytes[..] + } + /// Returns the bytes that were attempted to convert to a `String`. /// /// This method is carefully constructed to avoid allocation. It will From 4cb9de48fbf3e616d80bb6726ce590e06c763a1e Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Sat, 8 Apr 2017 19:37:46 +0300 Subject: [PATCH 2/6] fix: add feature to doc tests --- src/libcollections/string.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 12f582404dc7e..6da4cc1c7138b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1410,6 +1410,7 @@ impl FromUtf8Error { /// Basic usage: /// /// ``` + /// #![feature(from_utf8_error_as_bytes)] /// // some invalid bytes, in a vector /// let bytes = vec![0, 159]; /// From 0eecd5150d7d7456f324fe654076d6f9709c5106 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Sat, 8 Apr 2017 21:25:30 +0300 Subject: [PATCH 3/6] refactor: add reason string --- src/libcollections/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 6da4cc1c7138b..8d6cf30511260 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1418,7 +1418,7 @@ impl FromUtf8Error { /// /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes()); /// ``` - #[unstable(feature = "from_utf8_error_as_bytes", issue = "40895")] + #[unstable(feature = "from_utf8_error_as_bytes", reason = "recently added", issue = "40895")] pub fn as_bytes(&self) -> &[u8] { &self.bytes[..] } From 4cea3f74037bf0f7c61968949a764b5de20414b5 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Mon, 10 Apr 2017 19:12:33 +0300 Subject: [PATCH 4/6] docs: track newly introduced unstable feature --- src/doc/unstable-book/src/from_utf8_error_as_bytes.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/doc/unstable-book/src/from_utf8_error_as_bytes.md diff --git a/src/doc/unstable-book/src/from_utf8_error_as_bytes.md b/src/doc/unstable-book/src/from_utf8_error_as_bytes.md new file mode 100644 index 0000000000000..570f779417f09 --- /dev/null +++ b/src/doc/unstable-book/src/from_utf8_error_as_bytes.md @@ -0,0 +1,7 @@ +# `from_utf8_error_as_bytes` + +The tracking issue for this feature is: [#40895] + +[#40895]: https://github.com/rust-lang/rust/issues/40895 + +------------------------ From 3ae20deac7f7a4e9d5bcb640dd4e2eacd9c2cfe3 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Tue, 11 Apr 2017 09:01:10 +0300 Subject: [PATCH 5/6] fix: add missing link in SUMMARY.md --- src/doc/unstable-book/src/SUMMARY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 14774e2d2293c..9acb7b4afe7e1 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -78,6 +78,8 @@ - [fmt_internals](fmt-internals.md) - [fn_traits](fn-traits.md) - [fnbox](fnbox.md) +- [field_init_shorthand](field-init-shorthand.md) +- [from_utf8_error_as_bytes](from_utf8_error_as_bytes.md) - [fundamental](fundamental.md) - [fused](fused.md) - [future_atomic_orderings](future-atomic-orderings.md) From bbdf190c6a2d5bc095c8d3addff0868395bff31f Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Mon, 17 Apr 2017 09:41:12 +0300 Subject: [PATCH 6/6] fix: remove `field_init_shorthand` from SUMMARY.md --- src/doc/unstable-book/src/SUMMARY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 9acb7b4afe7e1..fe638fd641773 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -78,7 +78,6 @@ - [fmt_internals](fmt-internals.md) - [fn_traits](fn-traits.md) - [fnbox](fnbox.md) -- [field_init_shorthand](field-init-shorthand.md) - [from_utf8_error_as_bytes](from_utf8_error_as_bytes.md) - [fundamental](fundamental.md) - [fused](fused.md)