1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use doc_comment::doc_comment;
use duplicate::duplicate;
macro_rules! _example {
($e:literal) => {
concat!("```rust\n", $e, "\n```")
};
}
fn _example_code(input_type: String, input: String, res: String) -> String {
format!("let x: {} = {};\nprintln(x); // {}", input_type, input, res)
}
pub trait Turnable {
doc_comment!("Returns a `bool` value from any input", fn bool(&self) -> bool;);
doc_comment!("Returns a `String` value from any input", fn print(&self) -> String;);
doc_comment!("Returns a `Vec<u8>` bytes vector from any input", fn to_vec_u8(&self) -> Vec<u8>;);
doc_comment!("Returns a `Vec<char>` bytes vector from any input", fn to_vec_char(&self) -> Vec<char>;);
}
#[duplicate(
docu original_type;
["Apply `Turnable` trait to the `i8` type"] [ i8 ];
["Apply `Turnable` trait to the `i16` type"] [ i16 ];
["Apply `Turnable` trait to the `i32` type"] [ i32 ];
["Apply `Turnable` trait to the `i64` type"] [ i64 ];
["Apply `Turnable` trait to the `i128` type"] [ i128 ];
["Apply `Turnable` trait to the `u8` type"] [ u8 ];
["Apply `Turnable` trait to the `u16` type"] [ u16 ];
["Apply `Turnable` trait to the `u32` type"] [ u32 ];
["Apply `Turnable` trait to the `u64` type"] [ u64 ];
["Apply `Turnable` trait to the `u128` type"] [ u128 ];
)]
doc_comment!(docu, impl Turnable for original_type {
doc_comment!(concat!("Returns a `bool` value from a `", stringify!(original_type), "` integer value"),
fn bool(&self) -> bool {
*self != 0
}
);
doc_comment!(concat!("Returns a `String` value from a `", stringify!(original_type), "` integer value"),
fn print(&self) -> String {
self.to_string()
}
);
doc_comment!(concat!("Returns a `Vec<u8>` bytes vector from a `", stringify!(original_type), "` integer value"),
fn to_vec_u8(&self) -> Vec<u8> {
self.print().as_bytes().to_owned()
}
);
doc_comment!(concat!("Returns a `Vec<char>` vector from a `", stringify!(original_type), "` integer value"),
fn to_vec_char(&self) -> Vec<char> {
self.print().chars().collect()
}
);
});
#[duplicate(
docu original_type;
["Apply `Turnable` trait to the `f32` type"] [ f32 ];
["Apply `Turnable` trait to the `f64` type"] [ f64 ];
)]
doc_comment!(docu, impl Turnable for original_type {
doc_comment!(concat!("Returns a `bool` value from a `", stringify!(original_type), "` float value"),
fn bool(&self) -> bool {
if self.is_nan() {
false
} else {
*self != 0.0
}
}
);
doc_comment!(concat!("Returns a `String` value from a `", stringify!(original_type), "` float value"),
fn print(&self) -> String {
format!("{:e}", self)
}
);
doc_comment!(concat!("Returns a `Vec<u8>` bytes vector from a `", stringify!(original_type), "` float value"),
fn to_vec_u8(&self) -> Vec<u8> {
self.print().as_bytes().to_owned()
}
);
doc_comment!(concat!("Returns a `Vec<char>` vector from a `", stringify!(original_type), "` float value"),
fn to_vec_char(&self) -> Vec<char> {
self.print().chars().collect()
}
);
});
impl Turnable for String {
doc_comment!(concat!("Returns a `bool` value from a `String` value"),
fn bool(&self) -> bool {
self.is_empty()
}
);
doc_comment!(concat!("Returns a `String` value from... a `String` value"),
fn print(&self) -> String {
format!("{}", *self)
}
);
doc_comment!(concat!("Returns a `Vec<u8>` bytes vector from a `String` value"),
fn to_vec_u8(&self) -> Vec<u8> {
self.as_bytes().to_owned()
}
);
doc_comment!(concat!("Returns a `Vec<char>` vector from a `String` value"),
fn to_vec_char(&self) -> Vec<char> {
self.chars().collect()
}
);
}