1 use {Poll, Async, Future};
2 use sink::Sink;
3 
4 /// Future for the `Sink::flush` combinator, which polls the sink until all data
5 /// has been flushed.
6 #[derive(Debug)]
7 #[must_use = "futures do nothing unless polled"]
8 pub struct Flush<S> {
9     sink: Option<S>,
10 }
11 
new<S: Sink>(sink: S) -> Flush<S>12 pub fn new<S: Sink>(sink: S) -> Flush<S> {
13     Flush { sink: Some(sink) }
14 }
15 
16 impl<S: Sink> Flush<S> {
17     /// Get a shared reference to the inner sink.
get_ref(&self) -> &S18     pub fn get_ref(&self) -> &S {
19         self.sink.as_ref().expect("Attempted `Flush::get_ref` after the flush completed")
20     }
21 
22     /// Get a mutable reference to the inner sink.
get_mut(&mut self) -> &mut S23     pub fn get_mut(&mut self) -> &mut S {
24         self.sink.as_mut().expect("Attempted `Flush::get_mut` after the flush completed")
25     }
26 
27     /// Consume the `Flush` and return the inner sink.
into_inner(self) -> S28     pub fn into_inner(self) -> S {
29         self.sink.expect("Attempted `Flush::into_inner` after the flush completed")
30     }
31 }
32 
33 impl<S: Sink> Future for Flush<S> {
34     type Item = S;
35     type Error = S::SinkError;
36 
poll(&mut self) -> Poll<S, S::SinkError>37     fn poll(&mut self) -> Poll<S, S::SinkError> {
38         let mut sink = self.sink.take().expect("Attempted to poll Flush after it completed");
39         if sink.poll_complete()?.is_ready() {
40             Ok(Async::Ready(sink))
41         } else {
42             self.sink = Some(sink);
43             Ok(Async::NotReady)
44         }
45     }
46 }
47