Files
base64
byteorder
bytes
cfg_if
crossbeam_deque
crossbeam_epoch
crossbeam_queue
crossbeam_utils
fnv
futures
futures_cpupool
httparse
hyper
iovec
language_tags
lazy_static
libc
lock_api
log
maybe_uninit
memoffset
mime
mio
mio_uds
net2
num_cpus
parking_lot
parking_lot_core
percent_encoding
proc_macro2
quote
rand
relay
rfsapi
safemem
scoped_tls
scopeguard
serde
serde_derive
slab
smallvec
syn
take
time
tokio
tokio_codec
tokio_core
tokio_current_thread
tokio_executor
tokio_fs
tokio_io
tokio_proto
tokio_reactor
tokio_service
tokio_sync
tokio_tcp
tokio_threadpool
tokio_timer
tokio_udp
tokio_uds
try_lock
unicase
unicode_xid
want
  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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![allow(unused)]

use futures::{Poll, Async};
use futures::{StartSend, AsyncSink};
use futures::sink::Sink;
use futures::stream::Stream;
use futures::task::{self, Task};

use std::fmt;

#[must_use = "sinks do nothing unless polled"]
pub struct BufferOne<S: Sink> {
    sink: S,
    buf: Option<S::SinkItem>,
    task: Option<Task>,
}

impl<S: Sink> BufferOne<S> {
    pub fn new(sink: S) -> BufferOne<S> {
        BufferOne {
            sink: sink,
            buf: None,
            task: None,
        }
    }

    /// Get a shared reference to the inner sink.
    pub fn get_ref(&self) -> &S {
        &self.sink
    }

    /// Get a mutable reference to the inner sink.
    pub fn get_mut(&mut self) -> &mut S {
        &mut self.sink
    }

    pub fn poll_ready(&mut self) -> Async<()> {
        if self.buf.is_none() {
            return Async::Ready(());
        }

        self.task = Some(task::park());
        Async::NotReady
    }

    fn try_empty_buffer(&mut self) -> Poll<(), S::SinkError> {
        if let Some(buf) = self.buf.take() {
            if let AsyncSink::NotReady(buf) = try!(self.sink.start_send(buf)) {
                self.buf = Some(buf);
                return Ok(Async::NotReady);
            }

            // Unpark any pending tasks
            self.task.take()
                .map(|t| t.unpark());
        }

        Ok(Async::Ready(()))
    }
}

// Forwarding impl of Stream from the underlying sink
impl<S> Stream for BufferOne<S> where S: Sink + Stream {
    type Item = S::Item;
    type Error = S::Error;

    fn poll(&mut self) -> Poll<Option<S::Item>, S::Error> {
        self.sink.poll()
    }
}

impl<S: Sink> Sink for BufferOne<S> {
    type SinkItem = S::SinkItem;
    type SinkError = S::SinkError;

    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        if !try!(self.try_empty_buffer()).is_ready() {
            return Ok(AsyncSink::NotReady(item));
        }

        if let AsyncSink::NotReady(item) = try!(self.sink.start_send(item)) {
            self.buf = Some(item);
        }

        Ok(AsyncSink::Ready)
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        let mut flushed = try!(self.try_empty_buffer()).is_ready();
        flushed &= try!(self.sink.poll_complete()).is_ready();

        if flushed {
            Ok(Async::Ready(()))
        } else {
            Ok(Async::NotReady)
        }
    }

    fn close(&mut self) -> Poll<(), Self::SinkError> {
        try_ready!(self.poll_complete());
        self.sink.close()
    }
}

impl<S> fmt::Debug for BufferOne<S>
    where S: Sink + fmt::Debug,
          S::SinkItem: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BufferOne")
            .field("sink", &self.sink)
            .field("buf", &self.buf)
            .field("task", &self.task)
            .finish()
    }
}