image-rs / jpeg-decoder

JPEG decoder written in Rust
Apache License 2.0
148 stars 87 forks source link

Make jpeg_decoder::Error be Send + Sync + 'static #119

Closed kevinmehall closed 4 years ago

kevinmehall commented 4 years ago

When using jpeg-decoder in a multi-threaded application, or with a library like anyhow, it is desirable to have the error type meet the Error + Send + Sync + 'static bounds. jpeg_decoder::Error has an Internal(Box<dyn std::Error>) variant that prevented the type from being Send and Sync.

The only use of this variant was for errors related to communicating with the worker thread. SendError contains the object that failed to be sent on the channel, which made it not Sync. SendError doesn't seem to be designed to be propagated with ? -- returning this internal message object is not useful to expose to the caller of jpeg_decoder.

Those channel related errors can only occur if the worker thread panics or otherwise exits prematurely. This would be a library bug, not something caused by user input, so it seems like the kind of fault that should just be a panic rather than a variant of the Error type.

Because Error::Internal is a part of the public API, and crates like image match on it, this commit leaves it in place but unused, just adding the necessary trait bounds. It can be removed in version 0.2.