Files
drone_core
drone_cortexm
drone_ctypes
drone_nrf_map
drone_nrf_map_periph_uarte
drone_nrf_map_pieces
drone_nrf_map_pieces_1
drone_nrf_map_pieces_10
drone_nrf_map_pieces_11
drone_nrf_map_pieces_12
drone_nrf_map_pieces_2
drone_nrf_map_pieces_3
drone_nrf_map_pieces_4
drone_nrf_map_pieces_5
drone_nrf_map_pieces_6
drone_nrf_map_pieces_7
drone_nrf_map_pieces_8
drone_nrf_map_pieces_9
futures
futures_channel
futures_core
futures_io
futures_sink
futures_task
futures_util
pin_project_lite
pin_utils
proc_macro_nested
typenum
  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
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::ready;
use futures_core::stream::{FusedStream, Stream, TryStream};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;

pin_project! {
    #[project = TryFlattenProj]
    #[derive(Debug)]
    pub enum TryFlatten<Fut1, Fut2> {
        First { #[pin] f: Fut1 },
        Second { #[pin] f: Fut2 },
        Empty,
    }
}

impl<Fut1, Fut2> TryFlatten<Fut1, Fut2> {
    pub(crate) fn new(future: Fut1) -> Self {
        Self::First { f: future }
    }
}

impl<Fut> FusedFuture for TryFlatten<Fut, Fut::Ok>
    where Fut: TryFuture,
          Fut::Ok: TryFuture<Error=Fut::Error>,
{
    fn is_terminated(&self) -> bool {
        match self {
            Self::Empty => true,
            _ => false,
        }
    }
}

impl<Fut> Future for TryFlatten<Fut, Fut::Ok>
    where Fut: TryFuture,
          Fut::Ok: TryFuture<Error=Fut::Error>,
{
    type Output = Result<<Fut::Ok as TryFuture>::Ok, Fut::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Poll::Ready(loop {
            match self.as_mut().project() {
                TryFlattenProj::First { f } => {
                    match ready!(f.try_poll(cx)) {
                        Ok(f) => self.set(Self::Second { f }),
                        Err(e) => {
                            self.set(Self::Empty);
                            break Err(e);
                        }
                    }
                },
                TryFlattenProj::Second { f } => {
                    let output = ready!(f.try_poll(cx));
                    self.set(Self::Empty);
                    break output;
                },
                TryFlattenProj::Empty => panic!("TryFlatten polled after completion"),
            }
        })
    }
}

impl<Fut> FusedStream for TryFlatten<Fut, Fut::Ok>
    where Fut: TryFuture,
          Fut::Ok: TryStream<Error=Fut::Error>,
{
    fn is_terminated(&self) -> bool {
        match self {
            Self::Empty => true,
            _ => false,
        }
    }
}

impl<Fut> Stream for TryFlatten<Fut, Fut::Ok>
    where Fut: TryFuture,
          Fut::Ok: TryStream<Error=Fut::Error>,
{
    type Item = Result<<Fut::Ok as TryStream>::Ok, Fut::Error>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(loop {
            match self.as_mut().project() {
                TryFlattenProj::First { f } => {
                    match ready!(f.try_poll(cx)) {
                        Ok(f) => self.set(Self::Second { f }),
                        Err(e) => {
                            self.set(Self::Empty);
                            break Some(Err(e));
                        }
                    }
                },
                TryFlattenProj::Second { f } => {
                    let output = ready!(f.try_poll_next(cx));
                    if output.is_none() {
                        self.set(Self::Empty);
                    }
                    break output;
                },
                TryFlattenProj::Empty => break None,
            }
        })
    }
}


#[cfg(feature = "sink")]
impl<Fut, Item> Sink<Item> for TryFlatten<Fut, Fut::Ok>
where
    Fut: TryFuture,
    Fut::Ok: Sink<Item, Error=Fut::Error>,
{
    type Error = Fut::Error;

    fn poll_ready(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(loop {
            match self.as_mut().project() {
                TryFlattenProj::First { f } => {
                    match ready!(f.try_poll(cx)) {
                        Ok(f) => self.set(Self::Second { f }),
                        Err(e) => {
                            self.set(Self::Empty);
                            break Err(e);
                        }
                    }
                },
                TryFlattenProj::Second { f } => {
                    break ready!(f.poll_ready(cx));
                },
                TryFlattenProj::Empty => panic!("poll_ready called after eof"),
            }
        })
    }

    fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
        match self.project() {
            TryFlattenProj::First { .. } => panic!("poll_ready not called first"),
            TryFlattenProj::Second { f } => f.start_send(item),
            TryFlattenProj::Empty => panic!("start_send called after eof"),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.project() {
            TryFlattenProj::First { .. } => Poll::Ready(Ok(())),
            TryFlattenProj::Second { f } => f.poll_flush(cx),
            TryFlattenProj::Empty => panic!("poll_flush called after eof"),
        }
    }

    fn poll_close(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let res = match self.as_mut().project() {
            TryFlattenProj::Second { f } => f.poll_close(cx),
            _ => Poll::Ready(Ok(())),
        };
        if res.is_ready() {
            self.set(Self::Empty);
        }
        res
    }
}