diff --git a/foyer/src/hybrid/error.rs b/foyer/src/hybrid/error.rs index e5c0ec94..784642c4 100644 --- a/foyer/src/hybrid/error.rs +++ b/foyer/src/hybrid/error.rs @@ -28,20 +28,23 @@ pub enum Error { impl Error { /// Create customized error. - pub fn other(err: E) -> Self { - Self::Other(Box::new(err)) + pub fn other(e: E) -> Self + where + E: Into>, + { + Self::Other(e.into()) } /// 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(self) -> std::result::Result, Self> + pub fn downcast(self) -> std::result::Result where T: std::error::Error + 'static, { match self { - Self::Other(e) => e.downcast::().map_err(|e| { + Self::Other(e) => e.downcast::().map(|e| *e).map_err(|e| { let e: Box = format!("cannot downcast error: {e}").into(); Self::Other(e) @@ -92,9 +95,6 @@ mod tests { "Error or not error, that is a question.".to_string(), ))); let res = e.downcast::().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())); } }