summary refs log tree commit diff
path: root/cros_async/src/select.rs
blob: b10bc2c64683a4af4c6103e6c60cf910c5e134b6 (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
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Need non-snake case so the macro can re-use type names for variables.
#![allow(non_snake_case)]

use std::future::Future;
use std::pin::Pin;
use std::task::Context;

use futures::future::{maybe_done, FutureExt, MaybeDone};

use crate::executor::{FutureList, FutureState, UnitFutures};

pub enum SelectResult<F: Future> {
    Pending(F),
    Finished(F::Output),
}

// Macro-generate future combinators to allow for running different numbers of top-level futures in
// this FutureList. Generates the implementation of `FutureList` for the select types. For an
// explicit example this is modeled after, see `UnitFutures`.
macro_rules! generate {
    ($(
        $(#[$doc:meta])*
        ($Select:ident, <$($Fut:ident),*>),
    )*) => ($(
        #[must_use = "Combinations of futures don't do anything unless run in an executor."]
        paste::item! {
            pub(crate) struct $Select<$($Fut: Future + Unpin),*> {
                added_futures: UnitFutures,
                $($Fut: MaybeDone<$Fut>,)*
                $([<$Fut _state>]: FutureState,)*
            }
        }

        impl<$($Fut: Future + Unpin),*> $Select<$($Fut),*> {
            paste::item! {
                pub(crate) fn new($($Fut: $Fut),*) -> $Select<$($Fut),*> {
                    $Select {
                        added_futures: UnitFutures::new(),
                        $($Fut: maybe_done($Fut),)*
                        $([<$Fut _state>]: FutureState::new(),)*
                    }
                }
            }
        }

        impl<$($Fut: Future + Unpin),*> FutureList for $Select<$($Fut),*> {
            type Output = ($(SelectResult<$Fut>),*);

            fn futures_mut(&mut self) -> &mut UnitFutures {
                &mut self.added_futures
            }

            paste::item! {
                fn poll_results(&mut self) -> Option<Self::Output> {
                    let _ = self.added_futures.poll_results();

                    let mut complete = false;
                    $(
                        let $Fut = Pin::new(&mut self.$Fut);
                        if self.[<$Fut _state>].needs_poll.replace(false) {
                            let mut ctx = Context::from_waker(&self.[<$Fut _state>].waker);
                            // The future impls `Unpin`, use `poll_unpin` to avoid wrapping it in
                            // `Pin` to call `poll`.
                            complete |= self.$Fut.poll_unpin(&mut ctx).is_ready();
                        }
                    )*

                    if complete {
                        Some(($(
                                    match std::mem::replace(&mut self.$Fut, MaybeDone::Gone) {
                                        MaybeDone::Future(f) => SelectResult::Pending(f),
                                        MaybeDone::Done(o) => SelectResult::Finished(o),
                                        MaybeDone::Gone=>unreachable!(),
                                    }
                               ), *))
                    } else {
                        None
                    }
                }

                fn any_ready(&self) -> bool {
                    let mut ready = self.added_futures.any_ready();
                    $(
                        ready |= self.[<$Fut _state>].needs_poll.get();
                    )*
                    ready
                }
            }
        }
    )*)
}

generate! {
    /// _Future for the [`select2`] function.
    (Select2, <_Fut1, _Fut2>),

    /// _Future for the [`select3`] function.
    (Select3, <_Fut1, _Fut2, _Fut3>),

    /// _Future for the [`select4`] function.
    (Select4, <_Fut1, _Fut2, _Fut3, _Fut4>),

    /// _Future for the [`select5`] function.
    (Select5, <_Fut1, _Fut2, _Fut3, _Fut4, _Fut5>),

    /// _Future for the [`select6`] function.
    (Select6, <_Fut1, _Fut2, _Fut3, _Fut4, _Fut5, _Fut6>),
}