handy_async 0.2.13

A handy library for describing asynchronous code declaratively
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
use futures::{self, Poll, Async, Future, Stream};

use pattern::{Pattern, Branch, Iter};
use pattern::combinators::{Map, AndThen, Then, OrElse, Or, Chain};
use pattern::combinators::{IterFold, Expect, UnexpectedValue};
use error::AsyncError;
use super::Matcher;

/// The `AsyncMatch` trait allows for asyncronous matching
/// between a pattern `Self` and a matcher `M`.
///
/// Normally, users will not be aware of this trait and will use
/// more specific interfaces like [`ReadFrom`](../pattern/trait.ReadFrom.html) and
/// [`WriteInto`](../pattern/trait.WriteInto.html).
///
/// For details on how to define your own matcher,
/// see the documentation of [`Matcher`](./trait.Matcher.html) trait.
pub trait AsyncMatch<M: Matcher>: Pattern {
    /// The future type which will produce a value `Self::Value` by
    /// matching this pattern and a matcher `M`.
    type Future: Future<Item = (M, Self::Value), Error = AsyncError<M, M::Error>>;

    /// Creates a future which will produce a `Self::Value` by
    /// matching this pattern and the `matcher`.
    fn async_match(self, matcher: M) -> Self::Future;

    /// Consumes this pattern and the `matcher`,
    /// returning a stream which will produce a sequence of matched values.
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate futures;
    /// # extern crate handy_async;
    /// use futures::{Future, Stream};
    /// use handy_async::pattern::read::U8;
    /// use handy_async::matcher::AsyncMatch;
    /// use handy_async::io::PatternReader;
    ///
    /// # fn main() {
    /// let matcher = PatternReader::new(&b"hello"[..]);
    /// let values = U8.into_stream(matcher).take(3).collect().wait().unwrap();
    /// assert_eq!(values, b"hel");
    /// # }
    /// ```
    fn into_stream(self, matcher: M) -> MatchStream<M, Self>
    where
        Self: Clone,
    {
        let p = self.clone();
        MatchStream(self, p.async_match(matcher))
    }
}

/// Stream to produce a sequence of matched values.
///
/// This is created by calling `AsyncMatch::into_stream` method.
pub struct MatchStream<M: Matcher, P>(P, P::Future)
where
    P: AsyncMatch<M>;
impl<M: Matcher, P> Stream for MatchStream<M, P>
where
    P: AsyncMatch<M> + Clone,
{
    type Item = P::Value;
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        if let Async::Ready((m, v)) = self.1.poll()? {
            let p = self.0.clone();
            self.1 = p.async_match(m);
            Ok(Async::Ready(Some(v)))
        } else {
            Ok(Async::NotReady)
        }
    }
}

/// Future to do pattern matching of
/// [`Map`](../../pattern/combinators/struct.Map.html) pattern.
pub struct MatchMap<P, F>(Option<(P, F)>);
impl<M, P, F, T, U> Future for MatchMap<P, F>
where
    P: Future<Item = (M, T)>,
    F: FnOnce(T) -> U,
{
    type Item = (M, U);
    type Error = P::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let (mut p, f) = self.0.take().expect("Cannot poll MatchMap twice");
        if let Async::Ready((matcher, v)) = p.poll()? {
            Ok(Async::Ready((matcher, f(v))))
        } else {
            self.0 = Some((p, f));
            Ok(Async::NotReady)
        }
    }
}
impl<M: Matcher, P, F, T> AsyncMatch<M> for Map<P, F>
where
    F: FnOnce(P::Value) -> T,
    P: AsyncMatch<M>,
{
    type Future = MatchMap<<P as AsyncMatch<M>>::Future, F>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p, f) = self.unwrap();
        MatchMap(Some((p.async_match(matcher), f)))
    }
}

