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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//! Asynchronous sinks //! //! This module contains the `Sink` trait, along with a number of adapter types //! for it. An overview is available in the documentaiton for the trait itself. use {IntoFuture, Poll, StartSend}; use stream::Stream; mod with; // mod with_map; // mod with_filter; // mod with_filter_map; mod flush; mod send; mod send_all; if_std! { mod buffer; pub use self::buffer::Buffer; // TODO: consider expanding this via e.g. FromIterator impl<T> Sink for ::std::vec::Vec<T> { type SinkItem = T; type SinkError = (); // Change this to ! once it stabilizes fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> { self.push(item); Ok(::AsyncSink::Ready) } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { Ok(::Async::Ready(())) } } /// A type alias for `Box<Stream + Send>` pub type BoxSink<T, E> = ::std::boxed::Box<Sink<SinkItem = T, SinkError = E> + ::core::marker::Send>; impl<S: ?Sized + Sink> Sink for ::std::boxed::Box<S> { type SinkItem = S::SinkItem; type SinkError = S::SinkError; fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> { (**self).start_send(item) } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { (**self).poll_complete() } } } pub use self::with::With; pub use self::flush::Flush; pub use self::send::Send; pub use self::send_all::SendAll; /// A `Sink` is a value into which other values can be sent, asynchronously. /// /// Basic examples of sinks include the sending side of: /// /// - Channels /// - Sockets /// - Pipes /// /// In addition to such "primitive" sinks, it's typical to layer additional /// functionality, such as buffering, on top of an existing sink. /// /// Sending to a sink is "asynchronous" in the sense that the value may not be /// sent in its entirety immediately. Instead, values are sent in a two-phase /// way: first by initiating a send, and then by polling for completion. This /// two-phase setup is analogous to buffered writing in synchronous code, where /// writes often succeed immediately, but internally are buffered and are /// *actually* written only upon flushing. /// /// In addition, the `Sink` may be *full*, in which case it is not even possible /// to start the sending process. /// /// As with `Future` and `Stream`, the `Sink` trait is built from a few core /// required methods, and a host of default methods for working in a /// higher-level way. The `Sink::send_all` combinator is of particular /// importance: you can use it to send an entire stream to a sink, which is /// the simplest way to ultimately consume a sink. pub trait Sink { /// The type of value that the sink accepts. type SinkItem; /// The type of value produced by the sink when an error occurs. type SinkError; /// Begin the process of sending a value to the sink. /// /// As the name suggests, this method only *begins* the process of sending /// the item. If the sink employs buffering, the item isn't fully processed /// until the buffer is fully flushed. Since sinks are designed to work with /// asynchronous I/O, the process of actually writing out the data to an /// underlying object takes place asynchronously. **You *must* use /// `poll_complete` in order to drive completion of a send**. In particular, /// `start_send` does not begin the flushing process /// /// # Return value /// /// This method returns `AsyncSink::Ready` if the sink was able to start /// sending `item`. In that case, you *must* ensure that you call /// `poll_complete` to process the sent item to completion. Note, however, /// that several calls to `start_send` can be made prior to calling /// `poll_complete`, which will work on completing all pending items. /// /// The method returns `AsyncSink::NotReady` if the sink was unable to begin /// sending, usually due to being full. The sink must have attempted to /// complete processing any outstanding requests (equivalent to /// `poll_complete`) before yielding this result. The current task will be /// automatically scheduled for notification when the sink may be ready to /// receive new values. /// /// # Errors /// /// If the sink encounters an error other than being temporarily full, it /// uses the `Err` variant to signal that error. In most cases, such errors /// mean that the sink will permanently be unable to receive items. /// /// # Panics /// /// This method may panic in a few situations, depending on the specific /// sink: /// /// - It is called outside of the context of a task. /// - A previous call to `start_send` or `poll_complete` yielded a permanent /// error. fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError>; /// Make progress on all pending requests, and determine whether they have /// completed. /// /// Since sinks are asynchronous, no single method completes all of their /// work in one shot. Instead, you use `poll_complete` to repeatedly drive /// the sink to make progress on requests (such as `start_send`). As with /// `Future::poll`, if the pending requests are not able to complete during /// this call, the current task is automatically scheduled to be woken up /// again once more progress is possible. /// /// # Return value /// /// Returns `Ok(Async::Ready(()))` when no unprocessed requests remain. /// /// Returns `Ok(Async::NotReady)` if there is more work left to do, in which /// case the current task is scheduled to wake up when more progress may be /// possible. /// /// # Errors /// /// Returns `Err` if the sink encounters an error while processing one of /// its pending requests. Due to the buffered nature of requests, it is not /// generally possible to correlate the error with a particular request. As /// with `start_send`, these errors are generally "fatal" for continued use /// of the sink. /// /// # Panics /// /// This method may panic in a few situations, depending on the specific sink: /// /// - It is called outside of the context of a task. /// - A previous call to `start_send` or `poll_complete` yielded a permanent /// error. fn poll_complete(&mut self) -> Poll<(), Self::SinkError>; /// Composes a function *in front of* the sink. /// /// This adapter produces a new sink that passes each value through the /// given function `f` before sending it to `self`. /// /// To process each value, `f` produces a *future*, which is then polled to /// completion before passing its result down to the underlying sink. If the /// future produces an error, that error is returned by the new sink. /// /// Note that this function consumes the given sink, returning a wrapped /// version, much like `Iterator::map`. fn with<U, F, Fut>(self, f: F) -> With<Self, U, F, Fut> where F: FnMut(U) -> Fut, Fut: IntoFuture<Item = Self::SinkItem>, Fut::Error: From<Self::SinkError>, Self: Sized { with::new(self, f) } /* fn with_map<U, F>(self, f: F) -> WithMap<Self, U, F> where F: FnMut(U) -> Self::SinkItem, Self: Sized; fn with_filter<F>(self, f: F) -> WithFilter<Self, F> where F: FnMut(Self::SinkItem) -> bool, Self: Sized; fn with_filter_map<U, F>(self, f: F) -> WithFilterMap<Self, U, F> where F: FnMut(U) -> Option<Self::SinkItem>, Self: Sized; */ /// Adds a fixed-size buffer to the current sink. /// /// The resulting sink will buffer up to `amt` items when the underlying /// sink is unwilling to accept additional items. Calling `poll_complete` on /// the buffered sink will attempt to both empty the buffer and complete /// processing on the underlying sink. /// /// Note that this function consumes the given sink, returning a wrapped /// version, much like `Iterator::map`. #[cfg(feature = "use_std")] fn buffer(self, amt: usize) -> Buffer<Self> where Self: Sized { buffer::new(self, amt) } /// A future that completes when the sink has finished processing all /// pending requests. /// /// The sink itself is returned after flushing is complete; this adapter is /// intended to be used when you want to stop sending to the sink until /// all current requests are processed. fn flush(self) -> Flush<Self> where Self: Sized { flush::new(self) } /// A future that completes after the given item has been fully processed /// into the sink, including flushing. /// /// Note that, **because of the flushing requirement, it is usually better /// to batch together items to send via `send_all`, rather than flushing /// between each item.** /// /// On completion, the sink is returned. fn send(self, item: Self::SinkItem) -> Send<Self> where Self: Sized { send::new(self, item) } /// A future that completes after the given stream has been fully processed /// into the sink, including flushing. /// /// This future will drive the stream to keep producing items until it is /// exhausted, sending each item to the sink. It will complete once both the /// stream is exhausted, and the sink has fully processed and flushed all of /// the items sent to it. /// /// On completion, the sink is returned. fn send_all<S>(self, stream: S) -> SendAll<Self, S> where S: Stream<Item = Self::SinkItem>, Self::SinkError: From<S::Error>, Self: Sized { send_all::new(self, stream) } } impl<'a, S: ?Sized + Sink> Sink for &'a mut S { type SinkItem = S::SinkItem; type SinkError = S::SinkError; fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> { (**self).start_send(item) } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { (**self).poll_complete() } }