futures_util/sink/
fanout.rs1use core::fmt::{Debug, Formatter, Result as FmtResult};
2
3use futures_core::{Async, Poll};
4use futures_core::task;
5use futures_sink::{ Sink};
6
7#[derive(Clone)]
12pub struct Fanout<A: Sink, B: Sink> {
13 left: A,
14 right: B
15}
16
17impl<A: Sink, B: Sink> Fanout<A, B> {
18 pub fn into_inner(self) -> (A, B) {
23 (self.left, self.right)
24 }
25}
26
27impl<A: Sink + Debug, B: Sink + Debug> Debug for Fanout<A, B>
28 where A::SinkItem: Debug,
29 B::SinkItem: Debug
30{
31 fn fmt(&self, f: &mut Formatter) -> FmtResult {
32 f.debug_struct("Fanout")
33 .field("left", &self.left)
34 .field("right", &self.right)
35 .finish()
36 }
37}
38
39pub fn new<A: Sink, B: Sink>(left: A, right: B) -> Fanout<A, B> {
40 Fanout {left, right}
41}
42
43impl<A, B> Sink for Fanout<A, B>
44 where A: Sink,
45 A::SinkItem: Clone,
46 B: Sink<SinkItem=A::SinkItem, SinkError=A::SinkError>
47{
48 type SinkItem = A::SinkItem;
49 type SinkError = A::SinkError;
50
51 fn poll_ready(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
52 let left_ready = self.left.poll_ready(cx)?.is_ready();
53 let right_ready = self.right.poll_ready(cx)?.is_ready();
54 let ready = left_ready && right_ready;
55 Ok(if ready {Async::Ready(())} else {Async::Pending})
56 }
57
58 fn start_send(&mut self, item: Self::SinkItem) -> Result<(), Self::SinkError> {
59 self.left.start_send(item.clone())?;
60 self.right.start_send(item)?;
61 Ok(())
62 }
63
64 fn poll_flush(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
65 let left_ready = self.left.poll_flush(cx)?.is_ready();
66 let right_ready = self.right.poll_flush(cx)?.is_ready();
67 let ready = left_ready && right_ready;
68 Ok(if ready {Async::Ready(())} else {Async::Pending})
69 }
70
71 fn poll_close(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
72 let left_ready = self.left.poll_close(cx)?.is_ready();
73 let right_ready = self.right.poll_close(cx)?.is_ready();
74 let ready = left_ready && right_ready;
75 Ok(if ready {Async::Ready(())} else {Async::Pending})
76 }
77}
78
79#[cfg(test)]
80#[cfg(feature = "std")]
81mod tests {
82 use future::FutureExt;
83 use futures_channel::mpsc;
84 use futures_executor::block_on;
85 use sink::SinkExt;
86 use std::iter::Iterator;
87 use std::vec::Vec;
88 use stream::{self, StreamExt};
89
90 #[test]
91 fn it_works() {
92 let src = stream::iter_ok::<_, ()>(0..10);
93 let (sender0, receiver0) = mpsc::channel(1);
94 let (sender1, receiver1) = mpsc::channel(2);
95 let senders = sender0.fanout(sender1).sink_map_err(|_| ());
96 let fwd = src.forward(senders).map(|_| ());
97 let list0 = receiver0.collect::<Vec<_>>().map_err(|_| ());
98 let list1 = receiver1.collect::<Vec<_>>().map_err(|_| ());
99 let lists = fwd.join(list0.join(list1)).map(|(_, lists)| lists);
100 let lists = block_on(lists);
101 let lists = lists.as_ref()
102 .map(|&(ref list0, ref list1)| (&**list0, &**list1));
103 let expected = (0..10).collect::<Vec<_>>();
104 assert_eq!(lists, Ok((&*expected, &*expected)));
105 }
106}