diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-30 12:03:20 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-30 12:03:20 -0800 |
| commit | ac1a03d7422ba52749e4e513a46c8d2129c2c817 (patch) | |
| tree | cefa26a551d7703c5f8534cc6661432348c93e06 /src/libcoretest/num | |
| parent | 0ba812fbf00e3026b29282e1a72d58ea7959833e (diff) | |
| parent | 0cdde6e5e015ee6f6d9381ab624a312af7c9b069 (diff) | |
| download | rust-ac1a03d7422ba52749e4e513a46c8d2129c2c817.tar.gz rust-ac1a03d7422ba52749e4e513a46c8d2129c2c817.zip | |
rollup merge of #21718: alexcrichton/stabilize-from-str
This commits adds an associated type to the `FromStr` trait representing an error payload for parses which do not succeed. The previous return value, `Option<Self>` did not allow for this form of payload. After the associated type was added, the following attributes were applied: * `FromStr` is now stable * `FromStr::Err` is now stable * `FromStr::from_str` is now stable * `StrExt::parse` is now stable * `FromStr for bool` is now stable * `FromStr for $float` is now stable * `FromStr for $integral` is now stable * Errors returned from stable `FromStr` implementations are stable * Errors implement `Display` and `Error` (both impl blocks being `#[stable]`) Closes #15138
Diffstat (limited to 'src/libcoretest/num')
| -rw-r--r-- | src/libcoretest/num/int_macros.rs | 42 | ||||
| -rw-r--r-- | src/libcoretest/num/mod.rs | 52 |
2 files changed, 47 insertions, 47 deletions
diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs index b98432e26b2..d956cd4816b 100644 --- a/src/libcoretest/num/int_macros.rs +++ b/src/libcoretest/num/int_macros.rs @@ -159,7 +159,7 @@ mod tests { #[test] fn test_from_str() { fn from_str<T: ::std::str::FromStr>(t: &str) -> Option<T> { - ::std::str::FromStr::from_str(t) + ::std::str::FromStr::from_str(t).ok() } assert_eq!(from_str::<$T>("0"), Some(0 as $T)); assert_eq!(from_str::<$T>("3"), Some(3 as $T)); @@ -180,26 +180,26 @@ mod tests { #[test] fn test_from_str_radix() { - assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T)); - assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T)); - assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T)); - assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32)); - assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32)); - assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32)); - assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T)); - assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T)); - - assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T)); - assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T)); - assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T)); - assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32)); - assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32)); - assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32)); - assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T)); - assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T)); - - assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>); - assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>); + assert_eq!(FromStrRadix::from_str_radix("123", 10), Ok(123 as $T)); + assert_eq!(FromStrRadix::from_str_radix("1001", 2), Ok(9 as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 8), Ok(83 as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 16), Ok(291 as i32)); + assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Ok(65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Ok(65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("z", 36), Ok(35 as $T)); + assert_eq!(FromStrRadix::from_str_radix("Z", 36), Ok(35 as $T)); + + assert_eq!(FromStrRadix::from_str_radix("-123", 10), Ok(-123 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Ok(-9 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-123", 8), Ok(-83 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-123", 16), Ok(-291 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Ok(-65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Ok(-65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-z", 36), Ok(-35 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Ok(-35 as $T)); + + assert_eq!(FromStrRadix::from_str_radix("Z", 35).ok(), None::<$T>); + assert_eq!(FromStrRadix::from_str_radix("-9", 2).ok(), None::<$T>); } } diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index e0623bade5c..f93c07eac5f 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -62,64 +62,64 @@ mod test { #[test] fn from_str_issue7588() { - let u : Option<u8> = from_str_radix("1000", 10); + let u : Option<u8> = from_str_radix("1000", 10).ok(); assert_eq!(u, None); - let s : Option<i16> = from_str_radix("80000", 10); + let s : Option<i16> = from_str_radix("80000", 10).ok(); assert_eq!(s, None); - let f : Option<f32> = from_str_radix("10000000000000000000000000000000000000000", 10); + let f : Option<f32> = from_str_radix("10000000000000000000000000000000000000000", 10).ok(); assert_eq!(f, Some(Float::infinity())); - let fe : Option<f32> = from_str_radix("1e40", 10); + let fe : Option<f32> = from_str_radix("1e40", 10).ok(); assert_eq!(fe, Some(Float::infinity())); } #[test] fn test_from_str_radix_float() { - let x1 : Option<f64> = from_str_radix("-123.456", 10); + let x1 : Option<f64> = from_str_radix("-123.456", 10).ok(); assert_eq!(x1, Some(-123.456)); - let x2 : Option<f32> = from_str_radix("123.456", 10); + let x2 : Option<f32> = from_str_radix("123.456", 10).ok(); assert_eq!(x2, Some(123.456)); - let x3 : Option<f32> = from_str_radix("-0.0", 10); + let x3 : Option<f32> = from_str_radix("-0.0", 10).ok(); assert_eq!(x3, Some(-0.0)); - let x4 : Option<f32> = from_str_radix("0.0", 10); + let x4 : Option<f32> = from_str_radix("0.0", 10).ok(); assert_eq!(x4, Some(0.0)); - let x4 : Option<f32> = from_str_radix("1.0", 10); + let x4 : Option<f32> = from_str_radix("1.0", 10).ok(); assert_eq!(x4, Some(1.0)); - let x5 : Option<f32> = from_str_radix("-1.0", 10); + let x5 : Option<f32> = from_str_radix("-1.0", 10).ok(); assert_eq!(x5, Some(-1.0)); } #[test] fn test_int_from_str_overflow() { let mut i8_val: i8 = 127_i8; - assert_eq!("127".parse::<i8>(), Some(i8_val)); - assert_eq!("128".parse::<i8>(), None); + assert_eq!("127".parse::<i8>().ok(), Some(i8_val)); + assert_eq!("128".parse::<i8>().ok(), None); i8_val += 1 as i8; - assert_eq!("-128".parse::<i8>(), Some(i8_val)); - assert_eq!("-129".parse::<i8>(), None); + assert_eq!("-128".parse::<i8>().ok(), Some(i8_val)); + assert_eq!("-129".parse::<i8>().ok(), None); let mut i16_val: i16 = 32_767_i16; - assert_eq!("32767".parse::<i16>(), Some(i16_val)); - assert_eq!("32768".parse::<i16>(), None); + assert_eq!("32767".parse::<i16>().ok(), Some(i16_val)); + assert_eq!("32768".parse::<i16>().ok(), None); i16_val += 1 as i16; - assert_eq!("-32768".parse::<i16>(), Some(i16_val)); - assert_eq!("-32769".parse::<i16>(), None); + assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val)); + assert_eq!("-32769".parse::<i16>().ok(), None); let mut i32_val: i32 = 2_147_483_647_i32; - assert_eq!("2147483647".parse::<i32>(), Some(i32_val)); - assert_eq!("2147483648".parse::<i32>(), None); + assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val)); + assert_eq!("2147483648".parse::<i32>().ok(), None); i32_val += 1 as i32; - assert_eq!("-2147483648".parse::<i32>(), Some(i32_val)); - assert_eq!("-2147483649".parse::<i32>(), None); + assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val)); + assert_eq!("-2147483649".parse::<i32>().ok(), None); let mut i64_val: i64 = 9_223_372_036_854_775_807_i64; - assert_eq!("9223372036854775807".parse::<i64>(), Some(i64_val)); - assert_eq!("9223372036854775808".parse::<i64>(), None); + assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val)); + assert_eq!("9223372036854775808".parse::<i64>().ok(), None); i64_val += 1 as i64; - assert_eq!("-9223372036854775808".parse::<i64>(), Some(i64_val)); - assert_eq!("-9223372036854775809".parse::<i64>(), None); + assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val)); + assert_eq!("-9223372036854775809".parse::<i64>().ok(), None); } } |
