Skip to content

Commit 9e030fc

Browse files
committed
Have to_ascii and into_ascii return Result rather than Option.
This enables using them with try!(). For into_ascii, the Err case also returns the origin String or Vec.
1 parent 96ae57c commit 9e030fc

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/lib.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ impl<'a> fmt::Show for Ascii {
145145
/// Trait for converting into an ascii type.
146146
#[experimental = "may be replaced by generic conversion traits"]
147147
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.
149149
#[inline]
150-
fn to_ascii(&self) -> Option<T> {
150+
fn to_ascii(&self) -> Result<T, ()> {
151151
if self.is_ascii() {
152-
Some(unsafe { self.to_ascii_nocheck() })
152+
Ok(unsafe { self.to_ascii_nocheck() })
153153
} else {
154-
None
154+
Err(())
155155
}
156156
}
157157

@@ -195,16 +195,16 @@ impl AsciiCast<Ascii> for char {
195195
#[experimental = "may be replaced by generic conversion traits"]
196196
pub trait OwnedAsciiCast<Sized? T, U = Self>
197197
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.
199199
#[inline]
200-
fn into_ascii(self) -> Option<Vec<Ascii>> {
200+
fn into_ascii(self) -> Result<Vec<Ascii>, Self> {
201201
if {
202202
let borrowed: &T = BorrowFrom::borrow_from(&self);
203203
borrowed.is_ascii()
204204
} {
205-
Some(unsafe { self.into_ascii_nocheck() })
205+
Ok(unsafe { self.into_ascii_nocheck() })
206206
} else {
207-
None
207+
Err(self)
208208
}
209209
}
210210

@@ -316,9 +316,9 @@ mod tests {
316316
assert!(!'/'.to_ascii().unwrap().is_digit());
317317
assert!(!':'.to_ascii().unwrap().is_digit());
318318

319-
assert!((0x1fu8).to_ascii().unwrap().is_control());
319+
assert!(0x1f_u8.to_ascii().unwrap().is_control());
320320
assert!(!' '.to_ascii().unwrap().is_control());
321-
assert!((0x7fu8).to_ascii().unwrap().is_control());
321+
assert!(0x7f_u8.to_ascii().unwrap().is_control());
322322
}
323323

324324
#[test]
@@ -357,32 +357,32 @@ mod tests {
357357

358358
#[test]
359359
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(()));
362362

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(()));
365365

366-
assert_eq!("zoä华".to_ascii(), None);
366+
assert_eq!("zoä华".to_ascii(), Err(()));
367367

368368
let test1 = &[127u8, 128u8, 255u8];
369-
assert_eq!((test1).to_ascii(), None);
369+
assert_eq!(test1.to_ascii(), Err(()));
370370

371371
let v = [40u8, 32u8, 59u8];
372372
let v2: &[_] = v2ascii!(&[40, 32, 59]);
373-
assert_eq!(v.to_ascii(), Some(v2));
373+
assert_eq!(v.to_ascii(), Ok(v2));
374374
let v = [127u8, 128u8, 255u8];
375-
assert_eq!(v.to_ascii(), None);
375+
assert_eq!(v.to_ascii(), Err(()));
376376

377377
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(()));
380380

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]));
383383

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()));
386386
}
387387

388388
#[test]

0 commit comments

Comments
 (0)