/// Future to do pattern matching of
/// [`AndThen`](../../pattern/combinators/struct.AndThen.html) pattern.
pub struct MatchAndThen<M, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
where
    M: Matcher,
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(P0::Value) -> P1;
impl<M: Matcher, P0, P1, F> Future for MatchAndThen<M, P0, P1, F>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(P0::Value) -> P1,
{
    type Item = (M, P1::Value);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut p0, f)) => {
                if let Async::Ready((m, v0)) = p0.poll()? {
                    let p1 = f(v0).async_match(m);
                    self.0 = Phase::B(p1);
                    self.poll()
                } else {
                    self.0 = Phase::A((p0, f));
                    Ok(Async::NotReady)
                }
            }
            Phase::B(mut p1) => {
                if let Async::Ready((m, v1)) = p1.poll()? {
                    Ok(Async::Ready((m, v1)))
                } else {
                    self.0 = Phase::B(p1);
                    Ok(Async::NotReady)
                }
            }
            _ => panic!("Cannot poll MatchAndThen twice"),
        }
    }
}
impl<M: Matcher, P0, P1, F> AsyncMatch<M> for AndThen<P0, F>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(P0::Value) -> P1,
{
    type Future = MatchAndThen<M, P0, P1, F>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p, f) = self.unwrap();
        MatchAndThen(Phase::A((p.async_match(matcher), f)))
    }
}

/// Future to do pattern matching of
/// [Then](../../pattern/combinators/struct.Then.html) pattern.
pub struct MatchThen<M: Matcher, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(Result<P0::Value, M::Error>) -> P1;
impl<M: Matcher, P0, P1, F> Future for MatchThen<M, P0, P1, F>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(Result<P0::Value, M::Error>)
           -> P1,
{
    type Item = (M, P1::Value);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut p0, f)) => {
                match p0.poll() {
                    Err(e) => {
                        let (m, e) = e.unwrap();
                        let p1 = f(Err(e)).async_match(m);
                        self.0 = Phase::B(p1);
                        self.poll()
                    }
                    Ok(Async::Ready((m, v0))) => {
                        let p1 = f(Ok(v0)).async_match(m);
                        self.0 = Phase::B(p1);
                        self.poll()
                    }
                    Ok(Async::NotReady) => {
                        self.0 = Phase::A((p0, f));
                        Ok(Async::NotReady)
                    }
                }
            }
            Phase::B(mut p1) => {
                if let Async::Ready((m, v1)) = p1.poll()? {
                    Ok(Async::Ready((m, v1)))
                } else {
                    self.0 = Phase::B(p1);
                    Ok(Async::NotReady)
                }
            }
            _ => panic!("Cannot poll MatchThen twice"),
        }
    }
}
impl<M: Matcher, P0, P1, F> AsyncMatch<M> for Then<P0, F, M::Error>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(Result<
        P0::Value,
        M::Error,
    >)
           -> P1,
{
    type Future = MatchThen<M, P0, P1, F>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p, f) = self.unwrap();
        MatchThen(Phase::A((p.async_match(matcher), f)))
    }
}

/// Future to do pattern matching of
/// [`OrElse`](../../pattern/combinators/struct.OrElse.html) pattern.
pub struct MatchOrElse<M: Matcher, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
    F: FnOnce(M::Error) -> P1;
impl<M: Matcher, P0, P1, F> Future for MatchOrElse<M, P0, P1, F>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M, Value = P0::Value>,
    F: FnOnce(M::Error) -> P1,
{
    type Item = (M, P1::Value);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut p0, f)) => {
                match p0.poll() {
                    Err(e) => {
                        let (m, e) = e.unwrap();
                        let p1 = f(e).async_match(m);
                        self.0 = Phase::B(p1);
                        self.poll()
                    }
                    Ok(Async::Ready((m, v0))) => Ok(Async::Ready((m, v0))),
                    Ok(Async::NotReady) => {
                        self.0 = Phase::A((p0, f));
                        Ok(Async::NotReady)
                    }
                }
            }
            Phase::B(mut p1) => {
                if let Async::Ready((m, v1)) = p1.poll()? {
                    Ok(Async::Ready((m, v1)))
                } else {
                    self.0 = Phase::B(p1);
                    Ok(Async::NotReady)
                }
            }
            _ => panic!("Cannot poll MatchOrElse twice"),
        }
    }
}
impl<M: Matcher, P0, P1, F> AsyncMatch<M> for OrElse<P0, F, M::Error>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<
        M,
        Value = P0::Value,
    >,
    F: FnOnce(M::Error) -> P1,
{
    type Future = MatchOrElse<M, P0, P1, F>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p, f) = self.unwrap();
        MatchOrElse(Phase::A((p.async_match(matcher), f)))
    }
}

