zesterer / flume

A safe and fast multi-producer, multi-consumer channel.
https://crates.io/crates/flume
Apache License 2.0
2.47k stars 85 forks source link

SendError cannot be sent between threads? #116

Open stevefan1999-personal opened 1 year ago

stevefan1999-personal commented 1 year ago
error[E0277]: `SinkItem` cannot be shared between threads safely
   --> tarpc\src\transport\channel.rs:130:45
    |
130 |             .map_err(|e| ChannelError::Send(Box::new(e)))
    |                                             ^^^^^^^^^^^ `SinkItem` cannot be shared between threads safely
    |
    = note: required because it appears within the type `flume::SendError<SinkItem>`
    = note: required for the cast from `flume::SendError<SinkItem>` to the object type `dyn StdError + std::marker::Send + Sync`
help: consider restricting type parameter `SinkItem`
    |
123 | impl<Item, SinkItem: std::marker::Sync> Sink<SinkItem> for Channel<Item, SinkItem> {
    |                    +++++++++++++++++++

This is what ChannelError looks like:

/// Errors that occur in the sending or receiving of messages over a channel.
#[derive(thiserror::Error, Clone, PartialEq, Eq, Debug)]
pub enum ChannelError {
    /// An error occurred sending over the channel.
    #[error("an error occurred sending over the channel")]
    Send(#[from] Box<dyn Error + Send + Sync + 'static>),
    #[error("the channel is closed and cannot accept new items for sending")]
    Closed
}

So it seems like I have to limit the bound of SinkItem to Send + Sync? but far as I know futures::channel::mpsc::Sender does not require that

zesterer commented 1 year ago

That same requirement definitely exists on futures::channel::mpsc::Sender: the channel has the capacity to send data between threads, so that data must be thread-safe. It might be that the requirement is simply on an impl in futures, but it'll still be there.