handy_async/matcher/
async_match.rs

1use futures::{self, Poll, Async, Future, Stream};
2
3use pattern::{Pattern, Branch, Iter};
4use pattern::combinators::{Map, AndThen, Then, OrElse, Or, Chain};
5use pattern::combinators::{IterFold, Expect, UnexpectedValue};
6use error::AsyncError;
7use super::Matcher;
8
9/// The `AsyncMatch` trait allows for asyncronous matching
10/// between a pattern `Self` and a matcher `M`.
11///
12/// Normally, users will not be aware of this trait and will use
13/// more specific interfaces like [`ReadFrom`](../pattern/trait.ReadFrom.html) and
14/// [`WriteInto`](../pattern/trait.WriteInto.html).
15///
16/// For details on how to define your own matcher,
17/// see the documentation of [`Matcher`](./trait.Matcher.html) trait.
18pub trait AsyncMatch<M: Matcher>: Pattern {
19    /// The future type which will produce a value `Self::Value` by
20    /// matching this pattern and a matcher `M`.
21    type Future: Future<Item = (M, Self::Value), Error = AsyncError<M, M::Error>>;
22
23    /// Creates a future which will produce a `Self::Value` by
24    /// matching this pattern and the `matcher`.
25    fn async_match(self, matcher: M) -> Self::Future;
26
27    /// Consumes this pattern and the `matcher`,
28    /// returning a stream which will produce a sequence of matched values.
29    ///
30    /// # Examples
31    ///
32    /// ```
33    /// # extern crate futures;
34    /// # extern crate handy_async;
35    /// use futures::{Future, Stream};
36    /// use handy_async::pattern::read::U8;
37    /// use handy_async::matcher::AsyncMatch;
38    /// use handy_async::io::PatternReader;
39    ///
40    /// # fn main() {
41    /// let matcher = PatternReader::new(&b"hello"[..]);
42    /// let values = U8.into_stream(matcher).take(3).collect().wait().unwrap();
43    /// assert_eq!(values, b"hel");
44    /// # }
45    /// ```
46    fn into_stream(self, matcher: M) -> MatchStream<M, Self>
47    where
48        Self: Clone,
49    {
50        let p = self.clone();
51        MatchStream(self, p.async_match(matcher))
52    }
53}
54
55/// Stream to produce a sequence of matched values.
56///
57/// This is created by calling `AsyncMatch::into_stream` method.
58pub struct MatchStream<M: Matcher, P>(P, P::Future)
59where
60    P: AsyncMatch<M>;
61impl<M: Matcher, P> Stream for MatchStream<M, P>
62where
63    P: AsyncMatch<M> + Clone,
64{
65    type Item = P::Value;
66    type Error = AsyncError<M, M::Error>;
67    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
68        if let Async::Ready((m, v)) = self.1.poll()? {
69            let p = self.0.clone();
70            self.1 = p.async_match(m);
71            Ok(Async::Ready(Some(v)))
72        } else {
73            Ok(Async::NotReady)
74        }
75    }
76}
77
78/// Future to do pattern matching of
79/// [`Map`](../../pattern/combinators/struct.Map.html) pattern.
80pub struct MatchMap<P, F>(Option<(P, F)>);
81impl<M, P, F, T, U> Future for MatchMap<P, F>
82where
83    P: Future<Item = (M, T)>,
84    F: FnOnce(T) -> U,
85{
86    type Item = (M, U);
87    type Error = P::Error;
88    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
89        let (mut p, f) = self.0.take().expect("Cannot poll MatchMap twice");
90        if let Async::Ready((matcher, v)) = p.poll()? {
91            Ok(Async::Ready((matcher, f(v))))
92        } else {
93            self.0 = Some((p, f));
94            Ok(Async::NotReady)
95        }
96    }
97}
98impl<M: Matcher, P, F, T> AsyncMatch<M> for Map<P, F>
99where
100    F: FnOnce(P::Value) -> T,
101    P: AsyncMatch<M>,
102{
103    type Future = MatchMap<<P as AsyncMatch<M>>::Future, F>;
104    fn async_match(self, matcher: M) -> Self::Future {
105        let (p, f) = self.unwrap();
106        MatchMap(Some((p.async_match(matcher), f)))
107    }
108}
109
110/// Future to do pattern matching of
111/// [`AndThen`](../../pattern/combinators/struct.AndThen.html) pattern.
112pub struct MatchAndThen<M, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
113where
114    M: Matcher,
115    P0: AsyncMatch<M>,
116    P1: AsyncMatch<M>,
117    F: FnOnce(P0::Value) -> P1;
118impl<M: Matcher, P0, P1, F> Future for MatchAndThen<M, P0, P1, F>
119where
120    P0: AsyncMatch<M>,
121    P1: AsyncMatch<M>,
122    F: FnOnce(P0::Value) -> P1,
123{
124    type Item = (M, P1::Value);
125    type Error = AsyncError<M, M::Error>;
126    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
127        match self.0.take() {
128            Phase::A((mut p0, f)) => {
129                if let Async::Ready((m, v0)) = p0.poll()? {
130                    let p1 = f(v0).async_match(m);
131                    self.0 = Phase::B(p1);
132                    self.poll()
133                } else {
134                    self.0 = Phase::A((p0, f));
135                    Ok(Async::NotReady)
136                }
137            }
138            Phase::B(mut p1) => {
139                if let Async::Ready((m, v1)) = p1.poll()? {
140                    Ok(Async::Ready((m, v1)))
141                } else {
142                    self.0 = Phase::B(p1);
143                    Ok(Async::NotReady)
144                }
145            }
146            _ => panic!("Cannot poll MatchAndThen twice"),
147        }
148    }
149}
150impl<M: Matcher, P0, P1, F> AsyncMatch<M> for AndThen<P0, F>
151where
152    P0: AsyncMatch<M>,
153    P1: AsyncMatch<M>,
154    F: FnOnce(P0::Value) -> P1,
155{
156    type Future = MatchAndThen<M, P0, P1, F>;
157    fn async_match(self, matcher: M) -> Self::Future {
158        let (p, f) = self.unwrap();
159        MatchAndThen(Phase::A((p.async_match(matcher), f)))
160    }
161}
162
163/// Future to do pattern matching of
164/// [Then](../../pattern/combinators/struct.Then.html) pattern.
165pub struct MatchThen<M: Matcher, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
166where
167    P0: AsyncMatch<M>,
168    P1: AsyncMatch<M>,
169    F: FnOnce(Result<P0::Value, M::Error>) -> P1;
170impl<M: Matcher, P0, P1, F> Future for MatchThen<M, P0, P1, F>
171where
172    P0: AsyncMatch<M>,
173    P1: AsyncMatch<M>,
174    F: FnOnce(Result<P0::Value, M::Error>)
175           -> P1,
176{
177    type Item = (M, P1::Value);
178    type Error = AsyncError<M, M::Error>;
179    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
180        match self.0.take() {
181            Phase::A((mut p0, f)) => {
182                match p0.poll() {
183                    Err(e) => {
184                        let (m, e) = e.unwrap();
185                        let p1 = f(Err(e)).async_match(m);
186                        self.0 = Phase::B(p1);
187                        self.poll()
188                    }
189                    Ok(Async::Ready((m, v0))) => {
190                        let p1 = f(Ok(v0)).async_match(m);
191                        self.0 = Phase::B(p1);
192                        self.poll()
193                    }
194                    Ok(Async::NotReady) => {
195                        self.0 = Phase::A((p0, f));
196                        Ok(Async::NotReady)
197                    }
198                }
199            }
200            Phase::B(mut p1) => {
201                if let Async::Ready((m, v1)) = p1.poll()? {
202                    Ok(Async::Ready((m, v1)))
203                } else {
204                    self.0 = Phase::B(p1);
205                    Ok(Async::NotReady)
206                }
207            }
208            _ => panic!("Cannot poll MatchThen twice"),
209        }
210    }
211}
212impl<M: Matcher, P0, P1, F> AsyncMatch<M> for Then<P0, F, M::Error>
213where
214    P0: AsyncMatch<M>,
215    P1: AsyncMatch<M>,
216    F: FnOnce(Result<
217        P0::Value,
218        M::Error,
219    >)
220           -> P1,
221{
222    type Future = MatchThen<M, P0, P1, F>;
223    fn async_match(self, matcher: M) -> Self::Future {
224        let (p, f) = self.unwrap();
225        MatchThen(Phase::A((p.async_match(matcher), f)))
226    }
227}
228
229/// Future to do pattern matching of
230/// [`OrElse`](../../pattern/combinators/struct.OrElse.html) pattern.
231pub struct MatchOrElse<M: Matcher, P0, P1, F>(Phase<(P0::Future, F), P1::Future>)
232where
233    P0: AsyncMatch<M>,
234    P1: AsyncMatch<M>,
235    F: FnOnce(M::Error) -> P1;
236impl<M: Matcher, P0, P1, F> Future for MatchOrElse<M, P0, P1, F>
237where
238    P0: AsyncMatch<M>,
239    P1: AsyncMatch<M, Value = P0::Value>,
240    F: FnOnce(M::Error) -> P1,
241{
242    type Item = (M, P1::Value);
243    type Error = AsyncError<M, M::Error>;
244    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
245        match self.0.take() {
246            Phase::A((mut p0, f)) => {
247                match p0.poll() {
248                    Err(e) => {
249                        let (m, e) = e.unwrap();
250                        let p1 = f(e).async_match(m);
251                        self.0 = Phase::B(p1);
252                        self.poll()
253                    }
254                    Ok(Async::Ready((m, v0))) => Ok(Async::Ready((m, v0))),
255                    Ok(Async::NotReady) => {
256                        self.0 = Phase::A((p0, f));
257                        Ok(Async::NotReady)
258                    }
259                }
260            }
261            Phase::B(mut p1) => {
262                if let Async::Ready((m, v1)) = p1.poll()? {
263                    Ok(Async::Ready((m, v1)))
264                } else {
265                    self.0 = Phase::B(p1);
266                    Ok(Async::NotReady)
267                }
268            }
269            _ => panic!("Cannot poll MatchOrElse twice"),
270        }
271    }
272}
273impl<M: Matcher, P0, P1, F> AsyncMatch<M> for OrElse<P0, F, M::Error>
274where
275    P0: AsyncMatch<M>,
276    P1: AsyncMatch<
277        M,
278        Value = P0::Value,
279    >,
280    F: FnOnce(M::Error) -> P1,
281{
282    type Future = MatchOrElse<M, P0, P1, F>;
283    fn async_match(self, matcher: M) -> Self::Future {
284        let (p, f) = self.unwrap();
285        MatchOrElse(Phase::A((p.async_match(matcher), f)))
286    }
287}
288
289/// Future to do pattern matching of
290/// [Or](../../pattern/combinators/struct.Or.html) pattern.
291pub struct MatchOr<M: Matcher, P0, P1>(Phase<(P0::Future, P1), P1::Future>)
292where
293    P0: AsyncMatch<M>,
294    P1: AsyncMatch<M>;
295impl<M: Matcher, P0, P1> Future for MatchOr<M, P0, P1>
296where
297    P0: AsyncMatch<M>,
298    P1: AsyncMatch<M, Value = P0::Value>,
299{
300    type Item = (M, P1::Value);
301    type Error = AsyncError<M, M::Error>;
302    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
303        match self.0.take() {
304            Phase::A((mut p0, p1)) => {
305                match p0.poll() {
306                    Err(e) => {
307                        let (m, _) = e.unwrap();
308                        let p1 = p1.async_match(m);
309                        self.0 = Phase::B(p1);
310                        self.poll()
311                    }
312                    Ok(Async::Ready((m, v0))) => Ok(Async::Ready((m, v0))),
313                    Ok(Async::NotReady) => {
314                        self.0 = Phase::A((p0, p1));
315                        Ok(Async::NotReady)
316                    }
317                }
318            }
319            Phase::B(mut p1) => {
320                if let Async::Ready((m, v1)) = p1.poll()? {
321                    Ok(Async::Ready((m, v1)))
322                } else {
323                    self.0 = Phase::B(p1);
324                    Ok(Async::NotReady)
325                }
326            }
327            _ => panic!("Cannot poll MatchOr twice"),
328        }
329    }
330}
331impl<M: Matcher, P0, P1> AsyncMatch<M> for Or<P0, P1>
332where
333    P0: AsyncMatch<M>,
334    P1: AsyncMatch<M, Value = P0::Value>,
335{
336    type Future = MatchOr<M, P0, P1>;
337    fn async_match(self, matcher: M) -> Self::Future {
338        let (p0, p1) = self.unwrap();
339        MatchOr(Phase::A((p0.async_match(matcher), p1)))
340    }
341}
342
343/// Future to do pattern matching of
344/// [Chain](../../pattern/combinators/struct.Chain.html) pattern.
345pub struct MatchChain<M: Matcher, P0, P1>(ChainPhase<P0::Future, P1, P1::Future, P0::Value>)
346where
347    P0: AsyncMatch<M>,
348    P1: AsyncMatch<M>;
349impl<M: Matcher, P0, P1> Future for MatchChain<M, P0, P1>
350where
351    P0: AsyncMatch<M>,
352    P1: AsyncMatch<M>,
353{
354    type Item = (M, (P0::Value, P1::Value));
355    type Error = AsyncError<M, M::Error>;
356    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
357        match self.0.take() {
358            Phase::A((mut p0, p1)) => {
359                match p0.poll() {
360                    Err(e) => Err(e),
361                    Ok(Async::Ready((m, v0))) => {
362                        self.0 = Phase::B((p1.async_match(m), v0));
363                        self.poll()
364                    }
365                    Ok(Async::NotReady) => {
366                        self.0 = Phase::A((p0, p1));
367                        Ok(Async::NotReady)
368                    }
369                }
370            }
371            Phase::B((mut p1, v0)) => {
372                if let Async::Ready((m, v1)) = p1.poll()? {
373                    Ok(Async::Ready((m, (v0, v1))))
374                } else {
375                    self.0 = Phase::B((p1, v0));
376                    Ok(Async::NotReady)
377                }
378            }
379            _ => panic!("Cannot poll MatchChain twice"),
380        }
381    }
382}
383impl<M: Matcher, P0, P1> AsyncMatch<M> for Chain<P0, P1>
384where
385    P0: AsyncMatch<M>,
386    P1: AsyncMatch<M>,
387{
388    type Future = MatchChain<M, P0, P1>;
389    fn async_match(self, matcher: M) -> Self::Future {
390        let (p0, p1) = self.unwrap();
391        MatchChain(Phase::A((p0.async_match(matcher), p1)))
392    }
393}
394type ChainPhase<F0, Next, F1, Value> = Phase<(F0, Next), (F1, Value)>;
395
396/// Future to do pattern matching of
397/// [Option](../../pattern/type.Option.html) pattern.
398pub struct MatchOption<M: Matcher, P>(Option<Result<P::Future, M>>)
399where
400    P: AsyncMatch<M>;
401impl<M: Matcher, P> Future for MatchOption<M, P>
402where
403    P: AsyncMatch<M>,
404{
405    type Item = (M, Option<P::Value>);
406    type Error = AsyncError<M, M::Error>;
407    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
408        let inner = self.0.take().expect("Cannot poll MatchOption twice");
409        match inner {
410            Ok(mut f) => {
411                if let Async::Ready((m, v)) = f.poll()? {
412                    Ok(Async::Ready((m, Some(v))))
413                } else {
414                    self.0 = Some(Ok(f));
415                    Ok(Async::NotReady)
416                }
417            }
418            Err(m) => Ok(Async::Ready((m, None))),
419        }
420    }
421}
422impl<M: Matcher, P> AsyncMatch<M> for Option<P>
423where
424    P: AsyncMatch<M>,
425{
426    type Future = MatchOption<M, P>;
427    fn async_match(self, matcher: M) -> Self::Future {
428        if let Some(p) = self {
429            MatchOption(Some(Ok(p.async_match(matcher))))
430        } else {
431            MatchOption(Some(Err(matcher)))
432        }
433    }
434}
435
436impl<M: Matcher, T> AsyncMatch<M> for Result<T, M::Error> {
437    type Future = futures::Done<(M, T), AsyncError<M, M::Error>>;
438    fn async_match(self, matcher: M) -> Self::Future {
439        match self {
440            Ok(v) => futures::done(Ok((matcher, v))),
441            Err(e) => futures::done(Err(AsyncError::new(matcher, e))),
442        }
443    }
444}
445
446/// Future to do pattern matching of
447/// [Branch](../../pattern/struct.Branch.html) pattern.
448#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
449pub struct MatchBranch<M, A, B, C, D, E, F, G, H>
450where
451    M: Matcher,
452    A: AsyncMatch<M>,
453    B: AsyncMatch<M, Value = A::Value>,
454    C: AsyncMatch<M, Value = A::Value>,
455    D: AsyncMatch<M, Value = A::Value>,
456    E: AsyncMatch<M, Value = A::Value>,
457    F: AsyncMatch<M, Value = A::Value>,
458    G: AsyncMatch<M, Value = A::Value>,
459    H: AsyncMatch<M, Value = A::Value>,
460{
461    future: Branch<
462        A::Future,
463        B::Future,
464        C::Future,
465        D::Future,
466        E::Future,
467        F::Future,
468        G::Future,
469        H::Future,
470    >,
471}
472#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
473impl<M, A, B, C, D, E, F, G, H> Future for MatchBranch<M, A, B, C, D, E, F, G, H>
474    where M: Matcher,
475          A: AsyncMatch<M>,
476          B: AsyncMatch<M, Value = A::Value>,
477          C: AsyncMatch<M, Value = A::Value>,
478          D: AsyncMatch<M, Value = A::Value>,
479          E: AsyncMatch<M, Value = A::Value>,
480          F: AsyncMatch<M, Value = A::Value>,
481          G: AsyncMatch<M, Value = A::Value>,
482          H: AsyncMatch<M, Value = A::Value>
483{
484    type Item = <Branch<A::Future,
485           B::Future,
486           C::Future,
487           D::Future,
488           E::Future,
489           F::Future,
490           G::Future,
491           H::Future> as Future>::Item;
492    type Error = <Branch<A::Future,
493           B::Future,
494           C::Future,
495           D::Future,
496           E::Future,
497           F::Future,
498           G::Future,
499           H::Future> as Future>::Error;
500    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
501        self.future.poll()
502    }
503}
504impl<M, A, B, C, D, E, F, G, H> AsyncMatch<M> for Branch<A, B, C, D, E, F, G, H>
505    where M: Matcher,
506          A: AsyncMatch<M>,
507          B: AsyncMatch<M, Value = A::Value>,
508          C: AsyncMatch<M, Value = A::Value>,
509          D: AsyncMatch<M, Value = A::Value>,
510          E: AsyncMatch<M, Value = A::Value>,
511          F: AsyncMatch<M, Value = A::Value>,
512          G: AsyncMatch<M, Value = A::Value>,
513          H: AsyncMatch<M, Value = A::Value>
514{
515    type Future = MatchBranch<M, A, B, C, D, E, F, G, H>;
516    fn async_match(self, matcher: M) -> Self::Future {
517        let future = match self {
518            Branch::A(p) => Branch::A(p.async_match(matcher)),
519            Branch::B(p) => Branch::B(p.async_match(matcher)),
520            Branch::C(p) => Branch::C(p.async_match(matcher)),
521            Branch::D(p) => Branch::D(p.async_match(matcher)),
522            Branch::E(p) => Branch::E(p.async_match(matcher)),
523            Branch::F(p) => Branch::F(p.async_match(matcher)),
524            Branch::G(p) => Branch::G(p.async_match(matcher)),
525            Branch::H(p) => Branch::H(p.async_match(matcher)),
526        };
527        MatchBranch { future: future }
528    }
529}
530
531/// Future to do pattern matching of
532/// [`IterFold`](../../pattern/combinators/struct.IterFold.html) pattern.
533#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
534pub struct MatchIterFold<M: Matcher, I, F, T>
535where
536    I: Iterator,
537    I::Item: AsyncMatch<M>,
538{
539    phase: Phase<(<I::Item as AsyncMatch<M>>::Future, I, T, F), (M, T)>,
540}
541impl<M: Matcher, I, F, T> Future for MatchIterFold<M, I, F, T>
542    where I: Iterator,
543          I::Item: AsyncMatch<M>,
544          F: Fn(T, <I::Item as Pattern>::Value) -> T
545{
546    type Item = (M, T);
547    type Error = AsyncError<M, M::Error>;
548    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
549        match self.phase.take() {
550            Phase::A((mut f, mut iter, acc, fold)) => {
551                if let Async::Ready((m, v)) = f.poll()? {
552                    let acc = fold(acc, v);
553                    if let Some(p) = iter.next() {
554                        self.phase = Phase::A((p.async_match(m), iter, acc, fold));
555                        self.poll()
556                    } else {
557                        Ok(Async::Ready((m, acc)))
558                    }
559                } else {
560                    self.phase = Phase::A((f, iter, acc, fold));
561                    Ok(Async::NotReady)
562                }
563            }
564            Phase::B((m, v)) => Ok(Async::Ready((m, v))),
565            _ => panic!("Cannot poll MatchIterFold twice"),
566        }
567    }
568}
569impl<M: Matcher, I, F, T> AsyncMatch<M> for IterFold<I, F, T>
570    where I: Iterator,
571          I::Item: AsyncMatch<M>,
572          F: Fn(T, <I::Item as Pattern>::Value) -> T
573{
574    type Future = MatchIterFold<M, I, F, T>;
575    fn async_match(self, matcher: M) -> Self::Future {
576        let (mut iter, fold, acc) = self.unwrap();
577        if let Some(p) = iter.next() {
578            MatchIterFold{ phase: Phase::A((p.async_match(matcher), iter, acc, fold)) }
579        } else {
580            MatchIterFold{ phase: Phase::B((matcher, acc)) }
581        }
582    }
583}
584
585/// Future to do pattern matching of
586/// [Iter](../../pattern/struct.Iter.html) pattern.
587pub struct MatchIter<M: Matcher, I>(Phase<(<I::Item as AsyncMatch<M>>::Future, I), M>)
588where
589    I: Iterator,
590    I::Item: AsyncMatch<M>;
591impl<M: Matcher, I> Future for MatchIter<M, I>
592where
593    I: Iterator,
594    I::Item: AsyncMatch<M>,
595{
596    type Item = (M, ());
597    type Error = AsyncError<M, M::Error>;
598    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
599        match self.0.take() {
600            Phase::A((mut f, mut iter)) => {
601                if let Async::Ready((m, _)) = f.poll()? {
602                    if let Some(p) = iter.next() {
603                        self.0 = Phase::A((p.async_match(m), iter));
604                        self.poll()
605                    } else {
606                        Ok(Async::Ready((m, ())))
607                    }
608                } else {
609                    self.0 = Phase::A((f, iter));
610                    Ok(Async::NotReady)
611                }
612            }
613            Phase::B(m) => Ok(Async::Ready((m, ()))),
614            _ => panic!("Cannot poll MatchIter twice"),
615        }
616    }
617}
618impl<M: Matcher, I> AsyncMatch<M> for Iter<I>
619where
620    I: Iterator,
621    I::Item: AsyncMatch<M>,
622{
623    type Future = MatchIter<M, I>;
624    fn async_match(self, matcher: M) -> Self::Future {
625        let mut iter = self.0;
626        if let Some(p) = iter.next() {
627            MatchIter(Phase::A((p.async_match(matcher), iter)))
628        } else {
629            MatchIter(Phase::B(matcher))
630        }
631    }
632}
633
634/// Future to do pattern matching of
635/// [Expect](../../pattern/struct.Expect.html) pattern.
636pub struct MatchExpect<M: Matcher, P>(P::Future, P::Value)
637where
638    P: AsyncMatch<M>;
639impl<M: Matcher, P> Future for MatchExpect<M, P>
640where
641    P: AsyncMatch<M>,
642    P::Value: PartialEq,
643    M::Error: From<UnexpectedValue<P::Value>>,
644{
645    type Item = (M, P::Value);
646    type Error = AsyncError<M, M::Error>;
647    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
648        if let Async::Ready((m, v)) = self.0.poll()? {
649            if v == self.1 {
650                Ok(Async::Ready((m, v)))
651            } else {
652                let e = From::from(UnexpectedValue(v));
653                Err(AsyncError::new(m, e))
654            }
655        } else {
656            Ok(Async::NotReady)
657        }
658    }
659}
660impl<M: Matcher, P> AsyncMatch<M> for Expect<P>
661where
662    P: AsyncMatch<M>,
663    P::Value: PartialEq,
664    M::Error: From<UnexpectedValue<P::Value>>,
665{
666    type Future = MatchExpect<M, P>;
667    fn async_match(self, matcher: M) -> Self::Future {
668        let (pattern, expected_value) = self.unwrap();
669        MatchExpect(pattern.async_match(matcher), expected_value)
670    }
671}
672
673#[derive(Debug)]
674enum Phase<A, B> {
675    A(A),
676    B(B),
677    Polled,
678}
679impl<A, B> Phase<A, B> {
680    pub fn take(&mut self) -> Self {
681        use std::mem;
682        mem::replace(self, Phase::Polled)
683    }
684}