Skip to content

Commit

Permalink
added Error::try_into_host_error (bytecodealliance#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenpo authored Jul 22, 2020
1 parent 0344e55 commit 70b853e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,25 @@ impl Error {
_ => None,
}
}

/// Returns [`HostError`] if this `Error` represents some host error, otherwise returns the original error.
///
/// I.e. if this error have variant [`Host`] or [`Trap`][`Trap`] with [host][`TrapKind::Host`] error.
///
/// [`HostError`]: trait.HostError.html
/// [`Host`]: enum.Error.html#variant.Host
/// [`Trap`]: enum.Error.html#variant.Trap
/// [`TrapKind::Host`]: enum.TrapKind.html#variant.Host
pub fn try_into_host_error(self) -> Result<Box<dyn host::HostError>, Self> {
match self {
Error::Host(host_err) => Ok(host_err),
Error::Trap(trap) => match trap.into_kind() {
TrapKind::Host(host_err) => Ok(host_err),
other => Err(Error::Trap(Trap::new(other))),
},
other => Err(other),
}
}
}

impl Into<String> for Error {
Expand Down

0 comments on commit 70b853e

Please sign in to comment.