Skip to content

Commit 6a0ecc7

Browse files
committed
Do not loose self in into_ascii() in case of error.
Return Err(self) from into_ascii() in case of error, so the original value is not lost.
1 parent 0621f65 commit 6a0ecc7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/libstd/ascii.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use iter::Iterator;
2222
use vec::{ImmutableVector, MutableVector, Vector};
2323
use to_bytes::IterBytes;
2424
use option::{Option, Some, None};
25+
use result::{Result, Ok, Err};
2526

2627
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
2728
#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq)]
@@ -213,13 +214,13 @@ pub trait OwnedAsciiCast {
213214
/// Check if convertible to ascii
214215
fn is_ascii(&self) -> bool;
215216

216-
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
217+
/// Take ownership and cast to an ascii vector. Return Err(Self) on non-ASCII input.
217218
#[inline]
218-
fn into_ascii(self) -> Option<~[Ascii]> {
219+
fn into_ascii(self) -> Result<~[Ascii], Self> {
219220
if self.is_ascii() {
220-
Some(unsafe { self.into_ascii_nocheck() })
221+
Ok(unsafe { self.into_ascii_nocheck() })
221222
} else {
222-
None
223+
Err(self)
223224
}
224225
}
225226

@@ -536,8 +537,8 @@ mod tests {
536537

537538
#[test]
538539
fn test_owned_ascii_vec() {
539-
assert_eq!((~"( ;").into_ascii(), Some(v2ascii!(~[40, 32, 59])));
540-
assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), Some(v2ascii!(~[40, 32, 59])));
540+
assert_eq!((~"( ;").into_ascii(), Ok(v2ascii!(~[40, 32, 59])));
541+
assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), Ok(v2ascii!(~[40, 32, 59])));
541542
}
542543
543544
#[test]
@@ -592,11 +593,11 @@ mod tests {
592593
assert_eq!(v.to_ascii(), Some(v2));
593594
assert_eq!("zoä华".to_ascii(), None);
594595
595-
assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), Some(v2ascii!(~[40, 32, 59])));
596-
assert_eq!((~[127u8, 128u8, 255u8]).into_ascii(), None);
596+
assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), Ok(v2ascii!(~[40, 32, 59])));
597+
assert_eq!((~[127u8, 128u8, 255u8]).into_ascii(), Err(~[127u8, 128u8, 255u8]));
597598
598-
assert_eq!((~"( ;").into_ascii(), Some(v2ascii!(~[40, 32, 59])));
599-
assert_eq!((~"zoä华").into_ascii(), None);
599+
assert_eq!((~"( ;").into_ascii(), Ok(v2ascii!(~[40, 32, 59])));
600+
assert_eq!((~"zoä华").into_ascii(), Err(~"zoä华"));
600601
}
601602
602603
#[test]

0 commit comments

Comments
 (0)