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
//! Named branch results — the outputs of parallel fork branches.
use bytes::Bytes;
use std::collections::HashMap;
/// Named branch results — the outputs of parallel fork branches.
///
/// Wraps `Vec<(String, Bytes)>` and implements `serde::Serialize` /
/// `serde::Deserialize`, so any codec that handles serde types can
/// encode and decode it.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct NamedBranchResults(Vec<(String, Bytes)>);
impl NamedBranchResults {
/// Create from a vec of `(name, data)` pairs.
#[must_use]
pub fn new(results: Vec<(String, Bytes)>) -> Self {
Self(results)
}
/// View the underlying pairs.
#[must_use]
pub fn as_slice(&self) -> &[(String, Bytes)] {
&self.0
}
/// Number of branch results.
#[must_use]
pub const fn len(&self) -> usize {
self.0.len()
}
/// Whether there are no results.
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Consume into the inner vec.
#[must_use]
pub fn into_vec(self) -> Vec<(String, Bytes)> {
self.0
}
/// Convert to a lookup map. When duplicate names exist, the last value wins.
#[must_use]
pub fn into_map(self) -> HashMap<String, Bytes> {
self.0.into_iter().collect()
}
}
impl From<Vec<(String, Bytes)>> for NamedBranchResults {
fn from(results: Vec<(String, Bytes)>) -> Self {
Self(results)
}
}