-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make ParseIntError, ParseFloatError and ParseBoolError reusable (public) #1143
Comments
bump, since making it public would allow to do a matching on the Error kind which would really help on situations like this. |
I was hoping to match on whether there was an overflow, so I can handle the numbers differently. I was writing a comparator like this, which might not be the best way, but felt clear to me. Untested, as it doesn't compile. use std::cmp::Ordering;
pub fn compare_big_numbers(left: &str, right: &str) -> Ordering {
match left.parse::<u64>() {
Ok(left) => match right.parse::<u64>() {
Ok(right) => {
// both parsed; can use normal comparator (common case)
return left.cmp(&right)
},
Err(e) if e.kind() == ParseIntError::Overflow => {
// left fits, but right doesn't -> it must be bigger
return Ordering::Greater;
},
_ => panic!("unexpected parse error"),
},
Err(e) if e.kind() == ParseIntError::Overflow => {
// left overflowed, let's see if right does too
match right.parse::<u64>() {
Ok(_) => {
// it didn't, we're bigger
return Ordering::Less;
},
Err(e) if e.kind == Overflow => {
... |
Vote for this. Is there any reason that the interior of |
Any news on this? I just stumbled over this when writing a doctest: assert_eq!(num, Err(StrParseError::<i8, i8>::InvalidRealPart(ParseIntError{ kind: IntErrorKind::InvalidDigit }))); I worked around this via a |
You could write this: use core::num::{IntErrorKind, ParseIntError};
enum StrParseError {
InvalidRealPart(ParseIntError),
Foo,
}
fn main() {
let num = i32::from_str_radix("a12", 10).map_err(|e| StrParseError::InvalidRealPart(e));
let e = num.map_err(|e| match e {
StrParseError::InvalidRealPart(e) => e.kind() == &IntErrorKind::InvalidDigit,
_ => false,
});
assert_eq!(Err(true), e);
} However it would probably be more simple to have |
I'd like to reuse
ParseIntError
to write a wrapper for theas
keyword to make sure the new value matches the old one. It could look like this:Furthermore,
ParseIntError
andParseFloatError
would be useful if one decides to create his own integer or float data type.To make this work,
ParseIntError
'skind
field,ParseFloatError
'skind
field and both enumsIntErrorKind
andFloatErrorKind
have to be public.Whether this should affect
ParseBoolError
should be discussed.The text was updated successfully, but these errors were encountered: