Make swf::error::Error implement Send + Sync

This commit is contained in:
Juhan Oskar Hennoste 2024-07-20 23:06:01 +03:00 committed by Aaron Hill
parent 1c8e95a061
commit aa3f046f68
1 changed files with 21 additions and 6 deletions

View File

@ -11,7 +11,7 @@ pub enum Error {
/// This can contain sub-errors with further information (`Error::source`) /// This can contain sub-errors with further information (`Error::source`)
Avm1ParseError { Avm1ParseError {
opcode: u8, opcode: u8,
source: Option<Box<dyn error::Error + 'static>>, source: Option<Box<dyn error::Error + Send + Sync + 'static>>,
}, },
/// Invalid or unknown data was encountered. /// Invalid or unknown data was encountered.
@ -21,7 +21,7 @@ pub enum Error {
/// This can contain sub-errors with further information (`Error::source`) /// This can contain sub-errors with further information (`Error::source`)
SwfParseError { SwfParseError {
tag_code: u16, tag_code: u16,
source: Box<dyn error::Error + 'static>, source: Box<dyn error::Error + Send + Sync + 'static>,
}, },
/// An IO error occurred (probably unexpected EOF). /// An IO error occurred (probably unexpected EOF).
@ -43,7 +43,10 @@ impl Error {
/// Helper method to create `Error::Avm1ParseError`. /// Helper method to create `Error::Avm1ParseError`.
#[inline] #[inline]
pub fn avm1_parse_error_with_source(opcode: u8, source: impl error::Error + 'static) -> Self { pub fn avm1_parse_error_with_source(
opcode: u8,
source: impl error::Error + Send + Sync + 'static,
) -> Self {
Self::Avm1ParseError { Self::Avm1ParseError {
opcode, opcode,
source: Some(Box::new(source)), source: Some(Box::new(source)),
@ -58,7 +61,10 @@ impl Error {
/// Helper method to create `Error::SwfParseError`. /// Helper method to create `Error::SwfParseError`.
#[inline] #[inline]
pub fn swf_parse_error(tag_code: u16, source: impl error::Error + 'static) -> Self { pub fn swf_parse_error(
tag_code: u16,
source: impl error::Error + Send + Sync + 'static,
) -> Self {
Self::SwfParseError { Self::SwfParseError {
tag_code, tag_code,
source: Box::new(source), source: Box::new(source),
@ -99,9 +105,11 @@ impl fmt::Display for Error {
impl error::Error for Error { impl error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use std::ops::Deref;
match self { match self {
Self::Avm1ParseError { source, .. } => source.as_ref().map(|s| s.deref()), Self::Avm1ParseError { source, .. } => match source {
Some(s) => Some(s.as_ref()),
None => None,
},
Self::IoError(e) => e.source(), Self::IoError(e) => e.source(),
Self::InvalidData(_) => None, Self::InvalidData(_) => None,
Self::SwfParseError { source, .. } => Some(source.as_ref()), Self::SwfParseError { source, .. } => Some(source.as_ref()),
@ -115,3 +123,10 @@ impl From<io::Error> for Error {
Self::IoError(error) Self::IoError(error)
} }
} }
#[cfg(test)]
#[test]
fn test_error_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>()
}