From 2136f00bef96918343c5b388a7edfc58162c437e Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Sat, 21 Nov 2020 22:11:35 -0800 Subject: [PATCH] Make Error Send + Sync (#236) --- src/error.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 5897834..86ffdff 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,7 +12,7 @@ pub struct Error { kind: ErrorKind, message: Option, url: Option, - source: Option>, + source: Option>, response: Option>, } @@ -38,7 +38,10 @@ impl Display for Error { impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { - self.source.as_deref() + match &self.source { + Some(s) => Some(s.as_ref()), + None => None, + } } } @@ -58,7 +61,7 @@ impl Error { self } - pub(crate) fn src(mut self, e: impl error::Error + 'static) -> Self { + pub(crate) fn src(mut self, e: impl error::Error + Send + Sync + 'static) -> Self { self.source = Some(Box::new(e)); self } @@ -179,3 +182,11 @@ fn io_error() { err = err.url("http://example.com/".parse().unwrap()); assert_eq!(err.to_string(), "http://example.com/: Io: oops: too slow"); } + +#[test] +fn error_is_send_and_sync() { + fn takes_send(_: impl Send) {} + fn takes_sync(_: impl Sync) {} + takes_send(crate::error::ErrorKind::BadUrl.new()); + takes_sync(crate::error::ErrorKind::BadUrl.new()); +}