@@ -145,13 +145,13 @@ impl<'a> fmt::Show for Ascii {
145
145
/// Trait for converting into an ascii type.
146
146
#[ experimental = "may be replaced by generic conversion traits" ]
147
147
pub trait AsciiCast < T , U = Self > for Sized ?: AsciiExt < U > {
148
- /// Convert to an ascii type, return None on non-ASCII input.
148
+ /// Convert to an ascii type, return Err(()) on non-ASCII input.
149
149
#[ inline]
150
- fn to_ascii ( & self ) -> Option < T > {
150
+ fn to_ascii ( & self ) -> Result < T , ( ) > {
151
151
if self . is_ascii ( ) {
152
- Some ( unsafe { self . to_ascii_nocheck ( ) } )
152
+ Ok ( unsafe { self . to_ascii_nocheck ( ) } )
153
153
} else {
154
- None
154
+ Err ( ( ) )
155
155
}
156
156
}
157
157
@@ -195,16 +195,16 @@ impl AsciiCast<Ascii> for char {
195
195
#[ experimental = "may be replaced by generic conversion traits" ]
196
196
pub trait OwnedAsciiCast < Sized ? T , U = Self >
197
197
where T : BorrowFrom < Self > + AsciiExt < U > {
198
- /// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
198
+ /// Take ownership and cast to an ascii vector. Return Err(()) on non-ASCII input.
199
199
#[ inline]
200
- fn into_ascii ( self ) -> Option < Vec < Ascii > > {
200
+ fn into_ascii ( self ) -> Result < Vec < Ascii > , Self > {
201
201
if {
202
202
let borrowed: & T = BorrowFrom :: borrow_from ( & self ) ;
203
203
borrowed. is_ascii ( )
204
204
} {
205
- Some ( unsafe { self . into_ascii_nocheck ( ) } )
205
+ Ok ( unsafe { self . into_ascii_nocheck ( ) } )
206
206
} else {
207
- None
207
+ Err ( self )
208
208
}
209
209
}
210
210
@@ -316,9 +316,9 @@ mod tests {
316
316
assert ! ( !'/' . to_ascii( ) . unwrap( ) . is_digit( ) ) ;
317
317
assert ! ( !':' . to_ascii( ) . unwrap( ) . is_digit( ) ) ;
318
318
319
- assert ! ( ( 0x1fu8 ) . to_ascii( ) . unwrap( ) . is_control( ) ) ;
319
+ assert ! ( 0x1f_u8 . to_ascii( ) . unwrap( ) . is_control( ) ) ;
320
320
assert ! ( !' ' . to_ascii( ) . unwrap( ) . is_control( ) ) ;
321
- assert ! ( ( 0x7fu8 ) . to_ascii( ) . unwrap( ) . is_control( ) ) ;
321
+ assert ! ( 0x7f_u8 . to_ascii( ) . unwrap( ) . is_control( ) ) ;
322
322
}
323
323
324
324
#[ test]
@@ -357,32 +357,32 @@ mod tests {
357
357
358
358
#[ test]
359
359
fn test_opt ( ) {
360
- assert_eq ! ( 65u8 . to_ascii( ) , Some ( Ascii { chr: 65u8 } ) ) ;
361
- assert_eq ! ( 255u8 . to_ascii( ) , None ) ;
360
+ assert_eq ! ( 65u8 . to_ascii( ) , Ok ( Ascii { chr: 65u8 } ) ) ;
361
+ assert_eq ! ( 255u8 . to_ascii( ) , Err ( ( ) ) ) ;
362
362
363
- assert_eq ! ( 'A' . to_ascii( ) , Some ( Ascii { chr: 65u8 } ) ) ;
364
- assert_eq ! ( 'λ' . to_ascii( ) , None ) ;
363
+ assert_eq ! ( 'A' . to_ascii( ) , Ok ( Ascii { chr: 65u8 } ) ) ;
364
+ assert_eq ! ( 'λ' . to_ascii( ) , Err ( ( ) ) ) ;
365
365
366
- assert_eq ! ( "zoä华" . to_ascii( ) , None ) ;
366
+ assert_eq ! ( "zoä华" . to_ascii( ) , Err ( ( ) ) ) ;
367
367
368
368
let test1 = & [ 127u8 , 128u8 , 255u8 ] ;
369
- assert_eq ! ( ( test1) . to_ascii( ) , None ) ;
369
+ assert_eq ! ( test1. to_ascii( ) , Err ( ( ) ) ) ;
370
370
371
371
let v = [ 40u8 , 32u8 , 59u8 ] ;
372
372
let v2: & [ _ ] = v2ascii ! ( & [ 40 , 32 , 59 ] ) ;
373
- assert_eq ! ( v. to_ascii( ) , Some ( v2) ) ;
373
+ assert_eq ! ( v. to_ascii( ) , Ok ( v2) ) ;
374
374
let v = [ 127u8 , 128u8 , 255u8 ] ;
375
- assert_eq ! ( v. to_ascii( ) , None ) ;
375
+ assert_eq ! ( v. to_ascii( ) , Err ( ( ) ) ) ;
376
376
377
377
let v = "( ;" ;
378
- assert_eq ! ( v. to_ascii( ) , Some ( v2) ) ;
379
- assert_eq ! ( "zoä华" . to_ascii( ) , None ) ;
378
+ assert_eq ! ( v. to_ascii( ) , Ok ( v2) ) ;
379
+ assert_eq ! ( "zoä华" . to_ascii( ) , Err ( ( ) ) ) ;
380
380
381
- assert_eq ! ( ( vec![ 40u8 , 32u8 , 59u8 ] ) . into_ascii( ) , Some ( vec2ascii![ 40 , 32 , 59 ] ) ) ;
382
- assert_eq ! ( ( vec![ 127u8 , 128u8 , 255u8 ] ) . into_ascii( ) , None ) ;
381
+ assert_eq ! ( vec![ 40u8 , 32u8 , 59u8 ] . into_ascii( ) , Ok ( vec2ascii![ 40 , 32 , 59 ] ) ) ;
382
+ assert_eq ! ( vec![ 127u8 , 128u8 , 255u8 ] . into_ascii( ) , Err ( vec! [ 127u8 , 128u8 , 255u8 ] ) ) ;
383
383
384
- assert_eq ! ( ( "( ;" . to_string( ) ) . into_ascii( ) , Some ( vec2ascii![ 40 , 32 , 59 ] ) ) ;
385
- assert_eq ! ( ( "zoä华" . to_string( ) ) . into_ascii( ) , None ) ;
384
+ assert_eq ! ( "( ;" . to_string( ) . into_ascii( ) , Ok ( vec2ascii![ 40 , 32 , 59 ] ) ) ;
385
+ assert_eq ! ( "zoä华" . to_string( ) . into_ascii( ) , Err ( "zoä华" . to_string ( ) ) ) ;
386
386
}
387
387
388
388
#[ test]
0 commit comments