Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions exercises/13_error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ enum ParsePosNonzeroError {
ParseInt(ParseIntError),
}

impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> Self {
Self::Creation(err)
}

// TODO: Add another error conversion function here.
// fn from_parse_int(???) -> Self { ??? }
}

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);

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

Expand Down
16 changes: 3 additions & 13 deletions solutions/13_error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ enum ParsePosNonzeroError {
ParseInt(ParseIntError),
}

impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> Self {
Self::Creation(err)
}

fn from_parse_int(err: ParseIntError) -> Self {
Self::ParseInt(err)
}
}

// As an alternative solution, implementing the `From` trait allows for the
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
// using the `?` operator, without the need to call `map_err`.
Expand Down Expand Up @@ -59,9 +49,9 @@ impl PositiveNonzeroInteger {
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
// Return an appropriate error instead of panicking when `parse()`
// returns an error.
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parse_int)?;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
let x: i64 = s.parse().map_err(ParsePosNonzeroError::ParseInt)?;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Self::new(x).map_err(ParsePosNonzeroError::Creation)
}
}

Expand Down