aboutsummaryrefslogtreecommitdiffstats
path: root/tests/basic.rs
blob: 8cfebcdc6e88f54add5b2c9abc922f6f00a52d65 (plain) (blame)
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
extern crate component_future;

mod run;

#[test]
fn test_basic_future() {
    struct TwoFutures {
        fut1: Option<
            Box<
                dyn futures::future::Future<Item = u32, Error = String>
                    + Send,
            >,
        >,
        fut2: Option<
            Box<
                dyn futures::future::Future<Item = u32, Error = String>
                    + Send,
            >,
        >,
        val: u32,
    }

    impl TwoFutures {
        fn new<F1, F2>(fut1: F1, fut2: F2) -> Self
        where
            F1: futures::future::Future<Item = u32, Error = String>
                + Send
                + 'static,
            F2: futures::future::Future<Item = u32, Error = String>
                + Send
                + 'static,
        {
            Self {
                fut1: Some(Box::new(fut1)),
                fut2: Some(Box::new(fut2)),
                val: 1,
            }
        }
    }

    #[allow(clippy::type_complexity)]
    impl TwoFutures {
        const POLL_FNS:
            &'static [&'static dyn for<'a> Fn(
                &'a mut Self,
            )
                -> component_future::Poll<
                u32,
                String,
            >] = &[
            &Self::poll_future_1,
            &Self::poll_future_2,
            &Self::poll_return,
        ];

        fn poll_future_1(&mut self) -> component_future::Poll<u32, String> {
            if let Some(fut1) = &mut self.fut1 {
                let val = component_future::try_ready!(fut1.poll());
                self.val += val;
                self.fut1.take();
                Ok(component_future::Async::DidWork)
            } else {
                Ok(component_future::Async::NothingToDo)
            }
        }

        fn poll_future_2(&mut self) -> component_future::Poll<u32, String> {
            if self.fut1.is_some() {
                return Ok(component_future::Async::NothingToDo);
            }

            if let Some(fut2) = &mut self.fut2 {
                let val = component_future::try_ready!(fut2.poll());
                self.val *= val;
                self.fut2.take();
                Ok(component_future::Async::DidWork)
            } else {
                Ok(component_future::Async::NothingToDo)
            }
        }

        fn poll_return(&mut self) -> component_future::Poll<u32, String> {
            if self.fut1.is_some() || self.fut2.is_some() {
                return Ok(component_future::Async::NothingToDo);
            }

            Ok(component_future::Async::Ready(self.val))
        }
    }

    impl futures::future::Future for TwoFutures {
        type Item = u32;
        type Error = String;

        fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
            component_future::poll_future(self, Self::POLL_FNS)
        }
    }

    let cfut =
        TwoFutures::new(futures::future::ok(3), futures::future::ok(5));
    let i = run::future(cfut);
    assert_eq!(i, Ok(20));
}

#[test]
fn test_basic_stream() {
    struct TwoFutures {
        fut1: Option<
            Box<
                dyn futures::future::Future<Item = u32, Error = String>
                    + Send,
            >,
        >,
        fut2: Option<
            Box<
                dyn futures::future::Future<Item = u32, Error = String>
                    + Send,
            >,
        >,
    }

    impl TwoFutures {
        fn new<F1, F2>(fut1: F1, fut2: F2) -> Self
        where
            F1: futures::future::Future<Item = u32, Error = String>
                + Send
                + 'static,
            F2: futures::future::Future<Item = u32, Error = String>
                + Send
                + 'static,
        {
            Self {
                fut1: Some(Box::new(fut1)),
                fut2: Some(Box::new(fut2)),
            }
        }
    }

    impl TwoFutures {
        #[allow(clippy::type_complexity)]
        const POLL_FNS:
            &'static [&'static dyn for<'a> Fn(
                &'a mut Self,
            )
                -> component_future::Poll<
                Option<u32>,
                String,
            >] = &[
            &Self::poll_future_1,
            &Self::poll_future_2,
            &Self::poll_return,
        ];

        fn poll_future_1(
            &mut self,
        ) -> component_future::Poll<Option<u32>, String> {
            if let Some(fut1) = &mut self.fut1 {
                let val = component_future::try_ready!(fut1.poll());
                self.fut1.take();
                Ok(component_future::Async::Ready(Some(val)))
            } else {
                Ok(component_future::Async::NothingToDo)
            }
        }

        fn poll_future_2(
            &mut self,
        ) -> component_future::Poll<Option<u32>, String> {
            if self.fut1.is_some() {
                return Ok(component_future::Async::NothingToDo);
            }

            if let Some(fut2) = &mut self.fut2 {
                let val = component_future::try_ready!(fut2.poll());
                self.fut2.take();
                Ok(component_future::Async::Ready(Some(val)))
            } else {
                Ok(component_future::Async::NothingToDo)
            }
        }

        fn poll_return(
            &mut self,
        ) -> component_future::Poll<Option<u32>, String> {
            if self.fut1.is_some() || self.fut2.is_some() {
                return Ok(component_future::Async::NothingToDo);
            }

            Ok(component_future::Async::Ready(None))
        }
    }

    impl futures::stream::Stream for TwoFutures {
        type Item = u32;
        type Error = String;

        fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
            component_future::poll_stream(self, Self::POLL_FNS)
        }
    }
    let cstream =
        TwoFutures::new(futures::future::ok(3), futures::future::ok(5));
    let is = run::stream(cstream);
    assert_eq!(is, Ok(vec![3, 5]));
}