std_mel/ops/option/
mod.rs1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4pub mod block;
5
6#[mel_function(
8 generic T ()
9)]
10pub fn unwrap_or(option: Option<T>, default: T) -> T {
11 option.unwrap_or(default)
12}
13
14#[mel_treatment(
18 generic T ()
19 input option Stream<Option<T>>
20 output value Stream<T>
21)]
22pub async fn unwrap() {
23 while let Ok(values) = option
24 .recv_many()
25 .await
26 .map(|values| Into::<VecDeque<Value>>::into(values))
27 {
28 check!(
29 value
30 .send_many(TransmissionValue::Other(
31 values
32 .into_iter()
33 .filter_map(|val| match val {
34 Value::Option(Some(val)) => Some(*val),
35 _ => None,
36 })
37 .collect()
38 ))
39 .await
40 )
41 }
42}
43
44#[mel_treatment(
48 generic T ()
49 input option Stream<Option<T>>
50 output value Stream<T>
51)]
52pub async fn unwrap_or(default: T) {
53 while let Ok(values) = option
54 .recv_many()
55 .await
56 .map(|values| Into::<VecDeque<Value>>::into(values))
57 {
58 check!(
59 value
60 .send_many(TransmissionValue::Other(
61 values
62 .into_iter()
63 .filter_map(|val| match val {
64 Value::Option(Some(val)) => Some(*val),
65 Value::Option(None) => Some(default.clone()),
66 _ => None,
67 })
68 .collect()
69 ))
70 .await
71 )
72 }
73}
74
75#[mel_treatment(
79 generic T ()
80 input option Stream<Option<T>>
81 output value Stream<T>
82)]
83pub async fn fuse() {
84 'main: while let Ok(values) = option
85 .recv_many()
86 .await
87 .map(|values| Into::<VecDeque<Value>>::into(values))
88 {
89 for val in values {
90 match val {
91 Value::Option(Some(val)) => check!('main, value.send_one(*val).await),
92 _ => break 'main,
93 }
94 }
95 }
96}
97
98#[mel_function(
100 generic T ()
101)]
102pub fn wrap(value: T) -> Option<T> {
103 Some(value)
104}
105
106#[mel_treatment(
110 generic T ()
111 input value Stream<T>
112 output option Stream<Option<T>>
113)]
114pub async fn wrap() {
115 while let Ok(values) = value
116 .recv_many()
117 .await
118 .map(|values| Into::<VecDeque<Value>>::into(values))
119 {
120 check!(
121 option
122 .send_many(TransmissionValue::Other(
123 values
124 .into_iter()
125 .map(|val| Value::Option(Some(Box::new(val))))
126 .collect()
127 ))
128 .await
129 )
130 }
131}