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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};

/// Turns `u16` stream into `void` one.
#[mel_treatment(
    input value Stream<u16>
    output iter Stream<void>
)]
pub async fn to_void() {
    while let Ok(values) = value.recv_u16().await {
        check!(iter.send_void(vec![(); values.len()]).await)
    }
}

/// Turns `u16` into `Vec<byte>`.
#[mel_function]
pub fn to_byte(value: u16) -> Vec<byte> {
    value.to_be_bytes().to_vec()
}

/// Turns `u16` stream into `byte` one.
///
/// Each `u16` gets converted into `Vec<byte>`, with each vector containing the `bytes`s of the former scalar `u16` it represents.
#[mel_treatment(
    input value Stream<u16>
    output data Stream<Vec<byte>>
)]
pub async fn to_byte() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            data.send_vec_byte(
                values
                    .into_iter()
                    .map(|val| val.to_be_bytes().to_vec())
                    .collect()
            )
            .await
        )
    }
}

/// Turns `u16` into `u32`.
///
/// This conversion is lossless, as any `u16` value can fit into a `u32`.
#[mel_function]
pub fn to_u32(value: u16) -> u32 {
    value as u32
}

/// Turns `u16` stream into `u32` one.
///
/// Each `u16` gets converted into `u32`.
/// This conversion is lossless, as any `u16` value can fit into a `u32`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<u32>
)]
pub async fn to_u32() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_u32(values.into_iter().map(|val| val as u32).collect())
                .await
        )
    }
}

/// Turns `u16` into `u64`.
///
/// This conversion is lossless, as any `u16` value can fit into a `u64`.
#[mel_function]
pub fn to_u64(value: u16) -> u64 {
    value as u64
}

/// Turns `u16` stream into `u64` one.
///
/// Each `u16` gets converted into `u64`.
/// This conversion is lossless, as any `u16` value can fit into a `u64`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<u64>
)]
pub async fn to_u64() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_u64(values.into_iter().map(|val| val as u64).collect())
                .await
        )
    }
}

/// Turns `u16` into `u128`.
///
/// This conversion is lossless, as any `u16` value can fit into a `u128`.
#[mel_function]
pub fn to_u128(value: u16) -> u128 {
    value as u128
}

/// Turns `u16` stream into `u128` one.
///
/// Each `u16` gets converted into `u128`.
/// This conversion is lossless, as any `u16` value can fit into a `u128`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<u128>
)]
pub async fn to_u128() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_u128(values.into_iter().map(|val| val as u128).collect())
                .await
        )
    }
}

/// Turns `u16` into `i32`.
///
/// This conversion is lossless, as any `u16` value can fit into a `i32`.
#[mel_function]
pub fn to_i32(value: u16) -> i32 {
    value as i32
}

/// Turns `u16` stream into `i32` one.
///
/// Each `u16` gets converted into `i32`.
/// This conversion is lossless, as any `u16` value can fit into a `i32`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<i32>
)]
pub async fn to_i32() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_i32(values.into_iter().map(|val| val as i32).collect())
                .await
        )
    }
}

/// Turns `u16` into `i64`.
///
/// This conversion is lossless, as any `u16` value can fit into a `i64`.
#[mel_function]
pub fn to_i64(value: u16) -> i64 {
    value as i64
}

/// Turns `u16` stream into `i64` one.
///
/// Each `u16` gets converted into `i64`.
/// This conversion is lossless, as any `u16` value can fit into a `i64`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<i64>
)]
pub async fn to_i64() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_i64(values.into_iter().map(|val| val as i64).collect())
                .await
        )
    }
}

/// Turns `u16` into `i128`.
///
/// This conversion is lossless, as any `u16` value can fit into a `i128`.
#[mel_function]
pub fn to_i128(value: u16) -> i128 {
    value as i128
}

/// Turns `u16` stream into `i128` one.
///
/// Each `u16` gets converted into `i128`.
/// This conversion is lossless, as any `u16` value can fit into a `i128`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<i128>
)]
pub async fn to_i128() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_i128(values.into_iter().map(|val| val as i128).collect())
                .await
        )
    }
}

/// Turns `u16` into `f32`.
///
/// This conversion is lossless, as any `u16` value can fit into a `f32`.
#[mel_function]
pub fn to_f32(value: u16) -> f32 {
    value as f32
}

