tokio_io/io/
shutdown.rs

1use std::io;
2
3use futures::{Async, Future, Poll};
4
5use AsyncWrite;
6
7/// A future used to fully shutdown an I/O object.
8///
9/// Resolves to the underlying I/O object once the shutdown operation is
10/// complete.
11///
12/// Created by the [`shutdown`] function.
13///
14/// [`shutdown`]: fn.shutdown.html
15#[derive(Debug)]
16pub struct Shutdown<A> {
17    a: Option<A>,
18}
19
20/// Creates a future which will entirely shutdown an I/O object and then yield
21/// the object itself.
22///
23/// This function will consume the object provided if an error happens, and
24/// otherwise it will repeatedly call `shutdown` until it sees `Ok(())`,
25/// scheduling a retry if `WouldBlock` is seen along the way.
26pub fn shutdown<A>(a: A) -> Shutdown<A>
27where
28    A: AsyncWrite,
29{
30    Shutdown { a: Some(a) }
31}
32
33impl<A> Future for Shutdown<A>
34where
35    A: AsyncWrite,
36{
37    type Item = A;
38    type Error = io::Error;
39
40    fn poll(&mut self) -> Poll<A, io::Error> {
41        try_ready!(self.a.as_mut().unwrap().shutdown());
42        Ok(Async::Ready(self.a.take().unwrap()))
43    }
44}