any-mpsc
A wrapper for an mpsc::channel that allows arbitrary types to be passed through. Comes in 2 different flavors:
-
The
any_mpsc::channelfunction may be used to create a basic(AnySender, AnyReceiver).AnySendertakes any value and sends it through the underlying channel with thesendmethod (internally as aBox<dyn Any>).AnyReceivercontains generic versions ofrecv,recv_timeout, andtry_recv. If the generic parameter supplied doesn't correspond with the type theAnySenderpushed through, aAnyRecvError::WrongType(Box<dyn Any>)will be returned containing the value. -
Probably more useful, the
buf_recvdefault feature enables theany_mpsc::buffered_channelfunction. This will return a(AnySender, BufferedReceiver). TheBufferedReceiverworks differently from theAnyReceiverin that if an unmatching generic type is supplied, it will instead return aBufRecvError::WrongType(TypeId). The actual value will be stored in its internal buffer, and the next timerecv,recv_timeout, ortry_recvis called with a generic parameter matching its type, that buffered value will be returned and removed from the buffer. Additional methods for interaction with the channel and buffer exist, see the table below.
| Method | Description |
|---|---|
recv |
Attempts to pop from internal buffer. If buffer is empty, calls mpsc recv |
recv_timeout |
Attempts to pop from internal buffer. If buffer is empty, calls mpsc recv_timeout |
try_recv |
Attempts to pop from internal buffer. If buffer is empty, calls mpsc try_recv |
recv_live |
Calls mpsc recv regardless of whether or not the buffer is empty. Unmatching result types will still be placed in the buffer. |
recv_timeout_live |
Calls mpsc recv_timeout regardless of whether or not the buffer is empty. Unmatching result types will still be placed in the buffer. |
try_recv_live |
Calls mpsc try_recv regardless of whether or not the buffer is empty. Unmatching result types will still be placed in the buffer. |
recv_nobuf |
Equivalent to AnyReceiver::recv (bypasses the buffer entirely) |
recv_timeout_nobuf |
Equivalent to AnyReceiver::recv_timeout_nobuf (bypasses the buffer entirely) |
try_recv_nobuf |
Equivalent to AnyReceiver::try_recv_nobuf (bypasses the buffer entirely) |
recv_buf |
Attempts to pop from the internal buffer. Never attempts to access the internal channel at all. |