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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// TODO: Delete this once alexcrichton/futures-rs#414 lands
//
#![allow(warnings)]

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

/// Adds buffering for a single value to the underlying sink.
///
/// Unlike `Sink::buffer`, this combinator is able to only buffer a single
/// value. As such, the buffer does not require any allocation. `BufferOne` also
/// provides `poll_ready`, which allows checking if sending a value will be
/// accepted.
#[derive(Debug)]
#[must_use = "sinks do nothing unless polled"]
pub struct BufferOne<S: Sink> {
    sink: S,
    buf: Option<Result<S::SinkItem, S::SinkError>>,
}

impl<S: Sink> BufferOne<S> {
    /// Decorate `sink` with `BufferOne`.
    ///
    /// The returned `BufferOne` will have an empty buffer and will be
    /// guaranteed to be able to accept a single value.
    pub fn new(sink: S) -> BufferOne<S> {
        BufferOne {
            sink: sink,
            buf: 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
    }

    /// Consumes the `BufferOne`, returning its underlying sink.
    ///
    /// If a value is currently buffered, it will be lost.
    pub fn into_inner(self) -> S {
        self.sink
    }

    /// Polls the sink to detect whether a call to `start_send` will be accepted
    /// or not.
    ///
    /// If this function returns `Async::Ready`, then a call to `start_send` is
    /// guaranted to return either `Ok(AsyncSink::Ready)` or `Err(_)`.
    ///
    /// If this function returns `Async::NotReady`, then the current task will
    /// become notified once the sink becomes ready to accept a new value.
    pub fn poll_ready(&mut self) -> Async<()> {
        match self.try_empty_buffer() {
            Err(e) => {
                self.buf = Some(Err(e));
                Async::Ready(())
            }
            Ok(ready) => ready,
        }
    }

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

                    try!(self.sink.poll_complete());

                    Ok(Async::NotReady)
                }
            },
            Some(Err(e)) => Err(e),
            None => Ok(Async::Ready(())),
        }
    }
}

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> {
        try!(self.try_empty_buffer());

        if self.buf.is_some() {
            return Ok(AsyncSink::NotReady(item));
        }

        self.buf = Some(Ok(item));

        Ok(AsyncSink::Ready)
    }

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

    fn close(&mut self) -> Poll<(), Self::SinkError> {
        if self.buf.is_some() {
            try_ready!(self.try_empty_buffer());
        }

        assert!(self.buf.is_none());

        self.sink.close()
    }
}