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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};

/// Return whether `a` is equal to `b`
#[mel_function]
pub fn equal(a: byte, b: byte) -> bool {
    a == b
}

/// Return whether `a` is different `b`
#[mel_function]
pub fn not_equal(a: byte, b: byte) -> bool {
    a != b
}

/// Determine whether `a` is equal to `b`
#[mel_treatment(
    input a Stream<byte>
    input b Stream<byte>
    output result Stream<bool>
)]
pub async fn equal() {
    while let (Ok(a), Ok(b)) = (a.recv_one_byte().await, b.recv_one_byte().await) {
        check!(result.send_one_bool(a == b).await)
    }
}

/// Determine whether `a` is different from `b`
#[mel_treatment(
    input a Stream<byte>
    input b Stream<byte>
    output result Stream<bool>
)]
pub async fn not_equal() {
    while let (Ok(a), Ok(b)) = (a.recv_one_byte().await, b.recv_one_byte().await) {
        check!(result.send_one_bool(a != b).await)
    }
}

/// Makes _and_ ⋀ binary operation on `byte`
#[mel_function]
pub fn and(a: byte, b: byte) -> byte {
    a & b
}

/// Makes _or_ ⋁ binary operation on `byte`
#[mel_function]
pub fn or(a: byte, b: byte) -> byte {
    a | b
}

/// Makes _xor_ ⊕ binary operation on `byte`
#[mel_function]
pub fn xor(a: byte, b: byte) -> byte {
    a ^ b
}

/// Makes _not_ ¬ binary operation on `byte`
#[mel_function]
pub fn not(val: byte) -> byte {
    !val
}

/// Makes _and_ ⋀ binary operation on `byte`
#[mel_treatment(
    input a Stream<byte>
    input b Stream<byte>
    output result Stream<byte>
)]
pub async fn and() {
    while let (Ok(a), Ok(b)) = (a.recv_one_byte().await, b.recv_one_byte().await) {
        check!(result.send_one_byte(a & b).await)
    }
}

/// Makes _or_ ⋁ binary operation on `byte`
#[mel_treatment(
    input a Stream<byte>
    input b Stream<byte>
    output result Stream<byte>
)]
pub async fn or() {
    while let (Ok(a), Ok(b)) = (a.recv_one_byte().await, b.recv_one_byte().await) {
        check!(result.send_one_byte(a | b).await)
    }
}

/// Makes _xor_ ⊕ binary operation on `byte`
#[mel_treatment(
    input a Stream<byte>
    input b Stream<byte>
    output result Stream<byte>
)]
pub async fn xor() {
    while let (Ok(a), Ok(b)) = (a.recv_one_byte().await, b.recv_one_byte().await) {
        check!(result.send_one_byte(a ^ b).await)
    }
}

/// Makes _not_ ¬ binary operation on `byte`
#[mel_treatment(
    input value Stream<byte>
    output not Stream<byte>
)]
pub async fn not() {
    while let Ok(values) = value.recv_byte().await {
        check!(
            not.send_byte(values.into_iter().map(|v| !v).collect())
                .await
        )
    }
}