diff --git a/tokio-udp/src/recv_dgram.rs b/tokio-udp/src/recv_dgram.rs index 2a9e29606..2f3d5f204 100644 --- a/tokio-udp/src/recv_dgram.rs +++ b/tokio-udp/src/recv_dgram.rs @@ -24,12 +24,61 @@ struct RecvDgramInner { buffer: T } +/// Components of a `RecvDgram` future, returned from `into_parts`. +#[derive(Debug)] +pub struct Parts { + /// The socket + pub socket: UdpSocket, + /// The buffer + pub buffer: T, + + _priv: () +} + impl RecvDgram { /// Create a new future to receive UDP Datagram pub(crate) fn new(socket: UdpSocket, buffer: T) -> RecvDgram { let inner = RecvDgramInner { socket: socket, buffer: buffer }; RecvDgram { state: Some(inner) } } + + /// Consume the `RecvDgram`, returning the socket and buffer. + /// + /// ``` + /// # extern crate tokio_udp; + /// + /// use tokio_udp::UdpSocket; + /// + /// # pub fn main() { + /// + /// let socket = UdpSocket::bind(&([127, 0, 0, 1], 0).into()).unwrap(); + /// let mut buffer = vec![0; 4096]; + /// + /// let future = socket.recv_dgram(buffer); + /// + /// // ... polling `future` ... giving up (e.g. after timeout) + /// + /// let parts = future.into_parts(); + /// + /// let socket = parts.socket; // extract the socket + /// let buffer = parts.buffer; // extract the buffer + /// + /// # } + /// ``` + /// # Panics + /// + /// If called after the future has completed. + pub fn into_parts(mut self) -> Parts { + let state = self.state + .take() + .expect("into_parts called after completion"); + + Parts { + socket: state.socket, + buffer: state.buffer, + _priv: () + } + } } impl Future for RecvDgram