Skip to content
Merged
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
16 changes: 8 additions & 8 deletions foyer/src/hybrid/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@

impl Error {
/// Create customized error.
pub fn other<E: std::error::Error + Send + Sync + 'static>(err: E) -> Self {
Self::Other(Box::new(err))
pub fn other<E>(e: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,

Check warning on line 33 in foyer/src/hybrid/error.rs

View check run for this annotation

Codecov / codecov/patch

foyer/src/hybrid/error.rs#L31-L33

Added lines #L31 - L33 were not covered by tests
{
Self::Other(e.into())

Check warning on line 35 in foyer/src/hybrid/error.rs

View check run for this annotation

Codecov / codecov/patch

foyer/src/hybrid/error.rs#L35

Added line #L35 was not covered by tests
}

/// Downcast error to a specific type.
///
/// Only `Other` variant can be downcasted.
/// If the error is not `Other`, it will return an error indicating that downcasting is not possible.
pub fn downcast<T>(self) -> std::result::Result<Box<T>, Self>
pub fn downcast<T>(self) -> std::result::Result<T, Self>
where
T: std::error::Error + 'static,
{
match self {
Self::Other(e) => e.downcast::<T>().map_err(|e| {
Self::Other(e) => e.downcast::<T>().map(|e| *e).map_err(|e| {
let e: Box<dyn std::error::Error + Send + Sync + 'static> =
format!("cannot downcast error: {e}").into();
Self::Other(e)
Expand Down Expand Up @@ -92,9 +95,6 @@
"Error or not error, that is a question.".to_string(),
)));
let res = e.downcast::<TestError>().unwrap();
assert_eq!(
res,
Box::new(TestError("Error or not error, that is a question.".to_string(),))
);
assert_eq!(res, TestError("Error or not error, that is a question.".to_string()));
}
}
Loading