/// Future to do pattern matching of
/// [Or](../../pattern/combinators/struct.Or.html) pattern.
pub struct MatchOr<M: Matcher, P0, P1>(Phase<(P0::Future, P1), P1::Future>)
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>;
impl<M: Matcher, P0, P1> Future for MatchOr<M, P0, P1>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M, Value = P0::Value>,
{
    type Item = (M, P1::Value);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut p0, p1)) => {
                match p0.poll() {
                    Err(e) => {
                        let (m, _) = e.unwrap();
                        let p1 = p1.async_match(m);
                        self.0 = Phase::B(p1);
                        self.poll()
                    }
                    Ok(Async::Ready((m, v0))) => Ok(Async::Ready((m, v0))),
                    Ok(Async::NotReady) => {
                        self.0 = Phase::A((p0, p1));
                        Ok(Async::NotReady)
                    }
                }
            }
            Phase::B(mut p1) => {
                if let Async::Ready((m, v1)) = p1.poll()? {
                    Ok(Async::Ready((m, v1)))
                } else {
                    self.0 = Phase::B(p1);
                    Ok(Async::NotReady)
                }
            }
            _ => panic!("Cannot poll MatchOr twice"),
        }
    }
}
impl<M: Matcher, P0, P1> AsyncMatch<M> for Or<P0, P1>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M, Value = P0::Value>,
{
    type Future = MatchOr<M, P0, P1>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p0, p1) = self.unwrap();
        MatchOr(Phase::A((p0.async_match(matcher), p1)))
    }
}

/// Future to do pattern matching of
/// [Chain](../../pattern/combinators/struct.Chain.html) pattern.
pub struct MatchChain<M: Matcher, P0, P1>(ChainPhase<P0::Future, P1, P1::Future, P0::Value>)
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>;
impl<M: Matcher, P0, P1> Future for MatchChain<M, P0, P1>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
{
    type Item = (M, (P0::Value, P1::Value));
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut p0, p1)) => {
                match p0.poll() {
                    Err(e) => Err(e),
                    Ok(Async::Ready((m, v0))) => {
                        self.0 = Phase::B((p1.async_match(m), v0));
                        self.poll()
                    }
                    Ok(Async::NotReady) => {
                        self.0 = Phase::A((p0, p1));
                        Ok(Async::NotReady)
                    }
                }
            }
            Phase::B((mut p1, v0)) => {
                if let Async::Ready((m, v1)) = p1.poll()? {
                    Ok(Async::Ready((m, (v0, v1))))
                } else {
                    self.0 = Phase::B((p1, v0));
                    Ok(Async::NotReady)
                }
            }
            _ => panic!("Cannot poll MatchChain twice"),
        }
    }
}
impl<M: Matcher, P0, P1> AsyncMatch<M> for Chain<P0, P1>
where
    P0: AsyncMatch<M>,
    P1: AsyncMatch<M>,
{
    type Future = MatchChain<M, P0, P1>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (p0, p1) = self.unwrap();
        MatchChain(Phase::A((p0.async_match(matcher), p1)))
    }
}
type ChainPhase<F0, Next, F1, Value> = Phase<(F0, Next), (F1, Value)>;

/// Future to do pattern matching of
/// [Option](../../pattern/type.Option.html) pattern.
pub struct MatchOption<M: Matcher, P>(Option<Result<P::Future, M>>)
where
    P: AsyncMatch<M>;
impl<M: Matcher, P> Future for MatchOption<M, P>
where
    P: AsyncMatch<M>,
{
    type Item = (M, Option<P::Value>);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let inner = self.0.take().expect("Cannot poll MatchOption twice");
        match inner {
            Ok(mut f) => {
                if let Async::Ready((m, v)) = f.poll()? {
                    Ok(Async::Ready((m, Some(v))))
                } else {
                    self.0 = Some(Ok(f));
                    Ok(Async::NotReady)
                }
            }
            Err(m) => Ok(Async::Ready((m, None))),
        }
    }
}
impl<M: Matcher, P> AsyncMatch<M> for Option<P>
where
    P: AsyncMatch<M>,
{
    type Future = MatchOption<M, P>;
    fn async_match(self, matcher: M) -> Self::Future {
        if let Some(p) = self {
            MatchOption(Some(Ok(p.async_match(matcher))))
        } else {
            MatchOption(Some(Err(matcher)))
        }
    }
}

