1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use super::socket::UdpSocket;
use std::io;
use std::net::SocketAddr;
use futures::{Async, Future, Poll};
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct RecvDgram<T> {
state: Option<RecvDgramInner<T>>
}
#[derive(Debug)]
struct RecvDgramInner<T> {
socket: UdpSocket,
buffer: T
}
impl<T> RecvDgram<T> {
pub(crate) fn new(socket: UdpSocket, buffer: T) -> RecvDgram<T> {
let inner = RecvDgramInner { socket: socket, buffer: buffer };
RecvDgram { state: Some(inner) }
}
}
impl<T> Future for RecvDgram<T>
where T: AsMut<[u8]>,
{
type Item = (UdpSocket, T, usize, SocketAddr);
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, io::Error> {
let (n, addr) = {
let ref mut inner =
self.state.as_mut().expect("RecvDgram polled after completion");
try_ready!(inner.socket.poll_recv_from(inner.buffer.as_mut()))
};
let inner = self.state.take().unwrap();
Ok(Async::Ready((inner.socket, inner.buffer, n, addr)))
}
}