std_mel/ops/
bin.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Logical _and_
5///
6/// Makes _and_ ⋀ binary operation
7#[mel_function(
8    generic B (Binary)
9)]
10pub fn and(a: B, b: B) -> B {
11    a.binary_and(&b)
12}
13
14/// Logical _and_
15///
16/// Makes _and_ ⋀ binary operation between values passed through streams.
17#[mel_treatment(
18    generic B (Binary)
19    input a Stream<B>
20    input b Stream<B>
21    output and Stream<B>
22)]
23pub async fn and() {
24    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
25        check!(and.send_one(a.binary_and(&b)).await)
26    }
27}
28
29/// Logical inclusive _or_
30///
31/// Makes _or_ ⋁ binary operation
32#[mel_function(
33    generic B (Binary)
34)]
35pub fn or(a: B, b: B) -> B {
36    a.binary_or(&b)
37}
38
39/// Logical inclusive _or_.
40///
41/// Makes _or_ ⋁ binary operation between values passed through streams.
42#[mel_treatment(
43    generic B (Binary)
44    input a Stream<B>
45    input b Stream<B>
46    output or Stream<B>
47)]
48pub async fn or() {
49    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
50        check!(or.send_one(a.binary_or(&b)).await)
51    }
52}
53
54/// Logical exclusive _or_
55///
56/// Makes _xor_ ⊕ binary operation
57#[mel_function(
58    generic B (Binary)
59)]
60pub fn xor(a: B, b: B) -> B {
61    a.binary_xor(&b)
62}
63
64/// Logical exclusive _or_.
65///
66/// Makes _xor_ ⊕ binary operation between values passed through streams.
67#[mel_treatment(
68    generic B (Binary)
69    input a Stream<B>
70    input b Stream<B>
71    output or Stream<B>
72)]
73pub async fn xor() {
74    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
75        check!(or.send_one(a.binary_xor(&b)).await)
76    }
77}
78
79/// Inverse binary value.
80///
81/// Apply _not_ ¬ binary operation.
82#[mel_function(
83    generic B (Binary)
84)]
85pub fn not(value: B) -> B {
86    value.binary_not()
87}
88
89/// Inverse binary values of a stream.
90///
91/// Apply _not_ ¬ binary operation on all values in the stream.
92#[mel_treatment(
93    generic B (Binary)
94    input value Stream<B>
95    output not Stream<B>
96)]
97pub async fn not() {
98    while let Ok(values) = value
99        .recv_many()
100        .await
101        .map(|values| Into::<VecDeque<Value>>::into(values))
102    {
103        check!(
104            not.send_many(TransmissionValue::Other(
105                values.into_iter().map(|val| val.binary_not()).collect()
106            ))
107            .await
108        )
109    }
110}