Skip to main content

std_mel/flow/
mod.rs

1use futures::{pin_mut, select, FutureExt};
2use melodium_core::common::executive::{InputExt, OutputExt, Value};
3use melodium_macro::{check, mel_treatment};
4use std::collections::VecDeque;
5
6pub mod concentrate;
7pub mod scatter;
8pub mod vec;
9
10/// Chain two streams.
11///
12///
13/// ```mermaid
14/// graph LR
15///     T("chain()")
16///     A["🟨 🟨 🟨 🟨 🟨 🟨"] -->|first| T
17///     B["… 🟪 🟪 🟪"] -->|second| T
18///     
19///     T -->|chained| O["… 🟪 🟪 🟪 🟨 🟨 🟨 🟨 🟨 🟨"]
20///
21///     style A fill:#ffffff,stroke:#ffffff
22///     style B fill:#ffffff,stroke:#ffffff
23///     style O fill:#ffffff,stroke:#ffffff
24/// ```
25#[mel_treatment(
26    generic T ()
27    input first Stream<T>
28    input second Stream<T>
29    output chained Stream<T>
30)]
31pub async fn chain() {
32    while let Ok(values) = first.recv_many().await {
33        check!(chained.send_many(values).await)
34    }
35
36    while let Ok(values) = second.recv_many().await {
37        check!(chained.send_many(values).await)
38    }
39}
40
41/// Trigger on a stream start and end.
42///
43/// Emit `start` when a first value is send through the stream.
44/// Emit `end` when stream is finally over.
45///
46/// Emit `first` with the first value coming in the stream.
47/// Emit `last` with the last value coming in the stream.
48///
49/// ℹ️ `start` and `first` are always emitted together.
50/// If the stream only contains one element, `first` and `last` both contains it.
51/// If the stream never transmit any data before being ended, only `end` is emitted.
52///
53/// ```mermaid
54/// graph LR
55///     T("trigger()")
56///     B["🟥 … 🟨 🟨 🟨 🟨 🟨 🟨 … 🟩"] -->|stream| T
57///     
58///     T -->|start| S["〈🟦〉"]
59///     T -->|first| F["〈🟩〉"]
60///     T -->|last| L["〈🟥〉"]
61///     T -->|end| E["〈🟦〉"]
62///
63///     style B fill:#ffffff,stroke:#ffffff
64///     style S fill:#ffffff,stroke:#ffffff
65///     style F fill:#ffffff,stroke:#ffffff
66///     style L fill:#ffffff,stroke:#ffffff
67///     style E fill:#ffffff,stroke:#ffffff
68/// ```
69#[mel_treatment(
70    generic T ()
71    input stream Stream<T>
72    output start Block<void>
73    output end Block<void>
74    output first Block<T>
75    output last Block<T>
76)]
77pub async fn trigger() {
78    let mut last_value = None;
79
80    if let Ok(mut values) = stream.recv_many().await {
81        let _ = start.send_one_as(()).await;
82        if let Some(val) = values.pop_front() {
83            let _ = first.send_one(val.clone()).await;
84            last_value = Some(val);
85        }
86        if let Some(val) = Into::<VecDeque<Value>>::into(values).pop_back() {
87            last_value = Some(val);
88        }
89
90        let _ = futures::join!(start.close(), first.close());
91    }
92
93    while let Ok(values) = stream.recv_many().await {
94        last_value = Into::<VecDeque<Value>>::into(values).pop_back();
95    }
96
97    let _ = end.send_one_as(()).await;
98    if let Some(val) = last_value {
99        let _ = last.send_one(val).await;
100    }
101
102    // We don't close `end` and `last` explicitly here,
103    // because it would be redundant with boilerplate
104    // implementation of treatments.
105}
106
107/// Check a blocking value.
108///
109/// When `value` block is received, `check` is emitted.
110///
111/// ```mermaid
112/// graph LR
113///     T("check()")
114///     B["〈🟨〉"] -->|value| T
115///         
116///     T -->|check| S["〈🟦〉"]
117///     
118///     style B fill:#ffffff,stroke:#ffffff
119///     style S fill:#ffffff,stroke:#ffffff
120/// ```
121#[mel_treatment(
122    generic T ()
123    input value Block<T>
124    output check Block<void>
125)]
126pub async fn check() {
127    if let Ok(_) = value.recv_one().await {
128        let _ = check.send_one_as(()).await;
129    }
130}
131
132/// Uncheck a blocking value.
133///
134/// When `value` block stream is closed without receiving anything, `uncheck` is emitted.
135///
136/// ```mermaid
137/// graph LR
138///     T("uncheck()")
139///     B["〈🟨〉"] -->|value| T
140///         
141///     T -->|uncheck| S["〈🟦〉"]
142///     
143///     style B fill:#ffffff,stroke:#ffffff
144///     style S fill:#ffffff,stroke:#ffffff
145/// ```
146#[mel_treatment(
147    generic T ()
148    input value Block<T>
149    output uncheck Block<void>
150)]
151pub async fn uncheck() {
152    if let Err(_) = value.recv_one().await {
153        let _ = uncheck.send_one_as(()).await;
154    }
155}
156
157/// Emit a blocking value.
158///
159/// When `trigger` is enabled, `value` is emitted as block.
160///
161/// ```mermaid
162/// graph LR
163///     T("emit(value=🟨)")
164///     B["〈🟦〉"] -->|trigger| T
165///         
166///     T -->|emit| S["〈🟨〉"]
167///     
168///     style B fill:#ffffff,stroke:#ffffff
169///     style S fill:#ffffff,stroke:#ffffff
170/// ```
171#[mel_treatment(
172    generic T ()
173    input trigger Block<void>
174    output emit Block<T>
175)]
176pub async fn emit(value: T) {
177    if let Ok(_) = trigger.recv_one().await {
178        let _ = emit.send_one(value).await;
179    }
180}
181
182/// Stream a blocking value.
183///
184/// ```mermaid
185/// graph LR
186///     T("stream()")
187///     B["〈🟦〉"] -->|block| T
188///         
189///     T -->|stream| S["🟦"]
190///     
191///     
192///     style B fill:#ffffff,stroke:#ffffff
193///     style S fill:#ffffff,stroke:#ffffff
194/// ```
195#[mel_treatment(
196    generic T ()
197    input block Block<T>
198    output stream Stream<T>
199)]
200pub async fn stream() {
201    if let Ok(val) = block.recv_one().await {
202        let _ = stream.send_one(val).await;
203    }
204}
205
206/// Merge two streams.
207///
208/// The two streams are merged without predictible order.
209///
210/// ℹ️ Merge continues as long as `a` or `b` continues too, while the other can be ended.
211///
212/// ```mermaid
213/// graph LR
214///     T("merge()")
215///     A["… 🟦 🟫 …"] -->|a| T
216///     B["… 🟧 🟪 🟨 …"] -->|b| T
217///     
218///
219///     T -->|value| V["… 🟦 🟧 🟪 🟫 🟨 …"]
220///
221///     style V fill:#ffffff,stroke:#ffffff
222///     style A fill:#ffffff,stroke:#ffffff
223///     style B fill:#ffffff,stroke:#ffffff
224/// ```
225#[mel_treatment(
226    generic T ()
227    input a Stream<T>
228    input b Stream<T>
229    output value Stream<T>
230)]
231pub async fn merge() {
232    let xa = async {
233        while let Ok(a) = (&a).recv_many().await {
234            check!(value.send_many(a).await);
235        }
236    }
237    .fuse();
238    let xb = async {
239        while let Ok(b) = (&b).recv_many().await {
240            check!(value.send_many(b).await);
241        }
242    }
243    .fuse();
244
245    pin_mut!(xa, xb);
246
247    loop {
248        select! {
249            () = xa => {},
250            () = xb => {},
251            complete => break,
252        };
253    }
254}
255
256/// Arrange two streams as one.
257///
258/// The two streams are merged using the `select` stream:
259/// - when `true`, value from `a` is used;
260/// - when `false`, value from `b` is used.
261///
262/// ℹ️ No value from either `a` or `b` are discarded, they are used when `select` give turn.
263///
264/// ⚠️ When `select` ends merge terminates without treating the remaining values from `a` and `b`.
265/// When `select` give turn to `a` or `b` while the concerned stream is ended, the merge terminates.
266/// Merge continues as long as `select` and concerned stream does, while the other can be ended.
267///
268/// ```mermaid
269/// graph LR
270///     T("arrange()")
271///     A["… 🟦 🟫 …"] -->|a| T
272///     B["… 🟧 🟪 🟨 …"] -->|b| T
273///     O["… 🟩 🟥 🟥 🟩 🟥 …"] -->|select|T
274///     
275///
276///     T -->|value| V["… 🟦 🟧 🟪 🟫 🟨 …"]
277///
278///     style V fill:#ffffff,stroke:#ffffff
279///     style O fill:#ffffff,stroke:#ffffff
280///     style A fill:#ffffff,stroke:#ffffff
281///     style B fill:#ffffff,stroke:#ffffff
282/// ```
283#[mel_treatment(
284    generic T ()
285    input a Stream<T>
286    input b Stream<T>
287    input select Stream<bool>
288    output value Stream<T>
289)]
290pub async fn arrange() {
291    while let Ok(select) = select.recv_one_as::<bool>().await {
292        let val;
293        if select {
294            if let Ok(v) = a.recv_one().await {
295                val = v;
296            } else {
297                break;
298            }
299        } else {
300            if let Ok(v) = b.recv_one().await {
301                val = v;
302            } else {
303                break;
304            }
305        }
306
307        check!(value.send_one(val).await)
308    }
309}
310
311/// Fill a pattern stream with a `value.
312///
313/// ```mermaid
314/// graph LR
315/// T("fill(value=🟧)")
316/// B["… 🟦 🟦 🟦 …"] -->|pattern| T
317///
318/// T -->|filled| O["… 🟧 🟧 🟧 …"]
319///
320/// style B fill:#ffffff,stroke:#ffffff
321/// style O fill:#ffffff,stroke:#ffffff
322/// ```
323#[mel_treatment(
324    generic T ()
325    input pattern Stream<void>
326    output filled Stream<T>
327)]
328pub async fn fill(value: T) {
329    while let Ok(pat) = pattern.recv_many().await {
330        let mut transmission = melodium_core::TransmissionValue::new(value.clone());
331        for _ in 1..pat.len() {
332            transmission.push(value.clone());
333        }
334        check!(filled.send_many(transmission).await)
335    }
336}
337
338/// Filter a stream according to `bool` stream.
339///
340/// ℹ️ If both streams are not the same size nothing is sent through accepted nor rejected.
341///  
342/// ```mermaid
343/// graph LR
344///     T("filter()")
345///     V["… 🟦 🟧 🟪 🟫 🟨 …"] -->|value| T
346///     D["… 🟩 🟥 🟥 🟩 🟥 …"] -->|select|T
347///     
348///     T -->|accepted| A["… 🟦 🟫 …"]
349///     T -->|rejected| R["… 🟧 🟪 🟨 …"]
350///
351///     style V fill:#ffffff,stroke:#ffffff
352///     style D fill:#ffffff,stroke:#ffffff
353///     style A fill:#ffffff,stroke:#ffffff
354///     style R fill:#ffffff,stroke:#ffffff
355/// ```
356#[mel_treatment(
357    generic T ()
358    input value Stream<T>
359    input select Stream<bool>
360    output accepted Stream<T>
361    output rejected Stream<T>
362)]
363pub async fn filter() {
364    let mut accepted_op = true;
365    let mut rejected_op = true;
366
367    while let (Ok(value), Ok(select)) = futures::join!(value.recv_one(), select.recv_one()) {
368        let select = select.try_data::<bool>().unwrap();
369        if select {
370            if let Err(_) = accepted.send_one(value).await {
371                // If we cannot send anymore on accepted, we note it,
372                // and check if rejected is still valid, else just terminate.
373                accepted_op = false;
374                if !rejected_op {
375                    break;
376                }
377            }
378        } else {
379            if let Err(_) = rejected.send_one(value).await {
380                // If we cannot send anymore on rejected, we note it,
381                // and check if accepted is still valid, else just terminate.
382                rejected_op = false;
383                if !accepted_op {
384                    break;
385                }
386            }
387        }
388    }
389}
390
391/// Filter a block according to `bool` value.
392///
393/// ℹ️ If `select` is never received nothing is emitted.
394///  
395/// ```mermaid
396/// graph LR
397///     T("filterBlock()")
398///     V["〈🟦〉"] -->|value| T
399///     D["〈🟩〉"] -->|select|T
400///     
401///     T -->|accepted| A["〈🟦〉"]
402///     T -->|rejected| R[" "]
403///
404///     style V fill:#ffffff,stroke:#ffffff
405///     style D fill:#ffffff,stroke:#ffffff
406///     style A fill:#ffffff,stroke:#ffffff
407///     style R fill:#ffffff,stroke:#ffffff
408/// ```
409#[mel_treatment(
410    generic T ()
411    input value Block<T>
412    input select Block<bool>
413    output accepted Block<T>
414    output rejected Block<T>
415)]
416pub async fn filterBlock() {
417    if let (Ok(value), Ok(select)) = futures::join!(value.recv_one(), select.recv_one()) {
418        let select = select.try_data::<bool>().unwrap();
419        if select {
420            let _ = accepted.send_one(value).await;
421        } else {
422            let _ = rejected.send_one(value).await;
423        }
424    }
425}
426
427/// Fit a stream into a pattern.
428///
429/// ℹ️ If some remaining values doesn't fit into the pattern, they are trashed.
430///
431/// ```mermaid
432/// graph LR
433///     T("fit()")
434///     A["… 🟨 🟨 🟨 🟨 🟨 🟨"] -->|value| T
435///     B["🟦 🟦 🟦 🟦"] -->|pattern| T
436///     
437///     T -->|fitted| O["🟨 🟨 🟨 🟨"]
438///
439///     style A fill:#ffffff,stroke:#ffffff
440///     style B fill:#ffffff,stroke:#ffffff
441///     style O fill:#ffffff,stroke:#ffffff
442/// ```
443#[mel_treatment(
444    generic T ()
445    input value Stream<T>
446    input pattern Stream<void>
447    output fitted Stream<T>
448)]
449pub async fn fit() {
450    'main: while let Ok(pattern) = pattern.recv_many_as::<()>().await {
451        for _ in pattern {
452            if let Ok(val) = value.recv_one().await {
453                check!('main, fitted.send_one(val).await)
454            } else {
455                break 'main;
456            }
457        }
458    }
459}
460
461/// Gives count of elements passing through stream.
462///
463/// This count increment one for each element within the stream, starting at 1.
464///
465/// ```mermaid
466/// graph LR
467///     T("count()")
468///     V["🟦 🟦 🟦 …"] -->|iter| T
469///     
470///     T -->|count| P["1️⃣ 2️⃣ 3️⃣ …"]
471///
472///     style V fill:#ffffff,stroke:#ffffff
473///     style P fill:#ffffff,stroke:#ffffff
474/// ```
475#[mel_treatment(
476    generic T ()
477    input stream Stream<T>
478    output count Stream<u128>
479)]
480pub async fn count() {
481    let mut i: u128 = 1;
482    while let Ok(iter) = stream.recv_many().await {
483        let next_i = i + iter.len() as u128;
484        check!(count.send_many_as((i..next_i).collect::<Vec<_>>()).await);
485        i = next_i;
486    }
487}
488
489/// Generate a stream with a given length.
490///
491/// ```mermaid
492/// graph LR
493///     T("generate()")
494///     B["〈🟨〉"] -->|length| T
495///         
496///     T -->|stream| S["… 🟦 🟦 🟦 🟦 🟦 🟦"]
497///     
498///     
499///     style B fill:#ffffff,stroke:#ffffff
500///     style S fill:#ffffff,stroke:#ffffff
501/// ```
502#[mel_treatment(
503    generic T ()
504    input length Block<u128>
505    output stream Stream<T>
506)]
507pub async fn generate(data: T) {
508    if let Ok(length) = length.recv_one_as::<u128>().await {
509        const CHUNK: u128 = 2u128.pow(20);
510        let mut total = 0u128;
511        while total < length {
512            let chunk = u128::min(CHUNK, length - total) as usize;
513            let mut transmission = melodium_core::TransmissionValue::new(data.clone());
514            for _ in 1..chunk {
515                transmission.push(data.clone());
516            }
517            check!(stream.send_many(transmission).await);
518            total += chunk as u128;
519        }
520    }
521}
522
523/// Generate a stream indefinitely.
524///
525/// This generates a continuous stream, until stream consumers closes it.
526///
527/// ```mermaid
528/// graph LR
529///     T("generateIndefinitely()")
530///     B["〈🟦〉"] -->|trigger| T
531///         
532///     T -->|stream| S["… 🟦 🟦 🟦 🟦 🟦 🟦"]
533///     
534///     
535///     style B fill:#ffffff,stroke:#ffffff
536///     style S fill:#ffffff,stroke:#ffffff
537/// ```
538#[mel_treatment(
539    generic T ()
540    input trigger Block<void>
541    output stream Stream<T>
542)]
543pub async fn generate_indefinitely(data: T) {
544    if let Ok(_) = trigger.recv_one().await {
545        const CHUNK: usize = 2usize.pow(20);
546        loop {
547            let mut transmission = melodium_core::TransmissionValue::new(data.clone());
548            for _ in 1..CHUNK {
549                transmission.push(data.clone());
550            }
551            check!(stream.send_many(transmission).await);
552        }
553    }
554}
555
556/// Insert a block into a stream.
557///
558/// `block` is inserted into `stream` when it comes and everything is streamed to `output`.
559///
560/// ℹ️ No assumption on block insertion position in stream can be made.
561///
562/// ```mermaid
563/// graph LR
564///     T("insert()")
565///     A["… 🟦 🟦 🟦 🟦 …"] -->|stream| T
566///     B["〈🟧〉"] -->|block| T
567///     
568///
569///     T -->|output| V["… 🟦 🟧 🟦 🟦 🟦 …"]
570///
571///     style V fill:#ffffff,stroke:#ffffff
572///     style A fill:#ffffff,stroke:#ffffff
573///     style B fill:#ffffff,stroke:#ffffff
574/// ```
575#[mel_treatment(
576    generic T ()
577    input stream Stream<T>
578    input block Block<T>
579    output output Stream<T>
580)]
581pub async fn insert() {
582    let streaming = async {
583        while let Ok(values) = (&stream).recv_many().await {
584            check!(output.send_many(values).await);
585        }
586    }
587    .fuse();
588    let insert_block = async {
589        if let Ok(val) = (&block).recv_one().await {
590            let _ = output.send_one(val).await;
591        }
592    }
593    .fuse();
594
595    pin_mut!(streaming, insert_block);
596
597    loop {
598        select! {
599            () = streaming => {},
600            () = insert_block => {},
601            complete => break,
602        };
603    }
604}
605
606/// Merge two incoming blocks as a stream.
607///
608/// Each block is taken when it arrives and send through `stream`.
609///
610/// ℹ️ No priority on blocks order in stream can be assumed.
611///
612/// ```mermaid
613/// graph LR
614///     T("flock()")
615///     A["〈🟦〉"] -->|a| T
616///     B["〈🟧〉"] -->|b| T
617///     
618///
619///     T -->|stream| V["🟧 🟦"]
620///
621///     style V fill:#ffffff,stroke:#ffffff
622///     style A fill:#ffffff,stroke:#ffffff
623///     style B fill:#ffffff,stroke:#ffffff
624/// ```
625#[mel_treatment(
626    generic T ()
627    input a Block<T>
628    input b Block<T>
629    output stream Stream<T>
630)]
631pub async fn flock() {
632    let xa = async {
633        if let Ok(a) = (&a).recv_one().await {
634            let _ = stream.send_one(a).await;
635        }
636    }
637    .fuse();
638    let xb = async {
639        if let Ok(b) = (&b).recv_one().await {
640            let _ = stream.send_one(b).await;
641        }
642    }
643    .fuse();
644
645    pin_mut!(xa, xb);
646
647    loop {
648        select! {
649            () = xa => {},
650            () = xb => {},
651            complete => break,
652        };
653    }
654}
655
656/// Emit one block.
657///
658/// Take first block coming among `a` or `b` and emit it in `value`, ignoring the remaining one.
659///
660/// ℹ️ No priority between blocks can be assumed if they are ready at same moment.
661///
662/// ```mermaid
663/// graph LR
664///     T("one()")
665///     A["〈〉"] -->|a| T
666///     B["〈🟧〉"] -->|b| T
667///     
668///
669///     T -->|value| V["〈🟧〉"]
670///
671///     style V fill:#ffffff,stroke:#ffffff
672///     style A fill:#ffffff,stroke:#ffffff
673///     style B fill:#ffffff,stroke:#ffffff
674/// ```
675#[mel_treatment(
676    generic T ()
677    input a Block<T>
678    input b Block<T>
679    output value Block<T>
680)]
681pub async fn one() {
682    let xa = async { (&a).recv_one().await.ok() }.fuse();
683    let xb = async { (&b).recv_one().await.ok() }.fuse();
684
685    pin_mut!(xa, xb);
686
687    loop {
688        let val = select! {
689            val = xa => val,
690            val = xb => val,
691            complete => break,
692        };
693
694        if let Some(val) = val {
695            let _ = value.send_one(val).await;
696            break;
697        }
698    }
699}
700
701/// Never send any value.
702///
703/// No value is ever sent on `closed` output, which is immediately closed.
704///
705#[mel_treatment(
706    generic T ()
707    input trigger Block<void>
708    output closed Stream<T>
709)]
710pub async fn close() {
711    // Nothing to do
712}
713
714/// Never send any value.
715///
716/// No value is ever sent on `closed` output, which is immediately closed.
717///
718#[mel_treatment(
719    generic T ()
720    input trigger Block<void>
721    output closed Block<T>
722)]
723pub async fn close_block() {
724    // Nothing to do
725}
726
727/// Consume stream indefinitely.
728///
729/// Input `stream` is consumed indefinitely until it becomes closed by previous treatment.
730///
731#[mel_treatment(
732    generic T ()
733    input stream Stream<T>
734)]
735pub async fn consume() {
736    while let Ok(_) = stream.recv_many().await {
737        // Nothing to do.
738    }
739}
740
741/// Pass stream under condition.
742///
743/// If `if` is `true`, pass the stream, else closes it.
744///
745#[mel_treatment(
746    generic T ()
747    input stream Stream<T>
748    output passed Stream<T>
749)]
750pub async fn pass(cond: bool) {
751    if cond {
752        while let Ok(data) = stream.recv_many().await {
753            check!(passed.send_many(data).await)
754        }
755    }
756}
757
758/// Pass block under condition.
759///
760/// If `if` is `true`, pass the block, else nothing.
761///
762#[mel_treatment(
763    generic T ()
764    input block Block<T>
765    output passed Block<T>
766)]
767pub async fn passBlock(cond: bool) {
768    if cond {
769        if let Ok(data) = block.recv_one().await {
770            let _ = passed.send_one(data).await;
771        }
772    }
773}
774
775/// Barrier stream under condition.
776///
777/// Awaits `leverage` to let stream pass if it is `true`, else closes the stream.
778///
779#[mel_treatment(
780    generic T ()
781    input leverage Block<bool>
782    input stream Stream<T>
783    output passed Stream<T>
784)]
785pub async fn barrier() {
786    if let Ok(true) = leverage.recv_one_as::<bool>().await {
787        while let Ok(data) = stream.recv_many().await {
788            check!(passed.send_many(data).await)
789        }
790    }
791}
792
793/// Pass stream until cut signal.
794///
795/// Let stream pass until `cut` signal si received.
796///
797#[mel_treatment(
798    generic T ()
799    input cut Block<void>
800    input stream Stream<T>
801    output passed Stream<T>
802)]
803pub async fn cut() {
804    let cut = async { cut.recv_one().await.is_ok() }.fuse();
805    let pass = async {
806        while let Ok(values) = stream.recv_many().await {
807            check!(passed.send_many(values).await)
808        }
809    }
810    .fuse();
811
812    pin_mut!(cut, pass);
813
814    loop {
815        select! {
816            () = pass => break,
817            do_cut = cut => if do_cut {
818                break
819            },
820            complete => break,
821        }
822    }
823}
824
825/// Release stream once signal is received.
826///
827/// Awaits `leverage` to let stream pass, else closes the stream.
828///
829#[mel_treatment(
830    generic T ()
831    input leverage Block<void>
832    input data Stream<T>
833    output released Stream<T>
834)]
835pub async fn release() {
836    if let Ok(_) = leverage.recv_one().await {
837        while let Ok(data) = data.recv_many().await {
838            check!(released.send_many(data).await)
839        }
840    }
841}
842
843/// Release block once signal is received.
844///
845/// Awaits `leverage` to let block pass, else closes the flow.
846///
847#[mel_treatment(
848    generic T ()
849    input leverage Block<void>
850    input data Block<T>
851    output released Block<T>
852)]
853pub async fn releaseBlock() {
854    if let Ok(_) = leverage.recv_one().await {
855        if let Ok(data) = data.recv_one().await {
856            let _ = released.send_one(data).await;
857        }
858    }
859}
860
861/// Await blocks.
862///
863/// Wait for two blocks and send `awaited` once both are received.
864///
865/// ℹ️ If one block is never received, `awaited` is never emitted.
866#[mel_treatment(
867    generic T ()
868    input a Block<T>
869    input b Block<T>
870    output awaited Block<void>
871)]
872pub async fn waitBlock() {
873    let (a, b) = futures::join!(async { a.recv_one().await.is_ok() }, async {
874        b.recv_one().await.is_ok()
875    });
876
877    if a && b {
878        let _ = awaited.send_one_as(()).await;
879    }
880}