futures_util/io/
close.rs

1use std::io;
2
3use {Poll, Future, Async, task};
4
5use futures_io::AsyncWrite;
6
7/// A future used to fully close an I/O object.
8///
9/// Resolves to the underlying I/O object once the close operation is
10/// complete.
11///
12/// Created by the [`close`] function.
13///
14/// [`close`]: fn.close.html
15#[derive(Debug)]
16pub struct Close<A> {
17    a: Option<A>,
18}
19
20pub fn close<A>(a: A) -> Close<A>
21    where A: AsyncWrite,
22{
23    Close {
24        a: Some(a),
25    }
26}
27
28impl<A> Future for Close<A>
29    where A: AsyncWrite,
30{
31    type Item = A;
32    type Error = io::Error;
33
34    fn poll(&mut self, cx: &mut task::Context) -> Poll<A, io::Error> {
35        try_ready!(self.a.as_mut().unwrap().poll_close(cx));
36        Ok(Async::Ready(self.a.take().unwrap()))
37    }
38}