Skip to content

Commit db3f332

Browse files
authored
Merge pull request rust-lang#2315 from Lev200501/enum-constructor
Update `13_error_handling/error6`: Remove redundant functions, use enum constructors instead
2 parents ef218cd + c6c6d27 commit db3f332

2 files changed

Lines changed: 4 additions & 23 deletions

File tree

exercises/13_error_handling/errors6.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ enum ParsePosNonzeroError {
1919
ParseInt(ParseIntError),
2020
}
2121

22-
impl ParsePosNonzeroError {
23-
fn from_creation(err: CreationError) -> Self {
24-
Self::Creation(err)
25-
}
26-
27-
// TODO: Add another error conversion function here.
28-
// fn from_parse_int(???) -> Self { ??? }
29-
}
30-
3122
#[derive(PartialEq, Debug)]
3223
struct PositiveNonzeroInteger(u64);
3324

@@ -44,7 +35,7 @@ impl PositiveNonzeroInteger {
4435
// TODO: change this to return an appropriate error instead of panicking
4536
// when `parse()` returns an error.
4637
let x: i64 = s.parse().unwrap();
47-
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
38+
Self::new(x).map_err(ParsePosNonzeroError::Creation)
4839
}
4940
}
5041

solutions/13_error_handling/errors6.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ enum ParsePosNonzeroError {
1919
ParseInt(ParseIntError),
2020
}
2121

22-
impl ParsePosNonzeroError {
23-
fn from_creation(err: CreationError) -> Self {
24-
Self::Creation(err)
25-
}
26-
27-
fn from_parse_int(err: ParseIntError) -> Self {
28-
Self::ParseInt(err)
29-
}
30-
}
31-
3222
// As an alternative solution, implementing the `From` trait allows for the
3323
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
3424
// using the `?` operator, without the need to call `map_err`.
@@ -59,9 +49,9 @@ impl PositiveNonzeroInteger {
5949
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
6050
// Return an appropriate error instead of panicking when `parse()`
6151
// returns an error.
62-
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parse_int)?;
63-
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64-
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
52+
let x: i64 = s.parse().map_err(ParsePosNonzeroError::ParseInt)?;
53+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
Self::new(x).map_err(ParsePosNonzeroError::Creation)
6555
}
6656
}
6757

0 commit comments

Comments
 (0)