/// Turns `u16` stream into `f32` one.
///
/// Each `u16` gets converted into `f32`.
/// This conversion is lossless, as any `u16` value can fit into a `f32`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<f32>
)]
pub async fn to_f32() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_f32(values.into_iter().map(|val| val as f32).collect())
                .await
        )
    }
}

/// Turns `u16` into `f64`.
///
/// This conversion is lossless, as any `u16` value can fit into a `f64`.
#[mel_function]
pub fn to_f64(value: u16) -> f64 {
    value as f64
}

/// Turns `u16` stream into `f64` one.
///
/// Each `u16` gets converted into `f64`.
/// This conversion is lossless, as any `u16` value can fit into a `f64`.
#[mel_treatment(
    input value Stream<u16>
    output into Stream<f64>
)]
pub async fn to_f64() {
    while let Ok(values) = value.recv_u16().await {
        check!(
            into.send_f64(values.into_iter().map(|val| val as f64).collect())
                .await
        )
    }
}

/// Turns `u16` into `u8`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `u8`),
/// `truncate` allows value to be truncated to fit into a `u8`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `u8` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_function]
pub fn to_u8(value: u16, truncate: bool, or_default: u8) -> u8 {
    if truncate {
        value as u8
    } else {
        use std::convert::TryInto;
        TryInto::<u8>::try_into(value).unwrap_or(or_default)
    }
}

/// Convert stream of `u16` into `u8`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `u8`),
/// `truncate` allows value to be truncated to fit into a `u8`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `u8` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_treatment(
    default truncate true
    default or_default 0
    input value Stream<u16>
    output into Stream<u8>
)]
pub async fn to_u8(truncate: bool, or_default: u8) {
    if truncate {
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_u8(values.into_iter().map(|val| val as u8).collect())
                    .await
            )
        }
    } else {
        use std::convert::TryInto;
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_u8(
                    values
                        .into_iter()
                        .map(|val| TryInto::<u8>::try_into(val).unwrap_or(or_default))
                        .collect()
                )
                .await
            )
        }
    }
}

/// Turns `u16` into `i8`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `i8`),
/// `truncate` allows value to be truncated to fit into a `i8`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `i8` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_function]
pub fn to_i8(value: u16, truncate: bool, or_default: i8) -> i8 {
    if truncate {
        value as i8
    } else {
        use std::convert::TryInto;
        TryInto::<i8>::try_into(value).unwrap_or(or_default)
    }
}

/// Convert stream of `u16` into `i8`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `i8`),
/// `truncate` allows value to be truncated to fit into a `i8`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `i8` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_treatment(
    default truncate true
    default or_default 0
    input value Stream<u16>
    output into Stream<i8>
)]
pub async fn to_i8(truncate: bool, or_default: i8) {
    if truncate {
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_i8(values.into_iter().map(|val| val as i8).collect())
                    .await
            )
        }
    } else {
        use std::convert::TryInto;
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_i8(
                    values
                        .into_iter()
                        .map(|val| TryInto::<i8>::try_into(val).unwrap_or(or_default))
                        .collect()
                )
                .await
            )
        }
    }
}

/// Turns `u16` into `i16`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `i16`),
/// `truncate` allows value to be truncated to fit into a `i16`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `i16` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_function]
pub fn to_i16(value: u16, truncate: bool, or_default: i16) -> i16 {
    if truncate {
        value as i16
    } else {
        use std::convert::TryInto;
        TryInto::<i16>::try_into(value).unwrap_or(or_default)
    }
}

/// Convert stream of `u16` into `i16`.
///
/// As this conversion might be lossy (every possible `u16` value cannot fit into `i16`),
/// `truncate` allows value to be truncated to fit into a `i16`, and `or_default` set the
/// value that is assigned when a `u16` is out of range for `i16` and truncation not allowed.
///
/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
///
#[mel_treatment(
    default truncate true
    default or_default 0
    input value Stream<u16>
    output into Stream<i16>
)]
pub async fn to_i16(truncate: bool, or_default: i16) {
    if truncate {
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_i16(values.into_iter().map(|val| val as i16).collect())
                    .await
            )
        }
    } else {
        use std::convert::TryInto;
        while let Ok(values) = value.recv_u16().await {
            check!(
                into.send_i16(
                    values
                        .into_iter()
                        .map(|val| TryInto::<i16>::try_into(val).unwrap_or(or_default))
                        .collect()
                )
                .await
            )
        }
    }
}