hawkw / thingbuf

in-place allocation-reusing queues for Rust
MIT License
293 stars 24 forks source link

mpsc: add owned refs #62

Open hawkw opened 2 years ago

hawkw commented 2 years ago

Currently, the SendRef and RecvRef types returned by thingbuf::mpsc's senders and receivers contain a borrowed reference to the channel's queue. This means that a Ref from a channel cannot live for the 'static lifetime. However, because the channel's queue is either internally reference counted (in the case of the dynamically allocated MPSCs) or lives in a static variable (in the case of the static MPSCs), it should also be possible to create a reference to a queue slot that is 'static.

We could add an API like tokio::sync::mpsc's Sender::reserve_owned, which clones the internal Arc around the channel's shared data and returns a permit whose lifetime is not bound to a borrow of the Sender handle that produced it.

This could potentially enable some neat stuff, like using a thingbuf mpsc::Receiver of Vec<u8>s as a http_body::Body without having to copy data out of the thingbuf --- in this case, an OwnedRecvRef type could be passed directly into hyper or another HTTP library, and when the library finishes sending the body chunk, that ref is dropped, returning the buffer to the pool.

hawkw commented 2 years ago

This should be pretty straightforward but may require a bit of refactoring internally.