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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![doc(html_root_url = "https://docs.rs/col_proc_macros_impl/0.1.0/")]
extern crate proc_macro;
use core::fmt::Write;
use core::ptr;
use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;
use unicode_xid::UnicodeXID;
#[proc_macro_hack]
pub fn vector(input: TokenStream) -> TokenStream {
static CODE: &str = "]);\
let mut temp_vec = Vector::with_capacity(arr.len());\
core::ptr::copy_nonoverlapping(arr.as_ptr(), temp_vec.as_mut_ptr(), arr.len());\
temp_vec.set_len(arr.len());\
temp_vec\
}";
let input = input.to_string();
let input_t = input.trim();
let mut delimiters_count = 0;
let mut expansion = String::with_capacity(512);
expansion.push_str(
"unsafe {\
let arr = core::mem::ManuallyDrop::new([",
);
if let Some(i) = input_t.find(|c: char| {
if c == '[' || c == '{' || c == '(' || c == '<' {
delimiters_count += 1;
return false;
}
if c == ']' || c == '}' || c == ')' || c == '>' {
delimiters_count -= 1;
return false;
}
delimiters_count == 0 && c == ';'
}) {
let elem = (&input_t[..i]).trim();
let wheels = (&input_t[i + 1..])
.trim()
.parse::<usize>()
.expect("The length it's not a valid usize");
if wheels > 0 {
let mut b = false;
if elem
.find(|c: char| if b {
!c.is_xid_continue()
} else {
b = true;
!c.is_xid_start()
}
)
.is_some()
{
expansion.reserve_exact((wheels * (elem.len() + 1)).next_power_of_two());
for _ in 0..wheels {
write!(expansion, "{},", elem).unwrap()
}
} else {
expansion.reserve_exact((wheels * (elem.len() + 9)).next_power_of_two());
for _ in 1..wheels {
write!(expansion, "{}.clone(),", elem).unwrap()
}
expansion.push_str(elem);
}
}
} else {
for mut e in input_t.split(|c: char| {
if c == '[' || c == '{' || c == '(' || c == '<' {
delimiters_count += 1;
return false;
}
if c == ']' || c == '}' || c == ')' || c == '>' {
delimiters_count -= 1;
return false;
}
delimiters_count == 0 && c == ','
}) {
e = e.trim();
if e != "" {
write!(expansion, "{},", e).unwrap();
}
}
}
if expansion.ends_with(',') {
let len = expansion.len();
unsafe {
ptr::drop_in_place(expansion.as_mut_vec().as_mut_ptr().add(len));
expansion.as_mut_vec().set_len(len - 1);
}
}
if let Some(i) = expansion.rfind(|c: char| {
if c == '[' || c == '{' || c == '(' || c == '<' {
delimiters_count += 1;
return false;
}
if c == ']' || c == '}' || c == ')' || c == '>' {
delimiters_count -= 1;
return false;
}
delimiters_count == 0 && (c == ':' || c == ',')
}) {
if expansion.as_bytes()[i] == b':' {
let index = input.len() - expansion[i..].len() + 1;
unsafe {
expansion.as_mut_vec().truncate(i)
}
write!(
expansion,
"]);\
let mut temp_vec = <Vector<{}>>::with_capacity(arr.len());\
core::ptr::copy_nonoverlapping(arr.as_ptr(), temp_vec.as_mut_ptr(), arr.len());\
temp_vec.set_len(arr.len());\
temp_vec\
}}",
unsafe { input.get_unchecked(index..) }
)
.unwrap();
} else {
expansion.push_str(CODE);
}
} else {
expansion.push_str(CODE);
}
expansion.parse().unwrap()
}