rustasync / runtime

Empowering everyone to build asynchronous software
https://docs.rs/runtime
Apache License 2.0
862 stars 28 forks source link

Access to low-level poll functions on UdpSocket #100

Closed Nemo157 closed 4 years ago

Nemo157 commented 5 years ago

I want to wrap a UdpSocket into a type that provides higher level async APIs on top of it (specifically converting it into an application specific impl Stream + Sink). That would be much easier to do if it supported polling based APIs like AsyncDatagram rather than only providing functions that return futures.

Nemo157 commented 5 years ago

Workaround for now is that it appears that recreating and polling the futures every time works (although I haven't really stress tested it)

let mut recv_from_fut = socket.recv_from(&mut buf);
let recv_from_fut = Pin::new(&mut recv_from_fut);
let (length, peer) = ready!(recv_from_fut.poll(cx))?;

(this also allows simultaneously receiving and sending from the same socket, which should be supported but is blocked by recv_from and send_to both taking exclusive borrows).