Use `Arc<anyhow::Error>` instead of `Arc<dyn ...>`.
This is slightly more efficient in two ways:
1) it doesn't require converting to `Error` in as many places (because we can just deref)
2) it is a smaller pointer IIUC.
PiperOrigin-RevId: 460546294
diff --git a/common/arc_anyhow.rs b/common/arc_anyhow.rs
index 1fd9f9c..c5999c2 100644
--- a/common/arc_anyhow.rs
+++ b/common/arc_anyhow.rs
@@ -38,7 +38,7 @@
/// Two errors are equal if they are identical (i.e. they both have a common
/// cloned-from ancestor.)
#[derive(Clone)]
-pub struct Error(Arc<dyn std::error::Error + Send + Sync + 'static>);
+pub struct Error(Arc<anyhow::Error>);
impl Error {
/// Convert this into an `anyhow::Error`.
@@ -60,7 +60,7 @@
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
- std::ptr::eq(&*self.0, &*other.0)
+ std::ptr::eq(&*self, &*other)
}
}
@@ -68,13 +68,13 @@
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
- Display::fmt(&self.clone().into_anyhow(), f)
+ Display::fmt(&**self, f)
}
}
impl Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
- Debug::fmt(&self.clone().into_anyhow(), f)
+ Debug::fmt(&**self, f)
}
}
@@ -89,14 +89,12 @@
T: Into<anyhow::Error>,
{
fn from(e: T) -> Self {
- let e: Box<dyn std::error::Error + Send + Sync + 'static> = e.into().into();
- Error(e.into())
+ Error(Arc::new(e.into()))
}
}
impl std::ops::Deref for Error {
- type Target = dyn std::error::Error + Send + Sync + 'static;
-
+ type Target = anyhow::Error;
fn deref(&self) -> &Self::Target {
&*self.0
}
@@ -109,7 +107,7 @@
/// reason that `anyhow::Error` cannot -- at various places it produces blanket
/// impl conflicts and other metaprogramming difficulties.
#[derive(Clone)]
-struct StdError(Arc<dyn std::error::Error + Send + Sync + 'static>);
+struct StdError(Arc<anyhow::Error>);
impl Display for StdError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {