1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use num_traits::{NumCast, ToPrimitive};
pub fn try_convert_num_arr<T, U>(input: &Vec<T>) -> Option<Vec<U>>
where
T: ToPrimitive + Clone, // T must implement ToPrimitive
U: NumCast, // U must implement NumCast
{
//TODO: if U==T can I return just a reference to the input?
// if TypeId::of::<T>() == TypeId::of::<U>() {
// return Some(input);
//}
input.iter().map(|item| U::from(item.clone())).collect()
}
pub fn try_convert_str_arr<T: ToString>(input: &Vec<T>) -> Option<Vec<String>> {
// Map each item to its string representation and collect the results into a Vec<String>
Some(input.iter().map(|item| item.to_string()).collect())
}
pub fn try_convert_bool_arr<U>(input: &Vec<bool>) -> Option<Vec<U>>
where
U: NumCast, // U must implement NumCast
{
//TODO: if U==T can I return just a reference to the input?
// if TypeId::of::<T>() == TypeId::of::<U>() {
// return Some(input);
//}
//input.iter().map(|item| U::from(item.clone())).collect()
let au32: Vec<u32> = input.iter().map(|b| if *b { 1 } else { 0 }).collect();
//TODO: if U==u32 can I return just a reference to the input?
//if TypeId::of::<U>() == TypeId::of::<u32>() {
// return Some(au32);
//}
try_convert_num_arr::<u32, U>(&au32)
}
pub fn try_convert_num<T, U>(input: &T) -> Option<U>
where
T: ToPrimitive + Clone, // T must implement ToPrimitive
U: NumCast, // U must implement NumCast
{
U::from(input.clone())
}
pub fn try_convert_bool<U>(input: &bool) -> Option<U>
where
U: NumCast, // U must implement NumCast
{
let v = if *input { 1 } else { 0 };
U::from(v)
}