impl<M: Matcher, T> AsyncMatch<M> for Result<T, M::Error> {
    type Future = futures::Done<(M, T), AsyncError<M, M::Error>>;
    fn async_match(self, matcher: M) -> Self::Future {
        match self {
            Ok(v) => futures::done(Ok((matcher, v))),
            Err(e) => futures::done(Err(AsyncError::new(matcher, e))),
        }
    }
}

/// Future to do pattern matching of
/// [Branch](../../pattern/struct.Branch.html) pattern.
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
pub struct MatchBranch<M, A, B, C, D, E, F, G, H>
where
    M: Matcher,
    A: AsyncMatch<M>,
    B: AsyncMatch<M, Value = A::Value>,
    C: AsyncMatch<M, Value = A::Value>,
    D: AsyncMatch<M, Value = A::Value>,
    E: AsyncMatch<M, Value = A::Value>,
    F: AsyncMatch<M, Value = A::Value>,
    G: AsyncMatch<M, Value = A::Value>,
    H: AsyncMatch<M, Value = A::Value>,
{
    future: Branch<
        A::Future,
        B::Future,
        C::Future,
        D::Future,
        E::Future,
        F::Future,
        G::Future,
        H::Future,
    >,
}
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
impl<M, A, B, C, D, E, F, G, H> Future for MatchBranch<M, A, B, C, D, E, F, G, H>
    where M: Matcher,
          A: AsyncMatch<M>,
          B: AsyncMatch<M, Value = A::Value>,
          C: AsyncMatch<M, Value = A::Value>,
          D: AsyncMatch<M, Value = A::Value>,
          E: AsyncMatch<M, Value = A::Value>,
          F: AsyncMatch<M, Value = A::Value>,
          G: AsyncMatch<M, Value = A::Value>,
          H: AsyncMatch<M, Value = A::Value>
{
    type Item = <Branch<A::Future,
           B::Future,
           C::Future,
           D::Future,
           E::Future,
           F::Future,
           G::Future,
           H::Future> as Future>::Item;
    type Error = <Branch<A::Future,
           B::Future,
           C::Future,
           D::Future,
           E::Future,
           F::Future,
           G::Future,
           H::Future> as Future>::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.future.poll()
    }
}
impl<M, A, B, C, D, E, F, G, H> AsyncMatch<M> for Branch<A, B, C, D, E, F, G, H>
    where M: Matcher,
          A: AsyncMatch<M>,
          B: AsyncMatch<M, Value = A::Value>,
          C: AsyncMatch<M, Value = A::Value>,
          D: AsyncMatch<M, Value = A::Value>,
          E: AsyncMatch<M, Value = A::Value>,
          F: AsyncMatch<M, Value = A::Value>,
          G: AsyncMatch<M, Value = A::Value>,
          H: AsyncMatch<M, Value = A::Value>
{
    type Future = MatchBranch<M, A, B, C, D, E, F, G, H>;
    fn async_match(self, matcher: M) -> Self::Future {
        let future = match self {
            Branch::A(p) => Branch::A(p.async_match(matcher)),
            Branch::B(p) => Branch::B(p.async_match(matcher)),
            Branch::C(p) => Branch::C(p.async_match(matcher)),
            Branch::D(p) => Branch::D(p.async_match(matcher)),
            Branch::E(p) => Branch::E(p.async_match(matcher)),
            Branch::F(p) => Branch::F(p.async_match(matcher)),
            Branch::G(p) => Branch::G(p.async_match(matcher)),
            Branch::H(p) => Branch::H(p.async_match(matcher)),
        };
        MatchBranch { future: future }
    }
}

