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
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    fidl_fuchsia_pkg as fpkg,
    futures::{future::TryFutureExt as _, stream::Stream},
};

/// Converts a proxy to a FIDL iterator like:
///
/// protocol PayloadIterator {
///    Next() -> (vector<Payload>:MAX payloads);
/// };
///
/// into a `Stream` of `Result<Vec<Payload>, fidl::Error>`s.
///
/// The returned stream will never yield an empty `Vec`. When e.g. `PayloadIterator::Next` returns
/// an empty Vec, the returned stream will yield `None` (signaling the end of the stream).
///
/// To use with a new protocol (e.g. `PayloadIterator`), implement `FidlIterator` for
/// `PayloadIteratorProxy`.
pub fn fidl_iterator_to_stream<T: FidlIterator>(
    iterator: T,
) -> impl Stream<Item = Result<Vec<T::Item>, fidl::Error>> + Unpin {
    futures::stream::try_unfold(iterator, |iterator| {
        iterator.next().map_ok(|v| if v.is_empty() { None } else { Some((v, iterator)) })
    })
}

/// A FIDL proxy for a FIDL protocol following the iterator pattern.
pub trait FidlIterator {
    type Item: Unpin;

    fn next(&self) -> fidl::client::QueryResponseFut<Vec<Self::Item>>;
}

impl FidlIterator for fpkg::BlobInfoIteratorProxy {
    type Item = fpkg::BlobInfo;

    fn next(&self) -> fidl::client::QueryResponseFut<Vec<Self::Item>> {
        self.next()
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        assert_matches::assert_matches,
        fidl::endpoints::{ControlHandle as _, Responder as _},
        fuchsia_zircon_status::Status,
        futures::{
            future::join,
            stream::{StreamExt as _, TryStreamExt as _},
        },
    };

    struct MockIteratorServer {
        reqs: fpkg::BlobInfoIteratorRequestStream,
    }

    impl MockIteratorServer {
        fn new() -> (Self, impl Stream<Item = Result<Vec<fpkg::BlobInfo>, fidl::Error>>) {
            let (proxy, reqs) =
                fidl::endpoints::create_proxy_and_stream::<fpkg::BlobInfoIteratorMarker>().unwrap();
            (Self { reqs }, fidl_iterator_to_stream(proxy))
        }

        // On Some(resp) responds with resp, else closes channel with NO_RESOURCES.
        async fn expect_next(&mut self, resp: Option<Vec<fpkg::BlobInfo>>) {
            let fpkg::BlobInfoIteratorRequest::Next { responder } =
                self.reqs.next().await.unwrap().unwrap();
            match resp {
                Some(resp) => responder.send(&resp).unwrap(),
                None => responder.control_handle().shutdown_with_epitaph(Status::NO_RESOURCES),
            }
        }
    }

    fn blob_info(u: u8) -> fpkg::BlobInfo {
        fpkg::BlobInfo { blob_id: fpkg::BlobId { merkle_root: [u; 32] }, length: 0 }
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn read_one_item() {
        let (mut server, mut stream) = MockIteratorServer::new();

        let ((), item) = join(server.expect_next(Some(vec![blob_info(1)])), stream.next()).await;

        assert_matches!(item, Some(Ok(v)) if v == vec![blob_info(1)]);
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn read_two_items() {
        let (mut server, mut stream) = MockIteratorServer::new();

        let ((), (first, second)) = join(
            async {
                server.expect_next(Some(vec![blob_info(1)])).await;
                server.expect_next(Some(vec![blob_info(2)])).await
            },
            async { (stream.next().await, stream.next().await) },
        )
        .await;

        assert_matches!(first, Some(Ok(v)) if v == vec![blob_info(1)]);
        assert_matches!(second, Some(Ok(v)) if v == vec![blob_info(2)]);
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn error_terminates() {
        let (mut server, mut stream) = MockIteratorServer::new();

        let ((), (first, second)) =
            join(server.expect_next(None), async { (stream.next().await, stream.next().await) })
                .await;

        assert_matches!(
            first,
            Some(Err(fidl::Error::ClientChannelClosed{status, ..}))
                if status == Status::NO_RESOURCES
        );
        assert_matches!(second, None);
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn empty_response_terminates() {
        let (mut server, mut stream) = MockIteratorServer::new();

        let ((), item) = join(server.expect_next(Some(vec![])), stream.next()).await;

        assert_matches!(item, None);
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn read_one_item_then_terminate_successfully() {
        let (mut server, stream) = MockIteratorServer::new();

        let ((), items) = join(
            async {
                server.expect_next(Some(vec![blob_info(1)])).await;
                server.expect_next(Some(vec![])).await
            },
            stream.map_err(|_| ()).try_concat(),
        )
        .await;

        assert_eq!(items, Ok(vec![blob_info(1)]));
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn read_one_item_then_terminate_with_error() {
        let (mut server, stream) = MockIteratorServer::new();

        let ((), items) = join(
            async {
                server.expect_next(Some(vec![blob_info(1)])).await;
                server.expect_next(None).await
            },
            stream.map_err(|_| ()).collect::<Vec<_>>(),
        )
        .await;

        assert_eq!(items, vec![Ok(vec![blob_info(1)]), Err(())]);
    }
}