about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
authorEvgeny Safronov <division494@gmail.com>2017-03-06 09:03:31 +0300
committerEvgeny Safronov <division494@gmail.com>2017-04-14 09:46:03 +0300
commit5a09d7c3de6cc5daf1b9925a8e7d19edf44c27a7 (patch)
tree730e929fb44e5cd1520334b31762cbff6340c1fb /src/libcollections
parent4f32e0dfb287c2b3d0c48cb3b8090b3902960084 (diff)
downloadrust-5a09d7c3de6cc5daf1b9925a8e7d19edf44c27a7.tar.gz
rust-5a09d7c3de6cc5daf1b9925a8e7d19edf44c27a7.zip
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.
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/string.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs
index 7d9d7276201..12f582404dc 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