Skip to content

Commit c9260c7

Browse files
author
Without Boats
committed
Require that errors are Send/Sync/'static.
Currently, they are not Sync because they contain a non-Sync trait object. This is a breaking change. The decision to make errors Send but not Sync was made in rust-lang-deprecated#110. We believe that decision was a mistake, because it perpetuates a !Sync restriction on all users even if their errors are, in fact, Sync. Instead, users who need errors that are !Sync should use synchronization when transforming their errors into error-chain errors.
1 parent 48c18a9 commit c9260c7

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "error-chain"
3-
version = "0.11.0" # remember to update html_root_url
3+
version = "0.12.0" # remember to update html_root_url
44
authors = [ "Brian Anderson <banderson@mozilla.com>",
55
"Paul Colomiets <paul@colomiets.name>",
66
"Colin Kiegel <kiegel@gmx.de>",

src/error_chain.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! impl_error_chain_processed {
8787

8888
fn with_chain<E, K>(error: E, kind: K)
8989
-> Self
90-
where E: ::std::error::Error + Send + 'static,
90+
where E: ::std::error::Error + Send + Sync + 'static,
9191
K: Into<Self::ErrorKind>
9292
{
9393
Self::with_chain(error, kind)
@@ -129,14 +129,14 @@ macro_rules! impl_error_chain_processed {
129129
/// Constructs a chained error from another error and a kind, and generates a backtrace.
130130
pub fn with_chain<E, K>(error: E, kind: K)
131131
-> $error_name
132-
where E: ::std::error::Error + Send + 'static,
132+
where E: ::std::error::Error + Send + Sync + 'static,
133133
K: Into<$error_kind_name>
134134
{
135135
$error_name::with_boxed_chain(Box::new(error), kind)
136136
}
137137

138138
/// Construct a chained error from another boxed error and a kind, and generates a backtrace
139-
pub fn with_boxed_chain<K>(error: Box<::std::error::Error + Send>, kind: K)
139+
pub fn with_boxed_chain<K>(error: Box<::std::error::Error + Send + Sync>, kind: K)
140140
-> $error_name
141141
where K: Into<$error_kind_name>
142142
{
@@ -320,7 +320,7 @@ macro_rules! impl_error_chain_processed {
320320
EK: Into<$error_kind_name>;
321321
}
322322

323-
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
323+
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + Sync + 'static {
324324
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
325325
where F: FnOnce() -> EK,
326326
EK: Into<$error_kind_name> {

src/example_generated.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,20 @@ error_chain! {
3636
Custom
3737
}
3838
}
39+
40+
#[cfg(test)]
41+
mod test {
42+
43+
use super::Error;
44+
45+
#[test]
46+
fn generated_error_meets_bounds() {
47+
fn is_sync<T: Sync>() { }
48+
fn is_send<T: Send>() { }
49+
fn is_static<T: 'static>() { }
50+
is_sync::<Error>();
51+
is_send::<Error>();
52+
is_static::<Error>();
53+
assert!(true);
54+
}
55+
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ mod error_chain;
547547
#[macro_use]
548548
mod quick_main;
549549
pub use quick_main::ExitCode;
550-
#[cfg(feature = "example_generated")]
550+
#[cfg(any(test, feature = "example_generated"))]
551551
pub mod example_generated;
552552
mod backtrace;
553553
pub use backtrace::Backtrace;
@@ -591,7 +591,7 @@ pub trait ChainedError: error::Error + Send + 'static {
591591
/// Constructs a chained error from another error and a kind, and generates a backtrace.
592592
fn with_chain<E, K>(error: E, kind: K) -> Self
593593
where Self: Sized,
594-
E: ::std::error::Error + Send + 'static,
594+
E: ::std::error::Error + Send + Sync + 'static,
595595
K: Into<Self::ErrorKind>;
596596

597597
/// Returns the kind of the error.
@@ -655,7 +655,7 @@ impl<'a, T> fmt::Display for DisplayChain<'a, T>
655655
#[doc(hidden)]
656656
pub struct State {
657657
/// Next error in the error chain.
658-
pub next_error: Option<Box<error::Error + Send>>,
658+
pub next_error: Option<Box<error::Error + Send + Sync>>,
659659
/// Backtrace for the current error.
660660
pub backtrace: InternalBacktrace,
661661
}
@@ -671,7 +671,7 @@ impl Default for State {
671671

672672
impl State {
673673
/// Creates a new State type
674-
pub fn new<CE: ChainedError>(e: Box<error::Error + Send>) -> State {
674+
pub fn new<CE: ChainedError>(e: Box<error::Error + Send + Sync>) -> State {
675675
let backtrace = CE::extract_backtrace(&*e)
676676
.unwrap_or_else(InternalBacktrace::new);
677677
State {

0 commit comments

Comments
 (0)