Make Error Send + Sync (#236)

This commit is contained in:
Jacob Hoffman-Andrews
2020-11-21 22:11:35 -08:00
committed by GitHub
parent e92bf0b4bb
commit 2136f00bef

View File

@@ -12,7 +12,7 @@ pub struct Error {
kind: ErrorKind, kind: ErrorKind,
message: Option<String>, message: Option<String>,
url: Option<Url>, url: Option<Url>,
source: Option<Box<dyn error::Error>>, source: Option<Box<dyn error::Error + Send + Sync + 'static>>,
response: Option<Box<Response>>, response: Option<Box<Response>>,
} }
@@ -38,7 +38,10 @@ impl Display for Error {
impl error::Error for Error { impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> { 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 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.source = Some(Box::new(e));
self self
} }
@@ -179,3 +182,11 @@ fn io_error() {
err = err.url("http://example.com/".parse().unwrap()); err = err.url("http://example.com/".parse().unwrap());
assert_eq!(err.to_string(), "http://example.com/: Io: oops: too slow"); 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());
}