diff options
| author | Kevin Ballard <kevin@sb.org> | 2014-05-14 16:55:24 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2014-05-14 17:35:55 -0700 |
| commit | ba7844a7fff0061e5b4528c2ecd5adf765145b70 (patch) | |
| tree | bf1998a94512013b15d5216388b1d99db7b54935 /src/libstd | |
| parent | d0f3cb05df41b14b58553fab6a533e0e4c947b06 (diff) | |
| download | rust-ba7844a7fff0061e5b4528c2ecd5adf765145b70.tar.gz rust-ba7844a7fff0061e5b4528c2ecd5adf765145b70.zip | |
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]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/num/strconv.rs | 1 | ||||
| -rw-r--r-- | src/libstd/strbuf.rs | 14 |
2 files changed, 10 insertions, 5 deletions
diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 4769b17fb2b..63d6219ab8a 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -19,6 +19,7 @@ use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use num; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; +use result::ResultUnwrap; use slice::{CloneableVector, ImmutableVector, MutableVector}; use std::cmp::{Ord, Eq}; use str::{StrAllocating, StrSlice}; diff --git a/src/libstd/strbuf.rs b/src/libstd/strbuf.rs index 575de89fae2..de480ef1b7f 100644 --- a/src/libstd/strbuf.rs +++ b/src/libstd/strbuf.rs @@ -20,6 +20,7 @@ use mem; use option::{None, Option, Some}; use ptr::RawPtr; use ptr; +use result::{Result, Ok, Err}; use slice::{OwnedVector, Vector, CloneableVector}; use str::{CharRange, OwnedStr, Str, StrSlice, StrAllocating}; use str; @@ -72,14 +73,17 @@ impl StrBuf { } } - /// Tries to create a new string buffer from the given byte - /// vector, validating that the vector is UTF-8 encoded. + /// Returns the vector as a string buffer, if possible, taking care not to + /// copy it. + /// + /// Returns `Err` with the original vector if the vector contains invalid + /// UTF-8. #[inline] - pub fn from_utf8(vec: Vec<u8>) -> Option<StrBuf> { + pub fn from_utf8(vec: Vec<u8>) -> Result<StrBuf, Vec<u8>> { if str::is_utf8(vec.as_slice()) { - Some(StrBuf { vec: vec }) + Ok(StrBuf { vec: vec }) } else { - None + Err(vec) } } |
