conv_mel/i128.rs
1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Turns `i128` stream into `void` one.
5#[mel_treatment(
6 input value Stream<i128>
7 output iter Stream<void>
8)]
9pub async fn to_void() {
10 while let Ok(values) = value.recv_i128().await {
11 check!(iter.send_void(vec![(); values.len()]).await)
12 }
13}
14
15/// Turns `i128` into `Vec<byte>`.
16#[mel_function]
17pub fn to_byte(value: i128) -> Vec<byte> {
18 value.to_be_bytes().to_vec()
19}
20
21/// Turns `i128` stream into `byte` one.
22///
23/// Each `i128` gets converted into `Vec<byte>`, with each vector containing the `bytes`s of the former scalar `i128` it represents.
24#[mel_treatment(
25 input value Stream<i128>
26 output data Stream<Vec<byte>>
27)]
28pub async fn to_byte() {
29 while let Ok(values) = value.recv_i128().await {
30 check!(
31 data.send_vec_byte(
32 values
33 .into_iter()
34 .map(|val| val.to_be_bytes().to_vec())
35 .collect()
36 )
37 .await
38 )
39 }
40}
41
42/// Turns `i128` into `f32`.
43///
44/// This conversion is lossless, as any `i128` value can fit into a `f32`.
45#[mel_function]
46pub fn to_f32(value: i128) -> f32 {
47 value as f32
48}
49
50/// Turns `i128` stream into `f32` one.
51///
52/// Each `i128` gets converted into `f32`.
53/// This conversion is lossless, as any `i128` value can fit into a `f32`.
54#[mel_treatment(
55 input value Stream<i128>
56 output into Stream<f32>
57)]
58pub async fn to_f32() {
59 while let Ok(values) = value.recv_i128().await {
60 check!(
61 into.send_f32(values.into_iter().map(|val| val as f32).collect())
62 .await
63 )
64 }
65}
66
67/// Turns `i128` into `f64`.
68///
69/// This conversion is lossless, as any `i128` value can fit into a `f64`.
70#[mel_function]
71pub fn to_f64(value: i128) -> f64 {
72 value as f64
73}
74
75/// Turns `i128` stream into `f64` one.
76///
77/// Each `i128` gets converted into `f64`.
78/// This conversion is lossless, as any `i128` value can fit into a `f64`.
79#[mel_treatment(
80 input value Stream<i128>
81 output into Stream<f64>
82)]
83pub async fn to_f64() {
84 while let Ok(values) = value.recv_i128().await {
85 check!(
86 into.send_f64(values.into_iter().map(|val| val as f64).collect())
87 .await
88 )
89 }
90}
91
92/// Turns `i128` into `u8`.
93///
94/// As this conversion might be lossy (every possible `i128` value cannot fit into `u8`),
95/// `truncate` allows value to be truncated to fit into a `u8`, and `or_default` set the
96/// value that is assigned when a `i128` is out of range for `u8` and truncation not allowed.
97///
98/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
99///
100#[mel_function]
101pub fn to_u8(value: i128, truncate: bool, or_default: u8) -> u8 {
102 if truncate {
103 value as u8
104 } else {
105 use std::convert::TryInto;
106 TryInto::<u8>::try_into(value).unwrap_or(or_default)
107 }
108}
109
110/// Convert stream of `i128` into `u8`.
111///
112/// As this conversion might be lossy (every possible `i128` value cannot fit into `u8`),
113/// `truncate` allows value to be truncated to fit into a `u8`, and `or_default` set the
114/// value that is assigned when a `i128` is out of range for `u8` and truncation not allowed.
115///
116/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
117///
118#[mel_treatment(
119 default truncate true
120 default or_default 0
121 input value Stream<i128>
122 output into Stream<u8>
123)]
124pub async fn to_u8(truncate: bool, or_default: u8) {
125 if truncate {
126 while let Ok(values) = value.recv_i128().await {
127 check!(
128 into.send_u8(values.into_iter().map(|val| val as u8).collect())
129 .await
130 )
131 }
132 } else {
133 use std::convert::TryInto;
134 while let Ok(values) = value.recv_i128().await {
135 check!(
136 into.send_u8(
137 values
138 .into_iter()
139 .map(|val| TryInto::<u8>::try_into(val).unwrap_or(or_default))
140 .collect()
141 )
142 .await
143 )
144 }
145 }
146}
147
148/// Turns `i128` into `u16`.
149///
150/// As this conversion might be lossy (every possible `i128` value cannot fit into `u16`),
151/// `truncate` allows value to be truncated to fit into a `u16`, and `or_default` set the
152/// value that is assigned when a `i128` is out of range for `u16` and truncation not allowed.
153///
154/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
155///
156#[mel_function]
157pub fn to_u16(value: i128, truncate: bool, or_default: u16) -> u16 {
158 if truncate {
159 value as u16
160 } else {
161 use std::convert::TryInto;
162 TryInto::<u16>::try_into(value).unwrap_or(or_default)
163 }
164}
165
166/// Convert stream of `i128` into `u16`.
167///
168/// As this conversion might be lossy (every possible `i128` value cannot fit into `u16`),
169/// `truncate` allows value to be truncated to fit into a `u16`, and `or_default` set the
170/// value that is assigned when a `i128` is out of range for `u16` and truncation not allowed.
171///
172/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
173///
174#[mel_treatment(
175 default truncate true
176 default or_default 0
177 input value Stream<i128>
178 output into Stream<u16>
179)]
180pub async fn to_u16(truncate: bool, or_default: u16) {
181 if truncate {
182 while let Ok(values) = value.recv_i128().await {
183 check!(
184 into.send_u16(values.into_iter().map(|val| val as u16).collect())
185 .await
186 )
187 }
188 } else {
189 use std::convert::TryInto;
190 while let Ok(values) = value.recv_i128().await {
191 check!(
192 into.send_u16(
193 values
194 .into_iter()
195 .map(|val| TryInto::<u16>::try_into(val).unwrap_or(or_default))
196 .collect()
197 )
198 .await
199 )
200 }
201 }
202}
203
204/// Turns `i128` into `u32`.
205///
206/// As this conversion might be lossy (every possible `i128` value cannot fit into `u32`),
207/// `truncate` allows value to be truncated to fit into a `u32`, and `or_default` set the
208/// value that is assigned when a `i128` is out of range for `u32` and truncation not allowed.
209///
210/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
211///
212#[mel_function]
213pub fn to_u32(value: i128, truncate: bool, or_default: u32) -> u32 {
214 if truncate {
215 value as u32
216 } else {
217 use std::convert::TryInto;
218 TryInto::<u32>::try_into(value).unwrap_or(or_default)
219 }
220}
221
222/// Convert stream of `i128` into `u32`.
223///
224/// As this conversion might be lossy (every possible `i128` value cannot fit into `u32`),
225/// `truncate` allows value to be truncated to fit into a `u32`, and `or_default` set the
226/// value that is assigned when a `i128` is out of range for `u32` and truncation not allowed.
227///
228/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
229///
230#[mel_treatment(
231 default truncate true
232 default or_default 0
233 input value Stream<i128>
234 output into Stream<u32>
235)]
236pub async fn to_u32(truncate: bool, or_default: u32) {
237 if truncate {
238 while let Ok(values) = value.recv_i128().await {
239 check!(
240 into.send_u32(values.into_iter().map(|val| val as u32).collect())
241 .await
242 )
243 }
244 } else {
245 use std::convert::TryInto;
246 while let Ok(values) = value.recv_i128().await {
247 check!(
248 into.send_u32(
249 values
250 .into_iter()
251 .map(|val| TryInto::<u32>::try_into(val).unwrap_or(or_default))
252 .collect()
253 )
254 .await
255 )
256 }
257 }
258}
259
260/// Turns `i128` into `u64`.
261///
262/// As this conversion might be lossy (every possible `i128` value cannot fit into `u64`),
263/// `truncate` allows value to be truncated to fit into a `u64`, and `or_default` set the
264/// value that is assigned when a `i128` is out of range for `u64` and truncation not allowed.
265///
266/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
267///
268#[mel_function]
269pub fn to_u64(value: i128, truncate: bool, or_default: u64) -> u64 {
270 if truncate {
271 value as u64
272 } else {
273 use std::convert::TryInto;
274 TryInto::<u64>::try_into(value).unwrap_or(or_default)
275 }
276}
277
278/// Convert stream of `i128` into `u64`.
279///
280/// As this conversion might be lossy (every possible `i128` value cannot fit into `u64`),
281/// `truncate` allows value to be truncated to fit into a `u64`, and `or_default` set the
282/// value that is assigned when a `i128` is out of range for `u64` and truncation not allowed.
283///
284/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
285///
286#[mel_treatment(
287 default truncate true
288 default or_default 0
289 input value Stream<i128>
290 output into Stream<u64>
291)]
292pub async fn to_u64(truncate: bool, or_default: u64) {
293 if truncate {
294 while let Ok(values) = value.recv_i128().await {
295 check!(
296 into.send_u64(values.into_iter().map(|val| val as u64).collect())
297 .await
298 )
299 }
300 } else {
301 use std::convert::TryInto;
302 while let Ok(values) = value.recv_i128().await {
303 check!(
304 into.send_u64(
305 values
306 .into_iter()
307 .map(|val| TryInto::<u64>::try_into(val).unwrap_or(or_default))
308 .collect()
309 )
310 .await
311 )
312 }
313 }
314}
315
316/// Turns `i128` into `u128`.
317///
318/// As this conversion might be lossy (every possible `i128` value cannot fit into `u128`),
319/// `truncate` allows value to be truncated to fit into a `u128`, and `or_default` set the
320/// value that is assigned when a `i128` is out of range for `u128` and truncation not allowed.
321///
322/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
323///
324#[mel_function]
325pub fn to_u128(value: i128, truncate: bool, or_default: u128) -> u128 {
326 if truncate {
327 value as u128
328 } else {
329 use std::convert::TryInto;
330 TryInto::<u128>::try_into(value).unwrap_or(or_default)
331 }
332}
333
334/// Convert stream of `i128` into `u128`.
335///
336/// As this conversion might be lossy (every possible `i128` value cannot fit into `u128`),
337/// `truncate` allows value to be truncated to fit into a `u128`, and `or_default` set the
338/// value that is assigned when a `i128` is out of range for `u128` and truncation not allowed.
339///
340/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
341///
342#[mel_treatment(
343 default truncate true
344 default or_default 0
345 input value Stream<i128>
346 output into Stream<u128>
347)]
348pub async fn to_u128(truncate: bool, or_default: u128) {
349 if truncate {
350 while let Ok(values) = value.recv_i128().await {
351 check!(
352 into.send_u128(values.into_iter().map(|val| val as u128).collect())
353 .await
354 )
355 }
356 } else {
357 use std::convert::TryInto;
358 while let Ok(values) = value.recv_i128().await {
359 check!(
360 into.send_u128(
361 values
362 .into_iter()
363 .map(|val| TryInto::<u128>::try_into(val).unwrap_or(or_default))
364 .collect()
365 )
366 .await
367 )
368 }
369 }
370}
371
372/// Turns `i128` into `i8`.
373///
374/// As this conversion might be lossy (every possible `i128` value cannot fit into `i8`),
375/// `truncate` allows value to be truncated to fit into a `i8`, and `or_default` set the
376/// value that is assigned when a `i128` is out of range for `i8` and truncation not allowed.
377///
378/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
379///
380#[mel_function]
381pub fn to_i8(value: i128, truncate: bool, or_default: i8) -> i8 {
382 if truncate {
383 value as i8
384 } else {
385 use std::convert::TryInto;
386 TryInto::<i8>::try_into(value).unwrap_or(or_default)
387 }
388}
389
390/// Convert stream of `i128` into `i8`.
391///
392/// As this conversion might be lossy (every possible `i128` value cannot fit into `i8`),
393/// `truncate` allows value to be truncated to fit into a `i8`, and `or_default` set the
394/// value that is assigned when a `i128` is out of range for `i8` and truncation not allowed.
395///
396/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
397///
398#[mel_treatment(
399 default truncate true
400 default or_default 0
401 input value Stream<i128>
402 output into Stream<i8>
403)]
404pub async fn to_i8(truncate: bool, or_default: i8) {
405 if truncate {
406 while let Ok(values) = value.recv_i128().await {
407 check!(
408 into.send_i8(values.into_iter().map(|val| val as i8).collect())
409 .await
410 )
411 }
412 } else {
413 use std::convert::TryInto;
414 while let Ok(values) = value.recv_i128().await {
415 check!(
416 into.send_i8(
417 values
418 .into_iter()
419 .map(|val| TryInto::<i8>::try_into(val).unwrap_or(or_default))
420 .collect()
421 )
422 .await
423 )
424 }
425 }
426}
427
428/// Turns `i128` into `i16`.
429///
430/// As this conversion might be lossy (every possible `i128` value cannot fit into `i16`),
431/// `truncate` allows value to be truncated to fit into a `i16`, and `or_default` set the
432/// value that is assigned when a `i128` is out of range for `i16` and truncation not allowed.
433///
434/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
435///
436#[mel_function]
437pub fn to_i16(value: i128, truncate: bool, or_default: i16) -> i16 {
438 if truncate {
439 value as i16
440 } else {
441 use std::convert::TryInto;
442 TryInto::<i16>::try_into(value).unwrap_or(or_default)
443 }
444}
445
446/// Convert stream of `i128` into `i16`.
447///
448/// As this conversion might be lossy (every possible `i128` value cannot fit into `i16`),
449/// `truncate` allows value to be truncated to fit into a `i16`, and `or_default` set the
450/// value that is assigned when a `i128` is out of range for `i16` and truncation not allowed.
451///
452/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
453///
454#[mel_treatment(
455 default truncate true
456 default or_default 0
457 input value Stream<i128>
458 output into Stream<i16>
459)]
460pub async fn to_i16(truncate: bool, or_default: i16) {
461 if truncate {
462 while let Ok(values) = value.recv_i128().await {
463 check!(
464 into.send_i16(values.into_iter().map(|val| val as i16).collect())
465 .await
466 )
467 }
468 } else {
469 use std::convert::TryInto;
470 while let Ok(values) = value.recv_i128().await {
471 check!(
472 into.send_i16(
473 values
474 .into_iter()
475 .map(|val| TryInto::<i16>::try_into(val).unwrap_or(or_default))
476 .collect()
477 )
478 .await
479 )
480 }
481 }
482}
483
484/// Turns `i128` into `i32`.
485///
486/// As this conversion might be lossy (every possible `i128` value cannot fit into `i32`),
487/// `truncate` allows value to be truncated to fit into a `i32`, and `or_default` set the
488/// value that is assigned when a `i128` is out of range for `i32` and truncation not allowed.
489///
490/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
491///
492#[mel_function]
493pub fn to_i32(value: i128, truncate: bool, or_default: i32) -> i32 {
494 if truncate {
495 value as i32
496 } else {
497 use std::convert::TryInto;
498 TryInto::<i32>::try_into(value).unwrap_or(or_default)
499 }
500}
501
502/// Convert stream of `i128` into `i32`.
503///
504/// As this conversion might be lossy (every possible `i128` value cannot fit into `i32`),
505/// `truncate` allows value to be truncated to fit into a `i32`, and `or_default` set the
506/// value that is assigned when a `i128` is out of range for `i32` and truncation not allowed.
507///
508/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
509///
510#[mel_treatment(
511 default truncate true
512 default or_default 0
513 input value Stream<i128>
514 output into Stream<i32>
515)]
516pub async fn to_i32(truncate: bool, or_default: i32) {
517 if truncate {
518 while let Ok(values) = value.recv_i128().await {
519 check!(
520 into.send_i32(values.into_iter().map(|val| val as i32).collect())
521 .await
522 )
523 }
524 } else {
525 use std::convert::TryInto;
526 while let Ok(values) = value.recv_i128().await {
527 check!(
528 into.send_i32(
529 values
530 .into_iter()
531 .map(|val| TryInto::<i32>::try_into(val).unwrap_or(or_default))
532 .collect()
533 )
534 .await
535 )
536 }
537 }
538}
539
540/// Turns `i128` into `i64`.
541///
542/// As this conversion might be lossy (every possible `i128` value cannot fit into `i64`),
543/// `truncate` allows value to be truncated to fit into a `i64`, and `or_default` set the
544/// value that is assigned when a `i128` is out of range for `i64` and truncation not allowed.
545///
546/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
547///
548#[mel_function]
549pub fn to_i64(value: i128, truncate: bool, or_default: i64) -> i64 {
550 if truncate {
551 value as i64
552 } else {
553 use std::convert::TryInto;
554 TryInto::<i64>::try_into(value).unwrap_or(or_default)
555 }
556}
557
558/// Convert stream of `i128` into `i64`.
559///
560/// As this conversion might be lossy (every possible `i128` value cannot fit into `i64`),
561/// `truncate` allows value to be truncated to fit into a `i64`, and `or_default` set the
562/// value that is assigned when a `i128` is out of range for `i64` and truncation not allowed.
563///
564/// Truncation happens on the binary level, thus: `10010110` (150 if unsigned, -106 if [signed](https://en.wikipedia.org/wiki/Signed_number_representations)) → `0110` (6).
565///
566#[mel_treatment(
567 default truncate true
568 default or_default 0
569 input value Stream<i128>
570 output into Stream<i64>
571)]
572pub async fn to_i64(truncate: bool, or_default: i64) {
573 if truncate {
574 while let Ok(values) = value.recv_i128().await {
575 check!(
576 into.send_i64(values.into_iter().map(|val| val as i64).collect())
577 .await
578 )
579 }
580 } else {
581 use std::convert::TryInto;
582 while let Ok(values) = value.recv_i128().await {
583 check!(
584 into.send_i64(
585 values
586 .into_iter()
587 .map(|val| TryInto::<i64>::try_into(val).unwrap_or(or_default))
588 .collect()
589 )
590 .await
591 )
592 }
593 }
594}