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