Skip to main content

std_mel/
conv.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Turns any data into `void`.
5#[mel_function(
6    generic T ()
7)]
8pub fn to_void(_value: T) -> void {
9    ()
10}
11
12/// Turns any stream into `void` one.
13#[mel_treatment(
14    generic T ()
15    input value Stream<T>
16    output iter Stream<void>
17)]
18pub async fn to_void() {
19    while let Ok(values) = value.recv_many().await {
20        check!(iter.send_many(vec![(); values.len()].into()).await)
21    }
22}
23
24/// Turns data into `Vec<byte>`.
25///
26/// Data element gets converted into `Vec<byte>`, with vector containing the binary form of data it represents.
27///
28/// ℹ️ While this conversion is infaillible, resulting vector may be empty.
29/// Content format and length of vector is totally dependent on data type given, and might not be constant (like for `char` or `string` types).
30#[mel_function(
31    generic T ()
32)]
33pub fn to_bytes(value: T) -> Vec<byte> {
34    fn to_bytes(value: T) -> Vec<byte> {
35        match value {
36            Value::Void(_) => Vec::new(),
37            Value::I8(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
38            Value::I16(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
39            Value::I32(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
40            Value::I64(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
41            Value::I128(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
42            Value::U8(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
43            Value::U16(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
44            Value::U32(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
45            Value::U64(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
46            Value::U128(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
47            Value::F32(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
48            Value::F64(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
49            Value::Bool(val) => match val {
50                true => vec![1u8],
51                false => vec![0u8],
52            },
53            Value::Byte(val) => val.to_be_bytes().iter().map(|v| *v).collect(),
54            Value::Char(val) => val.to_string().as_bytes().iter().map(|v| *v).collect(),
55            Value::String(val) => val.as_bytes().iter().map(|v| *v).collect(),
56            Value::Vec(_vals) => Vec::new(),
57            // Same convention as `Value::Vec` just above: an aggregate value has no
58            // single well-defined flat byte representation, so it maps to an empty
59            // buffer. `Packed` is purely an internal representation choice for a
60            // `Vec<T>`-shaped value, not a distinct case semantically.
61            Value::Packed(_arr) => Vec::new(),
62            Value::Option(val) => match val {
63                Some(val) => to_bytes(*val),
64                None => Vec::new(),
65            },
66            Value::Data(_) => Vec::new(),
67        }
68    }
69    to_bytes(value)
70}
71
72/// Turns data stream into `Vec<byte>` one.
73///
74/// Each data element gets converted into `Vec<byte>`, with each vector containing the binary form of data it represents.
75///
76/// ℹ️ While this conversion is infaillible, resulting vector may be empty.
77/// Content format and length of each vector is totally dependent on data type given, and might not be constant (like for `char` or `string` types).
78#[mel_treatment(
79    generic T ()
80    input value Stream<T>
81    output data Stream<Vec<byte>>
82)]
83pub async fn to_bytes() {
84    while let Ok(values) = value
85        .recv_many()
86        .await
87        .map(|values| Into::<VecDeque<Value>>::into(values))
88    {
89        check!(
90            data.send_many(TransmissionValue::Other(
91                values.into_iter().map(|val| value_to_byte(val)).collect()
92            ))
93            .await
94        )
95    }
96}
97
98/// Converts any value into byte equivalent.
99fn value_to_byte(value: Value) -> Value {
100    match value {
101        Value::Void(_) => Value::Vec(Vec::new()),
102        Value::I8(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
103        Value::I16(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
104        Value::I32(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
105        Value::I64(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
106        Value::I128(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
107        Value::U8(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
108        Value::U16(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
109        Value::U32(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
110        Value::U64(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
111        Value::U128(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
112        Value::F32(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
113        Value::F64(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
114        Value::Bool(val) => Value::Vec(match val {
115            true => vec![Value::Byte(1)],
116            false => vec![Value::Byte(0)],
117        }),
118        Value::Byte(val) => Value::Vec(val.to_be_bytes().iter().map(|v| Value::Byte(*v)).collect()),
119        Value::Char(val) => Value::Vec(
120            val.to_string()
121                .as_bytes()
122                .iter()
123                .map(|v| Value::Byte(*v))
124                .collect(),
125        ),
126        Value::String(val) => Value::Vec(val.as_bytes().iter().map(|v| Value::Byte(*v)).collect()),
127        Value::Vec(vals) => Value::Vec(vals.into_iter().map(|val| value_to_byte(val)).collect()),
128        // Mirrors the `Value::Vec` arm above: expand and recurse per element.
129        // `Packed` is purely an internal representation choice for the same
130        // `Vec<T>`-shaped value, not a distinct case semantically.
131        Value::Packed(arr) => Value::Vec(
132            arr.into_values()
133                .into_iter()
134                .map(|val| value_to_byte(val))
135                .collect(),
136        ),
137        Value::Option(val) => match val {
138            Some(val) => value_to_byte(*val),
139            None => Value::Vec(Vec::new()),
140        },
141        Value::Data(_) => Value::Vec(Vec::new()),
142    }
143}
144
145/// Turns data into `i8`.
146#[mel_function(
147    generic T (ToI8)
148)]
149pub fn to_i8(value: T) -> i8 {
150    value.to_i8()
151}
152
153/// Turns stream into `i8` one.
154///
155/// This treatment manages infaillible conversions to `i8` data type.
156#[mel_treatment(
157    generic T (ToI8)
158    input value Stream<T>
159    output into Stream<i8>
160)]
161pub async fn to_i8() {
162    while let Ok(values) = value
163        .recv_many()
164        .await
165        .map(|values| Into::<VecDeque<Value>>::into(values))
166    {
167        check!(
168            into.send_many_as(
169                values
170                    .into_iter()
171                    .map(|val| val.to_i8())
172                    .collect::<Vec<_>>()
173            )
174            .await
175        )
176    }
177}
178
179/// Turns data into `i16`.
180#[mel_function(
181    generic T (ToI16)
182)]
183pub fn to_i16(value: T) -> i16 {
184    value.to_i16()
185}
186
187/// Turns stream into `i16` one.
188///
189/// This treatment manages infaillible conversions to `i16` data type.
190#[mel_treatment(
191    generic T (ToI16)
192    input value Stream<T>
193    output into Stream<i16>
194)]
195pub async fn to_i16() {
196    while let Ok(values) = value
197        .recv_many()
198        .await
199        .map(|values| Into::<VecDeque<Value>>::into(values))
200    {
201        check!(
202            into.send_many_as(
203                values
204                    .into_iter()
205                    .map(|val| val.to_i16())
206                    .collect::<Vec<_>>()
207            )
208            .await
209        )
210    }
211}
212
213/// Turns data into `i32`.
214#[mel_function(
215    generic T (ToI32)
216)]
217pub fn to_i32(value: T) -> i32 {
218    value.to_i32()
219}
220
221/// Turns stream into `i32` one.
222///
223/// This treatment manages infaillible conversions to `i32` data type.
224#[mel_treatment(
225    generic T (ToI32)
226    input value Stream<T>
227    output into Stream<i32>
228)]
229pub async fn to_i32() {
230    while let Ok(values) = value
231        .recv_many()
232        .await
233        .map(|values| Into::<VecDeque<Value>>::into(values))
234    {
235        check!(
236            into.send_many_as(
237                values
238                    .into_iter()
239                    .map(|val| val.to_i32())
240                    .collect::<Vec<_>>()
241            )
242            .await
243        )
244    }
245}
246
247/// Turns data into `i64`.
248#[mel_function(
249    generic T (ToI64)
250)]
251pub fn to_i64(value: T) -> i64 {
252    value.to_i64()
253}
254
255/// Turns stream into `i64` one.
256///
257/// This treatment manages infaillible conversions to `i64` data type.
258#[mel_treatment(
259    generic T (ToI64)
260    input value Stream<T>
261    output into Stream<i64>
262)]
263pub async fn to_i64() {
264    while let Ok(values) = value
265        .recv_many()
266        .await
267        .map(|values| Into::<VecDeque<Value>>::into(values))
268    {
269        check!(
270            into.send_many_as(
271                values
272                    .into_iter()
273                    .map(|val| val.to_i64())
274                    .collect::<Vec<_>>()
275            )
276            .await
277        )
278    }
279}
280
281/// Turns data into `i128`.
282#[mel_function(
283    generic T (ToI128)
284)]
285pub fn to_i128(value: T) -> i128 {
286    value.to_i128()
287}
288
289/// Turns stream into `i128` one.
290///
291/// This treatment manages infaillible conversions to `i128` data type.
292#[mel_treatment(
293    generic T (ToI128)
294    input value Stream<T>
295    output into Stream<i128>
296)]
297pub async fn to_i128() {
298    while let Ok(values) = value
299        .recv_many()
300        .await
301        .map(|values| Into::<VecDeque<Value>>::into(values))
302    {
303        check!(
304            into.send_many_as(
305                values
306                    .into_iter()
307                    .map(|val| val.to_i128())
308                    .collect::<Vec<_>>()
309            )
310            .await
311        )
312    }
313}
314
315/// Turns data into `u8`.
316#[mel_function(
317    generic T (ToU8)
318)]
319pub fn to_u8(value: T) -> u8 {
320    value.to_u8()
321}
322
323/// Turns stream into `u8` one.
324///
325/// This treatment manages infaillible conversions to `u8` data type.
326#[mel_treatment(
327    generic T (ToU8)
328    input value Stream<T>
329    output into Stream<u8>
330)]
331pub async fn to_u8() {
332    while let Ok(values) = value
333        .recv_many()
334        .await
335        .map(|values| Into::<VecDeque<Value>>::into(values))
336    {
337        check!(
338            into.send_many_as(
339                values
340                    .into_iter()
341                    .map(|val| val.to_u8())
342                    .collect::<Vec<_>>()
343            )
344            .await
345        )
346    }
347}
348
349/// Turns data into `u16`.
350#[mel_function(
351    generic T (ToU16)
352)]
353pub fn to_u16(value: T) -> u16 {
354    value.to_u16()
355}
356
357/// Turns stream into `u16` one.
358///
359/// This treatment manages infaillible conversions to `u16` data type.
360#[mel_treatment(
361    generic T (ToU16)
362    input value Stream<T>
363    output into Stream<u16>
364)]
365pub async fn to_u16() {
366    while let Ok(values) = value
367        .recv_many()
368        .await
369        .map(|values| Into::<VecDeque<Value>>::into(values))
370    {
371        check!(
372            into.send_many_as(
373                values
374                    .into_iter()
375                    .map(|val| val.to_u16())
376                    .collect::<Vec<_>>()
377            )
378            .await
379        )
380    }
381}
382
383/// Turns data into `u32`.
384#[mel_function(
385    generic T (ToU32)
386)]
387pub fn to_u32(value: T) -> u32 {
388    value.to_u32()
389}
390
391/// Turns stream into `u32` one.
392///
393/// This treatment manages infaillible conversions to `u32` data type.
394#[mel_treatment(
395    generic T (ToU32)
396    input value Stream<T>
397    output into Stream<u32>
398)]
399pub async fn to_u32() {
400    while let Ok(values) = value
401        .recv_many()
402        .await
403        .map(|values| Into::<VecDeque<Value>>::into(values))
404    {
405        check!(
406            into.send_many_as(
407                values
408                    .into_iter()
409                    .map(|val| val.to_u32())
410                    .collect::<Vec<_>>()
411            )
412            .await
413        )
414    }
415}
416
417/// Turns data into `u64`.
418#[mel_function(
419    generic T (ToU64)
420)]
421pub fn to_u64(value: T) -> u64 {
422    value.to_u64()
423}
424
425/// Turns stream into `u64` one.
426///
427/// This treatment manages infaillible conversions to `u64` data type.
428#[mel_treatment(
429    generic T (ToU64)
430    input value Stream<T>
431    output into Stream<u64>
432)]
433pub async fn to_u64() {
434    while let Ok(values) = value
435        .recv_many()
436        .await
437        .map(|values| Into::<VecDeque<Value>>::into(values))
438    {
439        check!(
440            into.send_many_as(
441                values
442                    .into_iter()
443                    .map(|val| val.to_u64())
444                    .collect::<Vec<_>>()
445            )
446            .await
447        )
448    }
449}
450
451/// Turns data into `u128`.
452#[mel_function(
453    generic T (ToU128)
454)]
455pub fn to_u128(value: T) -> u128 {
456    value.to_u128()
457}
458
459/// Turns stream into `u128` one.
460///
461/// This treatment manages infaillible conversions to `u128` data type.
462#[mel_treatment(
463    generic T (ToU128)
464    input value Stream<T>
465    output into Stream<u128>
466)]
467pub async fn to_u128() {
468    while let Ok(values) = value
469        .recv_many()
470        .await
471        .map(|values| Into::<VecDeque<Value>>::into(values))
472    {
473        check!(
474            into.send_many_as(
475                values
476                    .into_iter()
477                    .map(|val| val.to_u128())
478                    .collect::<Vec<_>>()
479            )
480            .await
481        )
482    }
483}
484
485/// Turns data into `f32`.
486#[mel_function(
487    generic T (ToF32)
488)]
489pub fn to_f32(value: T) -> f32 {
490    value.to_f32()
491}
492
493/// Turns stream into `f32` one.
494///
495/// This treatment manages infaillible conversions to `f32` data type.
496#[mel_treatment(
497    generic T (ToF32)
498    input value Stream<T>
499    output into Stream<f32>
500)]
501pub async fn to_f32() {
502    while let Ok(values) = value
503        .recv_many()
504        .await
505        .map(|values| Into::<VecDeque<Value>>::into(values))
506    {
507        check!(
508            into.send_many_as(
509                values
510                    .into_iter()
511                    .map(|val| val.to_f32())
512                    .collect::<Vec<_>>()
513            )
514            .await
515        )
516    }
517}
518
519/// Turns data into `f64`.
520#[mel_function(
521    generic T (ToF64)
522)]
523pub fn to_f64(value: T) -> f64 {
524    value.to_f64()
525}
526
527/// Turns stream into `f64` one.
528///
529/// This treatment manages infaillible conversions to `f64` data type.
530#[mel_treatment(
531    generic T (ToF64)
532    input value Stream<T>
533    output into Stream<f64>
534)]
535pub async fn to_f64() {
536    while let Ok(values) = value
537        .recv_many()
538        .await
539        .map(|values| Into::<VecDeque<Value>>::into(values))
540    {
541        check!(
542            into.send_many_as(
543                values
544                    .into_iter()
545                    .map(|val| val.to_f64())
546                    .collect::<Vec<_>>()
547            )
548            .await
549        )
550    }
551}
552
553/// Turns data into `bool`.
554#[mel_function(
555    generic T (ToBool)
556)]
557pub fn to_bool(value: T) -> bool {
558    value.to_bool()
559}
560
561/// Turns stream into `bool` one.
562///
563/// This treatment manages infaillible conversions to `bool` data type.
564#[mel_treatment(
565    generic T (ToBool)
566    input value Stream<T>
567    output into Stream<bool>
568)]
569pub async fn to_bool() {
570    while let Ok(values) = value
571        .recv_many()
572        .await
573        .map(|values| Into::<VecDeque<Value>>::into(values))
574    {
575        check!(
576            into.send_many_as(
577                values
578                    .into_iter()
579                    .map(|val| val.to_bool())
580                    .collect::<Vec<_>>()
581            )
582            .await
583        )
584    }
585}
586
587/// Turns data into `byte`.
588#[mel_function(
589    generic T (ToByte)
590)]
591pub fn to_byte(value: T) -> byte {
592    value.to_byte()
593}
594
595/// Turns stream into `byte` one.
596///
597/// This treatment manages infaillible conversions to `byte` data type.
598#[mel_treatment(
599    generic T (ToByte)
600    input value Stream<T>
601    output into Stream<byte>
602)]
603pub async fn to_byte() {
604    while let Ok(values) = value
605        .recv_many()
606        .await
607        .map(|values| Into::<VecDeque<Value>>::into(values))
608    {
609        check!(
610            into.send_many(TransmissionValue::Byte(
611                values.into_iter().map(|val| val.to_byte()).collect()
612            ))
613            .await
614        )
615    }
616}
617
618/// Turns data into `char`.
619#[mel_function(
620    generic T (ToChar)
621)]
622pub fn to_char(value: T) -> char {
623    value.to_char()
624}
625
626/// Turns stream into `char` one.
627///
628/// This treatment manages infaillible conversions to `char` data type.
629#[mel_treatment(
630    generic T (ToChar)
631    input value Stream<T>
632    output into Stream<char>
633)]
634pub async fn to_char() {
635    while let Ok(values) = value
636        .recv_many()
637        .await
638        .map(|values| Into::<VecDeque<Value>>::into(values))
639    {
640        check!(
641            into.send_many_as(
642                values
643                    .into_iter()
644                    .map(|val| val.to_char())
645                    .collect::<Vec<_>>()
646            )
647            .await
648        )
649    }
650}
651
652/// Turns data into `string`.
653#[mel_function(
654    generic T (ToString)
655)]
656pub fn to_string(value: T) -> string {
657    DataTrait::to_string(&value)
658}
659
660/// Turns stream into `string` one.
661///
662/// This treatment manages infaillible conversions to `string` data type.
663#[mel_treatment(
664    generic T (ToString)
665    input value Stream<T>
666    output into Stream<string>
667)]
668pub async fn to_string() {
669    while let Ok(values) = value
670        .recv_many()
671        .await
672        .map(|values| Into::<VecDeque<Value>>::into(values))
673    {
674        check!(
675            into.send_many_as(
676                values
677                    .into_iter()
678                    .map(|val| DataTrait::to_string(&val))
679                    .collect::<Vec<_>>()
680            )
681            .await
682        )
683    }
684}
685
686/// Try to turn data into `i8`.
687///
688/// This function returns an `Option` containing value if conversion is successful.
689#[mel_function(
690    generic T (TryToI8)
691)]
692pub fn try_to_i8(value: T) -> Option<i8> {
693    value.try_to_i8()
694}
695
696/// Try to turn data stream into `i8` one.
697///
698/// This treatment manages faillible conversion to `i8` data type.
699/// If conversion is successful, an option with `i8` value is streamed, else the option is set to none.
700#[mel_treatment(
701    generic T (TryToI8)
702    input value Stream<T>
703    output into Stream<Option<i8>>
704)]
705pub async fn try_to_i8() {
706    while let Ok(values) = value
707        .recv_many()
708        .await
709        .map(|values| Into::<VecDeque<Value>>::into(values))
710    {
711        check!(
712            into.send_many(TransmissionValue::Other(
713                values
714                    .into_iter()
715                    .map(|val| val.try_to_i8().into())
716                    .collect()
717            ))
718            .await
719        )
720    }
721}
722
723/// Try to turn data into `i16`.
724///
725/// This function returns an `Option` containing value if conversion is successful.
726#[mel_function(
727    generic T (TryToI16)
728)]
729pub fn try_to_i16(value: T) -> Option<i16> {
730    value.try_to_i16()
731}
732
733/// Try to turn data stream into `i16` one.
734///
735/// This treatment manages faillible conversion to `i16` data type.
736/// If conversion is successful, an option with `i16` value is streamed, else the option is set to none.
737#[mel_treatment(
738    generic T (TryToI16)
739    input value Stream<T>
740    output into Stream<Option<i16>>
741)]
742pub async fn try_to_i16() {
743    while let Ok(values) = value
744        .recv_many()
745        .await
746        .map(|values| Into::<VecDeque<Value>>::into(values))
747    {
748        check!(
749            into.send_many(TransmissionValue::Other(
750                values
751                    .into_iter()
752                    .map(|val| val.try_to_i16().into())
753                    .collect()
754            ))
755            .await
756        )
757    }
758}
759
760/// Try to turn data into `i32`.
761///
762/// This function returns an `Option` containing value if conversion is successful.
763#[mel_function(
764    generic T (TryToI32)
765)]
766pub fn try_to_i32(value: T) -> Option<i32> {
767    value.try_to_i32()
768}
769
770/// Try to turn data stream into `i32` one.
771///
772/// This treatment manages faillible conversion to `i32` data type.
773/// If conversion is successful, an option with `i32` value is streamed, else the option is set to none.
774#[mel_treatment(
775    generic T (TryToI32)
776    input value Stream<T>
777    output into Stream<Option<i32>>
778)]
779pub async fn try_to_i32() {
780    while let Ok(values) = value
781        .recv_many()
782        .await
783        .map(|values| Into::<VecDeque<Value>>::into(values))
784    {
785        check!(
786            into.send_many(TransmissionValue::Other(
787                values
788                    .into_iter()
789                    .map(|val| val.try_to_i32().into())
790                    .collect()
791            ))
792            .await
793        )
794    }
795}
796
797/// Try to turn data into `i64`.
798///
799/// This function returns an `Option` containing value if conversion is successful.
800#[mel_function(
801    generic T (TryToI64)
802)]
803pub fn try_to_i64(value: T) -> Option<i64> {
804    value.try_to_i64()
805}
806
807/// Try to turn data stream into `i64` one.
808///
809/// This treatment manages faillible conversion to `i64` data type.
810/// If conversion is successful, an option with `i64` value is streamed, else the option is set to none.
811#[mel_treatment(
812    generic T (TryToI64)
813    input value Stream<T>
814    output into Stream<Option<i64>>
815)]
816pub async fn try_to_i64() {
817    while let Ok(values) = value
818        .recv_many()
819        .await
820        .map(|values| Into::<VecDeque<Value>>::into(values))
821    {
822        check!(
823            into.send_many(TransmissionValue::Other(
824                values
825                    .into_iter()
826                    .map(|val| val.try_to_i64().into())
827                    .collect()
828            ))
829            .await
830        )
831    }
832}
833
834/// Try to turn data into `i128`.
835///
836/// This function returns an `Option` containing value if conversion is successful.
837#[mel_function(
838    generic T (TryToI128)
839)]
840pub fn try_to_i128(value: T) -> Option<i128> {
841    value.try_to_i128()
842}
843
844/// Try to turn data stream into `i128` one.
845///
846/// This treatment manages faillible conversion to `i128` data type.
847/// If conversion is successful, an option with `i128` value is streamed, else the option is set to none.
848#[mel_treatment(
849    generic T (TryToI128)
850    input value Stream<T>
851    output into Stream<Option<i128>>
852)]
853pub async fn try_to_i128() {
854    while let Ok(values) = value
855        .recv_many()
856        .await
857        .map(|values| Into::<VecDeque<Value>>::into(values))
858    {
859        check!(
860            into.send_many(TransmissionValue::Other(
861                values
862                    .into_iter()
863                    .map(|val| val.try_to_i128().into())
864                    .collect()
865            ))
866            .await
867        )
868    }
869}
870
871/// Try to turn data into `u8`.
872///
873/// This function returns an `Option` containing value if conversion is successful.
874#[mel_function(
875    generic T (TryToU8)
876)]
877pub fn try_to_u8(value: T) -> Option<u8> {
878    value.try_to_u8()
879}
880
881/// Try to turn data stream into `u8` one.
882///
883/// This treatment manages faillible conversion to `u8` data type.
884/// If conversion is successful, an option with `u8` value is streamed, else the option is set to none.
885#[mel_treatment(
886    generic T (TryToU8)
887    input value Stream<T>
888    output into Stream<Option<u8>>
889)]
890pub async fn try_to_u8() {
891    while let Ok(values) = value
892        .recv_many()
893        .await
894        .map(|values| Into::<VecDeque<Value>>::into(values))
895    {
896        check!(
897            into.send_many(TransmissionValue::Other(
898                values
899                    .into_iter()
900                    .map(|val| val.try_to_u8().into())
901                    .collect()
902            ))
903            .await
904        )
905    }
906}
907
908/// Try to turn data into `u16`.
909///
910/// This function returns an `Option` containing value if conversion is successful.
911#[mel_function(
912    generic T (TryToU16)
913)]
914pub fn try_to_u16(value: T) -> Option<u16> {
915    value.try_to_u16()
916}
917
918/// Try to turn data stream into `u16` one.
919///
920/// This treatment manages faillible conversion to `u16` data type.
921/// If conversion is successful, an option with `u16` value is streamed, else the option is set to none.
922#[mel_treatment(
923    generic T (TryToU16)
924    input value Stream<T>
925    output into Stream<Option<u16>>
926)]
927pub async fn try_to_u16() {
928    while let Ok(values) = value
929        .recv_many()
930        .await
931        .map(|values| Into::<VecDeque<Value>>::into(values))
932    {
933        check!(
934            into.send_many(TransmissionValue::Other(
935                values
936                    .into_iter()
937                    .map(|val| val.try_to_u16().into())
938                    .collect()
939            ))
940            .await
941        )
942    }
943}
944
945/// Try to turn data into `u32`.
946///
947/// This function returns an `Option` containing value if conversion is successful.
948#[mel_function(
949    generic T (TryToU32)
950)]
951pub fn try_to_u32(value: T) -> Option<u32> {
952    value.try_to_u32()
953}
954
955/// Try to turn data stream into `u32` one.
956///
957/// This treatment manages faillible conversion to `u32` data type.
958/// If conversion is successful, an option with `u32` value is streamed, else the option is set to none.
959#[mel_treatment(
960    generic T (TryToU32)
961    input value Stream<T>
962    output into Stream<Option<u32>>
963)]
964pub async fn try_to_u32() {
965    while let Ok(values) = value
966        .recv_many()
967        .await
968        .map(|values| Into::<VecDeque<Value>>::into(values))
969    {
970        check!(
971            into.send_many(TransmissionValue::Other(
972                values
973                    .into_iter()
974                    .map(|val| val.try_to_u32().into())
975                    .collect()
976            ))
977            .await
978        )
979    }
980}
981
982/// Try to turn data into `u64`.
983///
984/// This function returns an `Option` containing value if conversion is successful.
985#[mel_function(
986    generic T (TryToU64)
987)]
988pub fn try_to_u64(value: T) -> Option<u64> {
989    value.try_to_u64()
990}
991
992/// Try to turn data stream into `u64` one.
993///
994/// This treatment manages faillible conversion to `u64` data type.
995/// If conversion is successful, an option with `u64` value is streamed, else the option is set to none.
996#[mel_treatment(
997    generic T (TryToU64)
998    input value Stream<T>
999    output into Stream<Option<u64>>
1000)]
1001pub async fn try_to_u64() {
1002    while let Ok(values) = value
1003        .recv_many()
1004        .await
1005        .map(|values| Into::<VecDeque<Value>>::into(values))
1006    {
1007        check!(
1008            into.send_many(TransmissionValue::Other(
1009                values
1010                    .into_iter()
1011                    .map(|val| val.try_to_u64().into())
1012                    .collect()
1013            ))
1014            .await
1015        )
1016    }
1017}
1018
1019/// Try to turn data into `u128`.
1020///
1021/// This function returns an `Option` containing value if conversion is successful.
1022#[mel_function(
1023    generic T (TryToU128)
1024)]
1025pub fn try_to_u128(value: T) -> Option<u128> {
1026    value.try_to_u128()
1027}
1028
1029/// Try to turn data stream into `u128` one.
1030///
1031/// This treatment manages faillible conversion to `u128` data type.
1032/// If conversion is successful, an option with `u128` value is streamed, else the option is set to none.
1033#[mel_treatment(
1034    generic T (TryToU128)
1035    input value Stream<T>
1036    output into Stream<Option<u128>>
1037)]
1038pub async fn try_to_u128() {
1039    while let Ok(values) = value
1040        .recv_many()
1041        .await
1042        .map(|values| Into::<VecDeque<Value>>::into(values))
1043    {
1044        check!(
1045            into.send_many(TransmissionValue::Other(
1046                values
1047                    .into_iter()
1048                    .map(|val| val.try_to_u128().into())
1049                    .collect()
1050            ))
1051            .await
1052        )
1053    }
1054}
1055
1056/// Try to turn data into `f32`.
1057///
1058/// This function returns an `Option` containing value if conversion is successful.
1059#[mel_function(
1060    generic T (TryToF32)
1061)]
1062pub fn try_to_f32(value: T) -> Option<f32> {
1063    value.try_to_f32()
1064}
1065
1066/// Try to turn data stream into `f32` one.
1067///
1068/// This treatment manages faillible conversion to `f32` data type.
1069/// If conversion is successful, an option with `f32` value is streamed, else the option is set to none.
1070#[mel_treatment(
1071    generic T (TryToF32)
1072    input value Stream<T>
1073    output into Stream<Option<f32>>
1074)]
1075pub async fn try_to_f32() {
1076    while let Ok(values) = value
1077        .recv_many()
1078        .await
1079        .map(|values| Into::<VecDeque<Value>>::into(values))
1080    {
1081        check!(
1082            into.send_many(TransmissionValue::Other(
1083                values
1084                    .into_iter()
1085                    .map(|val| val.try_to_f32().into())
1086                    .collect()
1087            ))
1088            .await
1089        )
1090    }
1091}
1092
1093/// Try to turn data into `f64`.
1094///
1095/// This function returns an `Option` containing value if conversion is successful.
1096#[mel_function(
1097    generic T (TryToF64)
1098)]
1099pub fn try_to_f64(value: T) -> Option<f64> {
1100    value.try_to_f64()
1101}
1102
1103/// Try to turn data stream into `f64` one.
1104///
1105/// This treatment manages faillible conversion to `f64` data type.
1106/// If conversion is successful, an option with `f64` value is streamed, else the option is set to none.
1107#[mel_treatment(
1108    generic T (TryToF64)
1109    input value Stream<T>
1110    output into Stream<Option<f64>>
1111)]
1112pub async fn try_to_f64() {
1113    while let Ok(values) = value
1114        .recv_many()
1115        .await
1116        .map(|values| Into::<VecDeque<Value>>::into(values))
1117    {
1118        check!(
1119            into.send_many(TransmissionValue::Other(
1120                values
1121                    .into_iter()
1122                    .map(|val| val.try_to_f64().into())
1123                    .collect()
1124            ))
1125            .await
1126        )
1127    }
1128}
1129
1130/// Try to turn data into `bool`.
1131///
1132/// This function returns an `Option` containing value if conversion is successful.
1133#[mel_function(
1134    generic T (TryToBool)
1135)]
1136pub fn try_to_bool(value: T) -> Option<bool> {
1137    value.try_to_bool()
1138}
1139
1140/// Try to turn data stream into `bool` one.
1141///
1142/// This treatment manages faillible conversion to `bool` data type.
1143/// If conversion is successful, an option with `bool` value is streamed, else the option is set to none.
1144#[mel_treatment(
1145    generic T (TryToBool)
1146    input value Stream<T>
1147    output into Stream<Option<bool>>
1148)]
1149pub async fn try_to_bool() {
1150    while let Ok(values) = value
1151        .recv_many()
1152        .await
1153        .map(|values| Into::<VecDeque<Value>>::into(values))
1154    {
1155        check!(
1156            into.send_many(TransmissionValue::Other(
1157                values
1158                    .into_iter()
1159                    .map(|val| val.try_to_bool().into())
1160                    .collect()
1161            ))
1162            .await
1163        )
1164    }
1165}
1166
1167/// Try to turn data into `byte`.
1168///
1169/// This function returns an `Option` containing value if conversion is successful.
1170#[mel_function(
1171    generic T (TryToByte)
1172)]
1173pub fn try_to_byte(value: T) -> Option<byte> {
1174    value.try_to_byte()
1175}
1176
1177/// Try to turn data stream into `byte` one.
1178///
1179/// This treatment manages faillible conversion to `byte` data type.
1180/// If conversion is successful, an option with `byte` value is streamed, else the option is set to none.
1181#[mel_treatment(
1182    generic T (TryToByte)
1183    input value Stream<T>
1184    output into Stream<Option<byte>>
1185)]
1186pub async fn try_to_byte() {
1187    while let Ok(values) = value
1188        .recv_many()
1189        .await
1190        .map(|values| Into::<VecDeque<Value>>::into(values))
1191    {
1192        check!(
1193            into.send_many(TransmissionValue::Other(
1194                values
1195                    .into_iter()
1196                    .map(|val| val.try_to_byte().into())
1197                    .collect()
1198            ))
1199            .await
1200        )
1201    }
1202}
1203
1204/// Try to turn data into `char`.
1205///
1206/// This function returns an `Option` containing value if conversion is successful.
1207#[mel_function(
1208    generic T (TryToChar)
1209)]
1210pub fn try_to_char(value: T) -> Option<char> {
1211    value.try_to_char()
1212}
1213
1214/// Try to turn data stream into `char` one.
1215///
1216/// This treatment manages faillible conversion to `char` data type.
1217/// If conversion is successful, an option with `char` value is streamed, else the option is set to none.
1218#[mel_treatment(
1219    generic T (TryToChar)
1220    input value Stream<T>
1221    output into Stream<Option<char>>
1222)]
1223pub async fn try_to_char() {
1224    while let Ok(values) = value
1225        .recv_many()
1226        .await
1227        .map(|values| Into::<VecDeque<Value>>::into(values))
1228    {
1229        check!(
1230            into.send_many(TransmissionValue::Other(
1231                values
1232                    .into_iter()
1233                    .map(|val| val.try_to_char().into())
1234                    .collect()
1235            ))
1236            .await
1237        )
1238    }
1239}
1240
1241/// Try to turn data into `string`.
1242///
1243/// This function returns an `Option` containing value if conversion is successful.
1244#[mel_function(
1245    generic T (TryToString)
1246)]
1247pub fn try_to_string(value: T) -> Option<string> {
1248    value.try_to_string()
1249}
1250
1251/// Try to turn data stream into `string` one.
1252///
1253/// This treatment manages faillible conversion to `string` data type.
1254/// If conversion is successful, an option with `string` value is streamed, else the option is set to none.
1255#[mel_treatment(
1256    generic T (TryToString)
1257    input value Stream<T>
1258    output into Stream<Option<string>>
1259)]
1260pub async fn try_to_string() {
1261    while let Ok(values) = value
1262        .recv_many()
1263        .await
1264        .map(|values| Into::<VecDeque<Value>>::into(values))
1265    {
1266        check!(
1267            into.send_many(TransmissionValue::Other(
1268                values
1269                    .into_iter()
1270                    .map(|val| val.try_to_string().into())
1271                    .collect()
1272            ))
1273            .await
1274        )
1275    }
1276}
1277
1278/// Turns data into `i8`, saturating if needed.
1279///
1280/// This function makes a saturating and infaillible conversion to `i8`.
1281/// If incoming data represents something out of bounds for `i8`, then
1282/// the resulting value is set to minimum or maximum, depending what is
1283/// the closest to truth.
1284#[mel_function(
1285    generic T (SaturatingToI8)
1286)]
1287pub fn saturating_to_i8(value: T) -> i8 {
1288    value.saturating_to_i8()
1289}
1290
1291/// Turns stream into `i8` one, saturating if needed.
1292///
1293/// This treatment manages saturating and infaillible conversion to `i8`.
1294/// If incoming data represents something out of bounds for `i8`, then
1295/// the resulting value is set to minimum or maximum, depending what is
1296/// the closest to truth.
1297#[mel_treatment(
1298    generic T (SaturatingToI8)
1299    input value Stream<T>
1300    output into Stream<i8>
1301)]
1302pub async fn saturating_to_i8() {
1303    while let Ok(values) = value
1304        .recv_many()
1305        .await
1306        .map(|values| Into::<VecDeque<Value>>::into(values))
1307    {
1308        check!(
1309            into.send_many_as(
1310                values
1311                    .into_iter()
1312                    .map(|val| val.saturating_to_i8())
1313                    .collect::<Vec<_>>()
1314            )
1315            .await
1316        )
1317    }
1318}
1319
1320/// Turns data into `i16`, saturating if needed.
1321///
1322/// This function makes a saturating and infaillible conversion to `i16`.
1323/// If incoming data represents something out of bounds for `i16`, then
1324/// the resulting value is set to minimum or maximum, depending what is
1325/// the closest to truth.
1326#[mel_function(
1327    generic T (SaturatingToI16)
1328)]
1329pub fn saturating_to_i16(value: T) -> i16 {
1330    value.saturating_to_i16()
1331}
1332
1333/// Turns stream into `i16` one, saturating if needed.
1334///
1335/// This treatment manages saturating and infaillible conversion to `i16`.
1336/// If incoming data represents something out of bounds for `i16`, then
1337/// the resulting value is set to minimum or maximum, depending what is
1338/// the closest to truth.
1339#[mel_treatment(
1340    generic T (SaturatingToI16)
1341    input value Stream<T>
1342    output into Stream<i16>
1343)]
1344pub async fn saturating_to_i16() {
1345    while let Ok(values) = value
1346        .recv_many()
1347        .await
1348        .map(|values| Into::<VecDeque<Value>>::into(values))
1349    {
1350        check!(
1351            into.send_many_as(
1352                values
1353                    .into_iter()
1354                    .map(|val| val.saturating_to_i16())
1355                    .collect::<Vec<_>>()
1356            )
1357            .await
1358        )
1359    }
1360}
1361
1362/// Turns data into `i32`, saturating if needed.
1363///
1364/// This function makes a saturating and infaillible conversion to `i32`.
1365/// If incoming data represents something out of bounds for `i32`, then
1366/// the resulting value is set to minimum or maximum, depending what is
1367/// the closest to truth.
1368#[mel_function(
1369    generic T (SaturatingToI32)
1370)]
1371pub fn saturating_to_i32(value: T) -> i32 {
1372    value.saturating_to_i32()
1373}
1374
1375/// Turns stream into `i32` one, saturating if needed.
1376///
1377/// This treatment manages saturating and infaillible conversion to `i32`.
1378/// If incoming data represents something out of bounds for `i32`, then
1379/// the resulting value is set to minimum or maximum, depending what is
1380/// the closest to truth.
1381#[mel_treatment(
1382    generic T (SaturatingToI32)
1383    input value Stream<T>
1384    output into Stream<i32>
1385)]
1386pub async fn saturating_to_i32() {
1387    while let Ok(values) = value
1388        .recv_many()
1389        .await
1390        .map(|values| Into::<VecDeque<Value>>::into(values))
1391    {
1392        check!(
1393            into.send_many_as(
1394                values
1395                    .into_iter()
1396                    .map(|val| val.saturating_to_i32())
1397                    .collect::<Vec<_>>()
1398            )
1399            .await
1400        )
1401    }
1402}
1403
1404/// Turns data into `i64`, saturating if needed.
1405///
1406/// This function makes a saturating and infaillible conversion to `i64`.
1407/// If incoming data represents something out of bounds for `i64`, then
1408/// the resulting value is set to minimum or maximum, depending what is
1409/// the closest to truth.
1410#[mel_function(
1411    generic T (SaturatingToI64)
1412)]
1413pub fn saturating_to_i64(value: T) -> i64 {
1414    value.saturating_to_i64()
1415}
1416
1417/// Turns stream into `i64` one, saturating if needed.
1418///
1419/// This treatment manages saturating and infaillible conversion to `i64`.
1420/// If incoming data represents something out of bounds for `i64`, then
1421/// the resulting value is set to minimum or maximum, depending what is
1422/// the closest to truth.
1423#[mel_treatment(
1424    generic T (SaturatingToI64)
1425    input value Stream<T>
1426    output into Stream<i64>
1427)]
1428pub async fn saturating_to_i64() {
1429    while let Ok(values) = value
1430        .recv_many()
1431        .await
1432        .map(|values| Into::<VecDeque<Value>>::into(values))
1433    {
1434        check!(
1435            into.send_many_as(
1436                values
1437                    .into_iter()
1438                    .map(|val| val.saturating_to_i64())
1439                    .collect::<Vec<_>>()
1440            )
1441            .await
1442        )
1443    }
1444}
1445
1446/// Turns data into `i128`, saturating if needed.
1447///
1448/// This function makes a saturating and infaillible conversion to `i128`.
1449/// If incoming data represents something out of bounds for `i128`, then
1450/// the resulting value is set to minimum or maximum, depending what is
1451/// the closest to truth.
1452#[mel_function(
1453    generic T (SaturatingToI128)
1454)]
1455pub fn saturating_to_i128(value: T) -> i128 {
1456    value.saturating_to_i128()
1457}
1458
1459/// Turns stream into `i128` one, saturating if needed.
1460///
1461/// This treatment manages saturating and infaillible conversion to `i128`.
1462/// If incoming data represents something out of bounds for `i128`, then
1463/// the resulting value is set to minimum or maximum, depending what is
1464/// the closest to truth.
1465#[mel_treatment(
1466    generic T (SaturatingToI128)
1467    input value Stream<T>
1468    output into Stream<i128>
1469)]
1470pub async fn saturating_to_i128() {
1471    while let Ok(values) = value
1472        .recv_many()
1473        .await
1474        .map(|values| Into::<VecDeque<Value>>::into(values))
1475    {
1476        check!(
1477            into.send_many_as(
1478                values
1479                    .into_iter()
1480                    .map(|val| val.saturating_to_i128())
1481                    .collect::<Vec<_>>()
1482            )
1483            .await
1484        )
1485    }
1486}
1487
1488/// Turns data into `u8`, saturating if needed.
1489///
1490/// This function makes a saturating and infaillible conversion to `u8`.
1491/// If incoming data represents something out of bounds for `u8`, then
1492/// the resulting value is set to minimum or maximum, depending what is
1493/// the closest to truth.
1494#[mel_function(
1495    generic T (SaturatingToU8)
1496)]
1497pub fn saturating_to_u8(value: T) -> u8 {
1498    value.saturating_to_u8()
1499}
1500
1501/// Turns stream into `u8` one, saturating if needed.
1502///
1503/// This treatment manages saturating and infaillible conversion to `u8`.
1504/// If incoming data represents something out of bounds for `u8`, then
1505/// the resulting value is set to minimum or maximum, depending what is
1506/// the closest to truth.
1507#[mel_treatment(
1508    generic T (SaturatingToU8)
1509    input value Stream<T>
1510    output into Stream<u8>
1511)]
1512pub async fn saturating_to_u8() {
1513    while let Ok(values) = value
1514        .recv_many()
1515        .await
1516        .map(|values| Into::<VecDeque<Value>>::into(values))
1517    {
1518        check!(
1519            into.send_many_as(
1520                values
1521                    .into_iter()
1522                    .map(|val| val.saturating_to_u8())
1523                    .collect::<Vec<_>>()
1524            )
1525            .await
1526        )
1527    }
1528}
1529
1530/// Turns data into `u16`, saturating if needed.
1531///
1532/// This function makes a saturating and infaillible conversion to `u16`.
1533/// If incoming data represents something out of bounds for `u16`, then
1534/// the resulting value is set to minimum or maximum, depending what is
1535/// the closest to truth.
1536#[mel_function(
1537    generic T (SaturatingToU16)
1538)]
1539pub fn saturating_to_u16(value: T) -> u16 {
1540    value.saturating_to_u16()
1541}
1542
1543/// Turns stream into `u16` one, saturating if needed.
1544///
1545/// This treatment manages saturating and infaillible conversion to `u16`.
1546/// If incoming data represents something out of bounds for `u16`, then
1547/// the resulting value is set to minimum or maximum, depending what is
1548/// the closest to truth.
1549#[mel_treatment(
1550    generic T (SaturatingToU16)
1551    input value Stream<T>
1552    output into Stream<u16>
1553)]
1554pub async fn saturating_to_u16() {
1555    while let Ok(values) = value
1556        .recv_many()
1557        .await
1558        .map(|values| Into::<VecDeque<Value>>::into(values))
1559    {
1560        check!(
1561            into.send_many_as(
1562                values
1563                    .into_iter()
1564                    .map(|val| val.saturating_to_u16())
1565                    .collect::<Vec<_>>()
1566            )
1567            .await
1568        )
1569    }
1570}
1571
1572/// Turns data into `u32`, saturating if needed.
1573///
1574/// This function makes a saturating and infaillible conversion to `u32`.
1575/// If incoming data represents something out of bounds for `u32`, then
1576/// the resulting value is set to minimum or maximum, depending what is
1577/// the closest to truth.
1578#[mel_function(
1579    generic T (SaturatingToU32)
1580)]
1581pub fn saturating_to_u32(value: T) -> u32 {
1582    value.saturating_to_u32()
1583}
1584
1585/// Turns stream into `u32` one, saturating if needed.
1586///
1587/// This treatment manages saturating and infaillible conversion to `u32`.
1588/// If incoming data represents something out of bounds for `u32`, then
1589/// the resulting value is set to minimum or maximum, depending what is
1590/// the closest to truth.
1591#[mel_treatment(
1592    generic T (SaturatingToU32)
1593    input value Stream<T>
1594    output into Stream<u32>
1595)]
1596pub async fn saturating_to_u32() {
1597    while let Ok(values) = value
1598        .recv_many()
1599        .await
1600        .map(|values| Into::<VecDeque<Value>>::into(values))
1601    {
1602        check!(
1603            into.send_many_as(
1604                values
1605                    .into_iter()
1606                    .map(|val| val.saturating_to_u32())
1607                    .collect::<Vec<_>>()
1608            )
1609            .await
1610        )
1611    }
1612}
1613
1614/// Turns data into `u64`, saturating if needed.
1615///
1616/// This function makes a saturating and infaillible conversion to `u64`.
1617/// If incoming data represents something out of bounds for `u64`, then
1618/// the resulting value is set to minimum or maximum, depending what is
1619/// the closest to truth.
1620#[mel_function(
1621    generic T (SaturatingToU64)
1622)]
1623pub fn saturating_to_u64(value: T) -> u64 {
1624    value.saturating_to_u64()
1625}
1626
1627/// Turns stream into `u64` one, saturating if needed.
1628///
1629/// This treatment manages saturating and infaillible conversion to `u64`.
1630/// If incoming data represents something out of bounds for `u64`, then
1631/// the resulting value is set to minimum or maximum, depending what is
1632/// the closest to truth.
1633#[mel_treatment(
1634    generic T (SaturatingToU64)
1635    input value Stream<T>
1636    output into Stream<u64>
1637)]
1638pub async fn saturating_to_u64() {
1639    while let Ok(values) = value
1640        .recv_many()
1641        .await
1642        .map(|values| Into::<VecDeque<Value>>::into(values))
1643    {
1644        check!(
1645            into.send_many_as(
1646                values
1647                    .into_iter()
1648                    .map(|val| val.saturating_to_u64())
1649                    .collect::<Vec<_>>()
1650            )
1651            .await
1652        )
1653    }
1654}
1655
1656/// Turns data into `u128`, saturating if needed.
1657///
1658/// This function makes a saturating and infaillible conversion to `u128`.
1659/// If incoming data represents something out of bounds for `u128`, then
1660/// the resulting value is set to minimum or maximum, depending what is
1661/// the closest to truth.
1662#[mel_function(
1663    generic T (SaturatingToU128)
1664)]
1665pub fn saturating_to_u128(value: T) -> u128 {
1666    value.saturating_to_u128()
1667}
1668
1669/// Turns stream into `u128` one, saturating if needed.
1670///
1671/// This treatment manages saturating and infaillible conversion to `u128`.
1672/// If incoming data represents something out of bounds for `u128`, then
1673/// the resulting value is set to minimum or maximum, depending what is
1674/// the closest to truth.
1675#[mel_treatment(
1676    generic T (SaturatingToU128)
1677    input value Stream<T>
1678    output into Stream<u128>
1679)]
1680pub async fn saturating_to_u128() {
1681    while let Ok(values) = value
1682        .recv_many()
1683        .await
1684        .map(|values| Into::<VecDeque<Value>>::into(values))
1685    {
1686        check!(
1687            into.send_many_as(
1688                values
1689                    .into_iter()
1690                    .map(|val| val.saturating_to_u128())
1691                    .collect::<Vec<_>>()
1692            )
1693            .await
1694        )
1695    }
1696}
1697
1698/// Turns data into `f32`, saturating if needed.
1699///
1700/// This function makes a saturating and infaillible conversion to `f32`.
1701/// If incoming data represents something not representable purely in `f32`,
1702/// then the resulting value is set to the closest approximation possible.
1703#[mel_function(
1704    generic T (SaturatingToF32)
1705)]
1706pub fn saturating_to_f32(value: T) -> f32 {
1707    value.saturating_to_f32()
1708}
1709
1710/// Turns stream into `f32` one, saturating if needed.
1711///
1712/// This treatment manages saturating and infaillible conversion to `f32`.
1713/// If incoming data represents something out of bounds for `f32`,
1714/// then the resulting value is set to the closest approximation possible.
1715#[mel_treatment(
1716    generic T (SaturatingToF32)
1717    input value Stream<T>
1718    output into Stream<f32>
1719)]
1720pub async fn saturating_to_f32() {
1721    while let Ok(values) = value
1722        .recv_many()
1723        .await
1724        .map(|values| Into::<VecDeque<Value>>::into(values))
1725    {
1726        check!(
1727            into.send_many_as(
1728                values
1729                    .into_iter()
1730                    .map(|val| val.saturating_to_f32())
1731                    .collect::<Vec<_>>()
1732            )
1733            .await
1734        )
1735    }
1736}
1737
1738/// Turns data into `f64`, saturating if needed.
1739///
1740/// This function makes a saturating and infaillible conversion to `f64`.
1741/// If incoming data represents something out of bounds for `f64`,
1742/// then the resulting value is set to the closest approximation possible.
1743#[mel_function(
1744    generic T (SaturatingToF64)
1745)]
1746pub fn saturating_to_f64(value: T) -> f64 {
1747    value.saturating_to_f64()
1748}
1749
1750/// Turns stream into `f64` one, saturating if needed.
1751///
1752/// This treatment manages saturating and infaillible conversion to `f64`.
1753/// If incoming data represents something out of bounds for `f64`,
1754/// then the resulting value is set to the closest approximation possible.
1755#[mel_treatment(
1756    generic T (SaturatingToF64)
1757    input value Stream<T>
1758    output into Stream<f64>
1759)]
1760pub async fn saturating_to_f64() {
1761    while let Ok(values) = value
1762        .recv_many()
1763        .await
1764        .map(|values| Into::<VecDeque<Value>>::into(values))
1765    {
1766        check!(
1767            into.send_many_as(
1768                values
1769                    .into_iter()
1770                    .map(|val| val.saturating_to_f64())
1771                    .collect::<Vec<_>>()
1772            )
1773            .await
1774        )
1775    }
1776}