diff options
author | Vika <vika@fireburn.ru> | 2021-12-02 00:52:24 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-12-02 00:57:23 +0300 |
commit | 99d6a56336c8c6e5a3e5fb9f132bb15157fc1525 (patch) | |
tree | b1264f95e07100847ee426513ecc84ca4d50ad30 | |
parent | dd0bd6f9b7cb556c8eab12569b6096a5d8afa3e0 (diff) | |
download | kittybox-99d6a56336c8c6e5a3e5fb9f132bb15157fc1525.tar.zst |
Get rid of the unsafe code
Thanks to @Kloenk I was able to get rid of the unsafety and tell the compiler how to properly check what I needed for the StorageError to be declared thread-safe.
-rw-r--r-- | src/database/mod.rs | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs index 4e74c8f..339439d 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -46,13 +46,10 @@ pub enum ErrorKind { #[derive(Debug)] pub struct StorageError { msg: String, - source: Option<Box<dyn std::error::Error>>, + source: Option<Box<dyn std::error::Error + Send + Sync>>, kind: ErrorKind, } -// XXX figure out what makes this un-sendable by default -// and if it's sound to mark StorageError as Send & Sync -unsafe impl Send for StorageError {} -unsafe impl Sync for StorageError {} + impl From<StorageError> for tide::Response { fn from(err: StorageError) -> Self { tide::Response::builder(match err.kind() { @@ -73,7 +70,7 @@ impl From<StorageError> for tide::Response { } impl std::error::Error for StorageError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.source.as_ref().map(|e| e.as_ref()) + self.source.as_ref().map(|e| e.as_ref() as &dyn std::error::Error) } } impl From<serde_json::Error> for StorageError { @@ -118,7 +115,7 @@ impl StorageError { } } /// Create a StorageError using another arbitrary Error as a source. - fn with_source(kind: ErrorKind, msg: &str, source: Box<dyn std::error::Error>) -> Self { + fn with_source(kind: ErrorKind, msg: &str, source: Box<dyn std::error::Error + Send + Sync>) -> Self { Self { msg: msg.to_string(), source: Some(source), |