microasync_util/io/read/
udpsocket.rs

1extern crate std;
2
3use core::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8use std::{
9    io::{self, ErrorKind},
10    net::UdpSocket,
11};
12
13use super::{ReadExactFuture, ReadFuture};
14
15impl<'a> Future for ReadFuture<'a, UdpSocket> {
16    type Output = Result<usize, io::Error>;
17
18    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
19        let me = self.get_mut();
20        me.0.set_nonblocking(true)
21            .expect("unable to set nonblocking-mode.");
22        let r = me.0.recv(me.1);
23        me.0.set_nonblocking(false)
24            .expect("unable to clear nonblocking-mode.");
25
26        match r {
27            Ok(x) => Poll::Ready(Ok(x)),
28            Err(e) if e.kind() == ErrorKind::WouldBlock => Poll::Pending,
29            Err(e) => Poll::Ready(Err(e)),
30        }
31    }
32}
33
34impl<'a> Future for ReadExactFuture<'a, UdpSocket> {
35    type Output = Result<(), io::Error>;
36
37    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
38        let me = self.get_mut();
39        me.0.set_nonblocking(true)
40            .expect("unable to set nonblocking-mode.");
41        let mut n = 0;
42        while n != me.1.len() {
43            n += match me.0.recv(&mut (me.1)[n..]) {
44                Ok(x) => x,
45                Err(e) => return Poll::Ready(Err(e)),
46            }
47        }
48        me.0.set_nonblocking(false)
49            .expect("unable to clear nonblocking-mode.");
50
51        Poll::Ready(Ok(()))
52    }
53}