@@ -22,6 +22,7 @@ use iter::Iterator;
22
22
use vec:: { ImmutableVector , MutableVector , Vector } ;
23
23
use to_bytes:: IterBytes ;
24
24
use option:: { Option , Some , None } ;
25
+ use result:: { Result , Ok , Err } ;
25
26
26
27
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
27
28
#[ deriving( Clone , Eq , Ord , TotalOrd , TotalEq ) ]
@@ -213,13 +214,13 @@ pub trait OwnedAsciiCast {
213
214
/// Check if convertible to ascii
214
215
fn is_ascii ( & self ) -> bool ;
215
216
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.
217
218
#[ inline]
218
- fn into_ascii ( self ) -> Option < ~[ Ascii ] > {
219
+ fn into_ascii ( self ) -> Result < ~[ Ascii ] , Self > {
219
220
if self . is_ascii ( ) {
220
- Some ( unsafe { self . into_ascii_nocheck ( ) } )
221
+ Ok ( unsafe { self . into_ascii_nocheck ( ) } )
221
222
} else {
222
- None
223
+ Err ( self )
223
224
}
224
225
}
225
226
@@ -536,8 +537,8 @@ mod tests {
536
537
537
538
#[ test]
538
539
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])));
541
542
}
542
543
543
544
#[test]
@@ -592,11 +593,11 @@ mod tests {
592
593
assert_eq!(v.to_ascii(), Some(v2));
593
594
assert_eq!(" zoä华".to_ascii(), None);
594
595
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]) );
597
598
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ä华 ") );
600
601
}
601
602
602
603
#[test]
0 commit comments