Right now, the only Future combinator (that I am aware of) for efficiently combining many futures is FuturesUnordered
. However, it has a lot of overhead, due to synchronization and boxing.
I had an idea for a trait that could help: UnsafeFuture
. The ideal definition of UnsafeFuture
would be as follows:
trait UnsafeFuture {
type Output;
unsafe fn unsafe_poll(self: Pin<&mut Self>, waker: &mut Waker) -> Poll<Output>;
unsafe fn unsafe_cancel(self: Pin<&mut Self>, waker: &mut Waker);
}
The key difference is that unsafe_poll
is an unsafe fn
. In particular, one must follow the following rules when calling it:
- Once
unsafe_poll
has been called, one must neither drop theUnsafeFuture
, nor free the underlying memory, untilunsafe_poll
has returnedPoll::Ready
- If one wishes to cancel an
UnsafeFuture
, one must callunsafe_cancel
.
The advantage of such a design is performance. Since an UnsafeFuture
is guaranteed to not be dropped prematurely, it can store buffers inside itself, as well as wakers of sub-futures. The drawback is that there is not much one can safely do with an UnsafeFuture
. The main things one can do are:
-
.await
it. (edit: This returnsimpl UnsafeFuture
, notimpl Future
) - Spawn it on an executor
- Call combinators on it.
What do the other programmers on IRLO think of this idea?