-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This type makes for efficient use of the `?` operator when in a function with multiple return error types that all implement `IntoResponse`. Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
- Loading branch information
1 parent
01a4c88
commit 588086d
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use super::{IntoResponse, Response}; | ||
|
||
/// An [IntoResponse]-based error type | ||
/// | ||
/// All types which implement [IntoResponse] can be converted to an [Error]. | ||
/// This makes it useful as a general error type for functions which combine | ||
/// multiple distinct error types but all of which implement [IntoResponse]. | ||
#[derive(Debug)] | ||
pub struct Error(Response); | ||
|
||
impl<T: IntoResponse> From<T> for Error { | ||
#[inline] | ||
fn from(value: T) -> Self { | ||
Self(value.into_response()) | ||
} | ||
} | ||
|
||
impl<T: IntoResponse> IntoResponse for Result<T, Error> { | ||
#[inline] | ||
fn into_response(self) -> Response { | ||
match self { | ||
Ok(ok) => ok.into_response(), | ||
Err(err) => err.0, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters