-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_packet.rs
More file actions
46 lines (44 loc) · 1.56 KB
/
multi_packet.rs
File metadata and controls
46 lines (44 loc) · 1.56 KB
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
//! Helpers for draining `Response::MultiPacket` values in tests.
//!
//! These utilities collect all frames from a [`Response::MultiPacket`] into a
//! `Vec`, enabling concise assertions in tests and Cucumber steps.
use wireframe::Response;
/// Collect all frames from a [`Response::MultiPacket`].
///
/// # Examples
///
/// ```rust
/// use tokio::sync::mpsc;
/// use wireframe::Response;
/// use wireframe_testing::collect_multi_packet;
///
/// # async fn demo() {
/// let (tx, rx) = mpsc::channel(4);
/// tx.send(1u8).await.expect("send");
/// drop(tx);
/// let frames = collect_multi_packet(Response::MultiPacket(rx)).await;
/// assert_eq!(frames, vec![1]);
/// # }
/// ```
///
/// # Panics
/// Panics if `resp` is not [`Response::MultiPacket`]; the panic message names
/// the received variant and is attributed to the caller.
#[must_use]
#[track_caller]
#[allow(ungated_async_fn_track_caller)] // track_caller on async is unstable
pub async fn collect_multi_packet<F, E>(resp: Response<F, E>) -> Vec<F> {
match resp {
Response::MultiPacket(mut rx) => {
let mut frames = Vec::new();
while let Some(frame) = rx.recv().await {
frames.push(frame);
}
frames
}
Response::Single(_) => panic!("collect_multi_packet received Response::Single"),
Response::Vec(_) => panic!("collect_multi_packet received Response::Vec"),
Response::Stream(_) => panic!("collect_multi_packet received Response::Stream"),
Response::Empty => panic!("collect_multi_packet received Response::Empty"),
}
}