feat(into_diagnostic): .into_diagnostic() is now generic across any impl fmt::Display instead of expecting a `dyn`

This commit is contained in:
Kat Marchán 2021-08-21 22:55:45 -07:00
parent 5c077d30a4
commit c1da4a0d27
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
1 changed files with 5 additions and 2 deletions

View File

@ -30,14 +30,17 @@ impl Diagnostic for DiagnosticError {
}
}
/**
Convenience trait that adds a `.into_diagnostic()` method that converts a type to a `Result<T, DiagnosticError>`.
*/
pub trait IntoDiagnostic<T, E> {
/// Converts [Result]-like types that return regular errors into a
/// `Result` that returns a [Diagnostic].
fn into_diagnostic(self, code: &(dyn fmt::Display)) -> Result<T, DiagnosticError>;
fn into_diagnostic(self, code: impl fmt::Display) -> Result<T, DiagnosticError>;
}
impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
fn into_diagnostic(self, code: &(dyn fmt::Display)) -> Result<T, DiagnosticError> {
fn into_diagnostic(self, code: impl fmt::Display) -> Result<T, DiagnosticError> {
self.map_err(|e| DiagnosticError {
error: Box::new(e),
code: format!("{}", code),