/// Future to do pattern matching of
/// [`IterFold`](../../pattern/combinators/struct.IterFold.html) pattern.
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
pub struct MatchIterFold<M: Matcher, I, F, T>
where
    I: Iterator,
    I::Item: AsyncMatch<M>,
{
    phase: Phase<(<I::Item as AsyncMatch<M>>::Future, I, T, F), (M, T)>,
}
impl<M: Matcher, I, F, T> Future for MatchIterFold<M, I, F, T>
    where I: Iterator,
          I::Item: AsyncMatch<M>,
          F: Fn(T, <I::Item as Pattern>::Value) -> T
{
    type Item = (M, T);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.phase.take() {
            Phase::A((mut f, mut iter, acc, fold)) => {
                if let Async::Ready((m, v)) = f.poll()? {
                    let acc = fold(acc, v);
                    if let Some(p) = iter.next() {
                        self.phase = Phase::A((p.async_match(m), iter, acc, fold));
                        self.poll()
                    } else {
                        Ok(Async::Ready((m, acc)))
                    }
                } else {
                    self.phase = Phase::A((f, iter, acc, fold));
                    Ok(Async::NotReady)
                }
            }
            Phase::B((m, v)) => Ok(Async::Ready((m, v))),
            _ => panic!("Cannot poll MatchIterFold twice"),
        }
    }
}
impl<M: Matcher, I, F, T> AsyncMatch<M> for IterFold<I, F, T>
    where I: Iterator,
          I::Item: AsyncMatch<M>,
          F: Fn(T, <I::Item as Pattern>::Value) -> T
{
    type Future = MatchIterFold<M, I, F, T>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (mut iter, fold, acc) = self.unwrap();
        if let Some(p) = iter.next() {
            MatchIterFold{ phase: Phase::A((p.async_match(matcher), iter, acc, fold)) }
        } else {
            MatchIterFold{ phase: Phase::B((matcher, acc)) }
        }
    }
}

/// Future to do pattern matching of
/// [Iter](../../pattern/struct.Iter.html) pattern.
pub struct MatchIter<M: Matcher, I>(Phase<(<I::Item as AsyncMatch<M>>::Future, I), M>)
where
    I: Iterator,
    I::Item: AsyncMatch<M>;
impl<M: Matcher, I> Future for MatchIter<M, I>
where
    I: Iterator,
    I::Item: AsyncMatch<M>,
{
    type Item = (M, ());
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0.take() {
            Phase::A((mut f, mut iter)) => {
                if let Async::Ready((m, _)) = f.poll()? {
                    if let Some(p) = iter.next() {
                        self.0 = Phase::A((p.async_match(m), iter));
                        self.poll()
                    } else {
                        Ok(Async::Ready((m, ())))
                    }
                } else {
                    self.0 = Phase::A((f, iter));
                    Ok(Async::NotReady)
                }
            }
            Phase::B(m) => Ok(Async::Ready((m, ()))),
            _ => panic!("Cannot poll MatchIter twice"),
        }
    }
}
impl<M: Matcher, I> AsyncMatch<M> for Iter<I>
where
    I: Iterator,
    I::Item: AsyncMatch<M>,
{
    type Future = MatchIter<M, I>;
    fn async_match(self, matcher: M) -> Self::Future {
        let mut iter = self.0;
        if let Some(p) = iter.next() {
            MatchIter(Phase::A((p.async_match(matcher), iter)))
        } else {
            MatchIter(Phase::B(matcher))
        }
    }
}

/// Future to do pattern matching of
/// [Expect](../../pattern/struct.Expect.html) pattern.
pub struct MatchExpect<M: Matcher, P>(P::Future, P::Value)
where
    P: AsyncMatch<M>;
impl<M: Matcher, P> Future for MatchExpect<M, P>
where
    P: AsyncMatch<M>,
    P::Value: PartialEq,
    M::Error: From<UnexpectedValue<P::Value>>,
{
    type Item = (M, P::Value);
    type Error = AsyncError<M, M::Error>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        if let Async::Ready((m, v)) = self.0.poll()? {
            if v == self.1 {
                Ok(Async::Ready((m, v)))
            } else {
                let e = From::from(UnexpectedValue(v));
                Err(AsyncError::new(m, e))
            }
        } else {
            Ok(Async::NotReady)
        }
    }
}
impl<M: Matcher, P> AsyncMatch<M> for Expect<P>
where
    P: AsyncMatch<M>,
    P::Value: PartialEq,
    M::Error: From<UnexpectedValue<P::Value>>,
{
    type Future = MatchExpect<M, P>;
    fn async_match(self, matcher: M) -> Self::Future {
        let (pattern, expected_value) = self.unwrap();
        MatchExpect(pattern.async_match(matcher), expected_value)
    }
}

#[derive(Debug)]
enum Phase<A, B> {
    A(A),
    B(B),
    Polled,
}
impl<A, B> Phase<A, B> {
    pub fn take(&mut self) -> Self {
        use std::mem;
        mem::replace(self, Phase::Polled)
    }
}