Skip to content
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

Add as_bytes() for FromUtf8Error. #40290

Merged
merged 6 commits into from
Apr 18, 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
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
- [fmt_internals](fmt-internals.md)
- [fn_traits](fn-traits.md)
- [fnbox](fnbox.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)
Expand Down
7 changes: 7 additions & 0 deletions src/doc/unstable-book/src/from_utf8_error_as_bytes.md
Original file line number Diff line number Diff line change
@@ -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

------------------------
20 changes: 20 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,26 @@ impl String {
}

impl FromUtf8Error {
/// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(from_utf8_error_as_bytes)]
/// // 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", reason = "recently added", 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
Expand Down