-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bugen Zhao <[email protected]>
- Loading branch information
Showing
7 changed files
with
173 additions
and
87 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
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
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
This file was deleted.
Oops, something went wrong.
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
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,89 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::backtrace::WithBacktrace; | ||
|
||
/// Workaround for https://github.com/rust-lang/rust/issues/117432. | ||
#[derive(Clone)] | ||
#[repr(transparent)] | ||
pub struct ErrorBox<T, B>(Box<(T, B)>); | ||
|
||
impl<T, B> ErrorBox<T, B> { | ||
pub fn inner_mut(&mut self) -> &mut T { | ||
&mut self.0.as_mut().0 | ||
} | ||
|
||
pub fn into_inner(self) -> T { | ||
(*self.0).0 | ||
} | ||
} | ||
|
||
impl<T, B> std::ops::DerefMut for ErrorBox<T, B> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.inner_mut() | ||
} | ||
} | ||
|
||
#[repr(transparent)] | ||
pub struct ErrorArc<T, B>(Arc<(T, B)>); | ||
|
||
impl<T, B> Clone for ErrorArc<T, B> { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} | ||
|
||
macro_rules! impl_methods { | ||
($ty:ident) => { | ||
impl<T: std::error::Error, B: WithBacktrace> $ty<T, B> { | ||
pub fn new(t: T) -> Self { | ||
let backtrace = B::capture(&t); | ||
Self((t, backtrace).into()) | ||
} | ||
} | ||
|
||
impl<T, B> $ty<T, B> { | ||
fn backtrace(&self) -> &B { | ||
&self.0.as_ref().1 | ||
} | ||
|
||
pub fn inner(&self) -> &T { | ||
&self.0.as_ref().0 | ||
} | ||
} | ||
|
||
impl<T, B> std::ops::Deref for $ty<T, B> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.inner() | ||
} | ||
} | ||
|
||
impl<T: std::fmt::Display, B> std::fmt::Display for $ty<T, B> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner().fmt(f) | ||
} | ||
} | ||
|
||
impl<T: std::fmt::Debug, B> std::fmt::Debug for $ty<T, B> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner().fmt(f) | ||
} | ||
} | ||
|
||
impl<T: std::error::Error, B: WithBacktrace> std::error::Error for $ty<T, B> { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
T::source(self.inner()) | ||
} | ||
|
||
// https://github.com/rust-lang/rust/issues/117432 | ||
fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) { | ||
self.backtrace().provide(request); | ||
T::provide(self.inner(), request); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_methods!(ErrorBox); | ||
impl_methods!(ErrorArc); |
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,23 @@ | ||
#![feature(error_generic_member_access)] | ||
|
||
use std::{error::Error, num::ParseIntError}; | ||
|
||
use thiserror::*; | ||
use thiserror_ext::*; | ||
|
||
#[derive(Error, Debug, Arc, Construct)] | ||
#[thiserror_ext(type = SharedMyError)] | ||
pub enum MyErrorInner { | ||
#[error("foo: {foo}")] | ||
Foo { source: ParseIntError, foo: String }, | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let error = SharedMyError::foo("nope".parse::<i32>().unwrap_err(), "hello".to_owned()); | ||
let error2 = error.clone(); | ||
|
||
// Test source preserved. | ||
let source = error2.source().unwrap(); | ||
assert_eq!(source.to_string(), "invalid digit found in string"); | ||
} |