Skip to content

Commit 98206a5

Browse files
bors-voyager[bot]sgrifjtgeibel
committed
Merge #1516
1516: Refactor how we convert from `T: Error` to `dyn CargoError` r=jtgeibel a=sgrif The way we handled this before with a `Shim` struct breaks any later downcasting we may try to do, and can lead to errors slipping through in a hard to debug way. It's much easier if we just directly implement `CargoError` on those types (and lets us get rid of some trivial impls). This new impl conflicts with `impl<T: CargoError> CargoError for Box<T>`. It's fine to remove that impl. It means that `Box<ConcreteType>` no longer implements `CargoError`, but we wouldn't have boxed the error in the first place unless we needed `dyn CargoError`. Co-authored-by: Sean Griffin <sean@seantheprogrammer.com> Co-authored-by: Justin Geibel <jtgeibel@gmail.com>
2 parents ab0163c + 869bbb5 commit 98206a5

File tree

1 file changed

+8
-43
lines changed

1 file changed

+8
-43
lines changed

src/util/errors.rs

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,6 @@ impl CargoError for Box<dyn CargoError> {
6565
(**self).response()
6666
}
6767
}
68-
impl<T: CargoError> CargoError for Box<T> {
69-
fn description(&self) -> &str {
70-
(**self).description()
71-
}
72-
fn cause(&self) -> Option<&dyn CargoError> {
73-
(**self).cause()
74-
}
75-
fn human(&self) -> bool {
76-
(**self).human()
77-
}
78-
fn response(&self) -> Option<Response> {
79-
(**self).response()
80-
}
81-
}
8268

8369
pub type CargoResult<T> = Result<T, Box<dyn CargoError>>;
8470

@@ -163,42 +149,21 @@ impl<E: CargoError> fmt::Display for ChainedError<E> {
163149
// =============================================================================
164150
// Error impls
165151

166-
impl<E: Any + Error + Send + 'static> From<E> for Box<dyn CargoError> {
167-
fn from(err: E) -> Box<dyn CargoError> {
168-
if let Some(err) = Any::downcast_ref::<DieselError>(&err) {
169-
if let DieselError::NotFound = *err {
170-
return Box::new(NotFound);
171-
}
172-
}
173-
174-
#[derive(Debug)]
175-
struct Shim<E>(E);
176-
impl<E: Error + Send + 'static> CargoError for Shim<E> {
177-
fn description(&self) -> &str {
178-
Error::description(&self.0)
179-
}
180-
}
181-
impl<E: fmt::Display> fmt::Display for Shim<E> {
182-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183-
self.0.fmt(f)
184-
}
185-
}
186-
Box::new(Shim(err))
187-
}
188-
}
189-
190-
impl CargoError for ::serde_json::Error {
152+
impl<E: Error + Send + 'static> CargoError for E {
191153
fn description(&self) -> &str {
192154
Error::description(self)
193155
}
194156
}
195157

196-
impl CargoError for ::std::io::Error {
197-
fn description(&self) -> &str {
198-
Error::description(self)
158+
impl<E: Any + Error + Send + 'static> From<E> for Box<dyn CargoError> {
159+
fn from(err: E) -> Box<dyn CargoError> {
160+
if let Some(DieselError::NotFound) = Any::downcast_ref::<DieselError>(&err) {
161+
Box::new(NotFound)
162+
} else {
163+
Box::new(err)
164+
}
199165
}
200166
}
201-
202167
// =============================================================================
203168
// Concrete errors
204169

0 commit comments

Comments
 (0)