Skip to content

Commit ba7844a

Browse files
committed
Change StrBuf::from_utf8() to return Result
This allows the original vector to be recovered in the event that it is not UTF-8. [breaking-change]
1 parent d0f3cb0 commit ba7844a

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/libserialize/base64.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ impl<'a> FromBase64 for &'a str {
181181
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
182182
* to the byte values it encodes.
183183
*
184-
* You can use the `from_utf8_owned` function in `std::str`
185-
* to turn a `[u8]` into a string with characters corresponding to those
186-
* values.
184+
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
185+
* `Vec<u8>` into a string with characters corresponding to those values.
187186
*
188187
* # Example
189188
*
@@ -199,7 +198,7 @@ impl<'a> FromBase64 for &'a str {
199198
* let res = hello_str.from_base64();
200199
* if res.is_ok() {
201200
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
202-
* if opt_bytes.is_some() {
201+
* if opt_bytes.is_ok() {
203202
* println!("decoded from base64: {}", opt_bytes.unwrap());
204203
* }
205204
* }

src/libserialize/hex.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ impl<'a> FromHex for &'a str {
8080
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
8181
* to the byte values it encodes.
8282
*
83-
* You can use the `from_utf8_owned` function in `std::str`
84-
* to turn a `[u8]` into a string with characters corresponding to those
85-
* values.
83+
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
84+
* `Vec<u8>` into a string with characters corresponding to those values.
8685
*
8786
* # Example
8887
*

src/libstd/num/strconv.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use num::{Float, FPNaN, FPInfinite, ToPrimitive};
1919
use num;
2020
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2121
use option::{None, Option, Some};
22+
use result::ResultUnwrap;
2223
use slice::{CloneableVector, ImmutableVector, MutableVector};
2324
use std::cmp::{Ord, Eq};
2425
use str::{StrAllocating, StrSlice};

src/libstd/strbuf.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use mem;
2020
use option::{None, Option, Some};
2121
use ptr::RawPtr;
2222
use ptr;
23+
use result::{Result, Ok, Err};
2324
use slice::{OwnedVector, Vector, CloneableVector};
2425
use str::{CharRange, OwnedStr, Str, StrSlice, StrAllocating};
2526
use str;
@@ -72,14 +73,17 @@ impl StrBuf {
7273
}
7374
}
7475

75-
/// Tries to create a new string buffer from the given byte
76-
/// vector, validating that the vector is UTF-8 encoded.
76+
/// Returns the vector as a string buffer, if possible, taking care not to
77+
/// copy it.
78+
///
79+
/// Returns `Err` with the original vector if the vector contains invalid
80+
/// UTF-8.
7781
#[inline]
78-
pub fn from_utf8(vec: Vec<u8>) -> Option<StrBuf> {
82+
pub fn from_utf8(vec: Vec<u8>) -> Result<StrBuf, Vec<u8>> {
7983
if str::is_utf8(vec.as_slice()) {
80-
Some(StrBuf { vec: vec })
84+
Ok(StrBuf { vec: vec })
8185
} else {
82-
None
86+
Err(vec)
8387
}
8488
}
8589

0 commit comments

Comments
 (0)