Skip to content

Commit 79f21e6

Browse files
committed
Remove to_ascii and into_ascii methods that panic on failure.
1 parent 47f74b2 commit 79f21e6

File tree

1 file changed

+15
-53
lines changed

1 file changed

+15
-53
lines changed

src/lib.rs

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ 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, panic on non-ASCII input.
149-
#[inline]
150-
fn to_ascii(&self) -> T {
151-
assert!(self.is_ascii());
152-
unsafe {self.to_ascii_nocheck()}
153-
}
154-
155148
/// Convert to an ascii type, return None on non-ASCII input.
156149
#[inline]
157150
fn to_ascii_opt(&self) -> Option<T> {
@@ -202,19 +195,6 @@ impl AsciiCast<Ascii> for char {
202195
#[experimental = "may be replaced by generic conversion traits"]
203196
pub trait OwnedAsciiCast<Sized? T, U = Self>
204197
where T: BorrowFrom<Self> + AsciiExt<U> {
205-
/// Take ownership and cast to an ascii vector.
206-
/// # Panics
207-
///
208-
/// Panic on non-ASCII input.
209-
#[inline]
210-
fn into_ascii(self) -> Vec<Ascii> {
211-
assert!({
212-
let borrowed: &T = BorrowFrom::borrow_from(&self);
213-
borrowed.is_ascii()
214-
});
215-
unsafe {self.into_ascii_nocheck()}
216-
}
217-
218198
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
219199
#[inline]
220200
fn into_ascii_opt(self) -> Option<Vec<Ascii>> {
@@ -326,36 +306,30 @@ mod tests {
326306

327307
#[test]
328308
fn test_ascii() {
329-
assert_eq!(65u8.to_ascii().as_byte(), 65u8);
330-
assert_eq!(65u8.to_ascii().as_char(), 'A');
331-
assert_eq!('A'.to_ascii().as_char(), 'A');
332-
assert_eq!('A'.to_ascii().as_byte(), 65u8);
309+
assert_eq!(65u8.to_ascii_opt().unwrap().as_byte(), 65u8);
310+
assert_eq!(65u8.to_ascii_opt().unwrap().as_char(), 'A');
311+
assert_eq!('A'.to_ascii_opt().unwrap().as_char(), 'A');
312+
assert_eq!('A'.to_ascii_opt().unwrap().as_byte(), 65u8);
333313

334-
assert!('0'.to_ascii().is_digit());
335-
assert!('9'.to_ascii().is_digit());
336-
assert!(!'/'.to_ascii().is_digit());
337-
assert!(!':'.to_ascii().is_digit());
314+
assert!('0'.to_ascii_opt().unwrap().is_digit());
315+
assert!('9'.to_ascii_opt().unwrap().is_digit());
316+
assert!(!'/'.to_ascii_opt().unwrap().is_digit());
317+
assert!(!':'.to_ascii_opt().unwrap().is_digit());
338318

339-
assert!((0x1fu8).to_ascii().is_control());
340-
assert!(!' '.to_ascii().is_control());
341-
assert!((0x7fu8).to_ascii().is_control());
319+
assert!((0x1fu8).to_ascii_opt().unwrap().is_control());
320+
assert!(!' '.to_ascii_opt().unwrap().is_control());
321+
assert!((0x7fu8).to_ascii_opt().unwrap().is_control());
342322
}
343323

344324
#[test]
345325
fn test_ascii_vec() {
346326
let test = &[40u8, 32u8, 59u8];
347327
let b: &[_] = v2ascii!([40, 32, 59]);
348-
assert_eq!(test.to_ascii(), b);
349-
assert_eq!("( ;".to_ascii(), b);
328+
assert_eq!(test.to_ascii_opt().unwrap(), b);
329+
assert_eq!("( ;".to_ascii_opt().unwrap(), b);
350330
let v = vec![40u8, 32u8, 59u8];
351-
assert_eq!(v.as_slice().to_ascii(), b);
352-
assert_eq!("( ;".to_string().as_slice().to_ascii(), b);
353-
}
354-
355-
#[test]
356-
fn test_owned_ascii_vec() {
357-
assert_eq!(("( ;".to_string()).into_ascii(), vec2ascii![40, 32, 59]);
358-
assert_eq!((vec![40u8, 32u8, 59u8]).into_ascii(), vec2ascii![40, 32, 59]);
331+
assert_eq!(v.as_slice().to_ascii_opt().unwrap(), b);
332+
assert_eq!("( ;".to_string().as_slice().to_ascii_opt().unwrap(), b);
359333
}
360334

361335
#[test]
@@ -381,18 +355,6 @@ mod tests {
381355
assert_eq!(vec2ascii![40, 32, 59].into_bytes(), vec![40u8, 32u8, 59u8]);
382356
}
383357

384-
#[test] #[should_fail]
385-
fn test_ascii_vec_panic_u8_slice() { (&[127u8, 128u8, 255u8]).to_ascii(); }
386-
387-
#[test] #[should_fail]
388-
fn test_ascii_vec_panic_str_slice() { "zoä华".to_ascii(); }
389-
390-
#[test] #[should_fail]
391-
fn test_ascii_panic_u8_slice() { 255u8.to_ascii(); }
392-
393-
#[test] #[should_fail]
394-
fn test_ascii_panic_char_slice() { 'λ'.to_ascii(); }
395-
396358
#[test]
397359
fn test_opt() {
398360
assert_eq!(65u8.to_ascii_opt(), Some(Ascii { chr: 65u8 }));

0 commit comments

Comments
 (0)