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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
extern crate scopeguard;
use scopeguard::guard;
use std::{mem, ptr};
static ERR_ZERO_SIZED: &'static str = "Cannot reuse memory for zero-sized types";
static ERR_ALIGNMENT: &'static str = "`A` and `B` have different alignment";
static ERR_NEED_EXACT_SIZE: &'static str = "`A` and `B` have different sizes";
static ERR_NEED_DIVISIBLE_SIZE: &'static str
= "The size of `A` is not equal to or a multiple of the size of `B`";
pub trait MapBoxInPlace<A> {
fn map<B,F:FnOnce(A)->B>(self, f: F) -> Box<B>;
fn map_in_place<B,F:FnOnce(A)->B>(self, f: F) -> Box<B>;
}
pub trait MapSliceInPlace<A> {
fn map<B,F:FnMut(A)->B>(self, f: F) -> Box<[B]>;
fn map_in_place<B,F:FnMut(A)->B>(self, f: F) -> Box<[B]>;
}
pub trait MapVecInPlace<A> {
fn map<B,F:FnMut(A)->B>(self, f: F) -> Vec<B>;
fn map_in_place<B,F:FnMut(A)->B>(self, f: F) -> Vec<B>;
fn filter_map<B,F:FnMut(A)->Option<B>>(self, f: F) -> Vec<B>;
fn filter_map_in_place<B,F:FnMut(A)->Option<B>>(self, f: F) -> Vec<B>;
}
fn handle_unwind_of<R, F:FnOnce()->R, D:FnMut()>
(might_panic: F, mut cleanup: D) -> R {
let guard = guard((), |_| cleanup() );
let r = might_panic();
mem::forget(guard);
r
}
macro_rules! size {($t:ty) => {mem::size_of::<$t>()}}
macro_rules! align {($t:ty) => {mem::align_of::<$t>()}}
macro_rules! sizes {($a:ty, $b:ty, $zero:expr, $alignment:expr,
$f:expr => $incompatible:expr, $ok:expr,) => {
unsafe {
if size!($a) == 0 || size!($b) == 0 {
$zero
} else if align!($a) != align!($b) {
$alignment
} else if $f(size!($a),size!($b)) {
$incompatible
} else {
$ok
}
}
}}
macro_rules! panicy {($a:ty, $b:ty, $rel:expr=>$ok:expr, $notrel:expr) => {
sizes!{$a,$b,
panic!("{}", ERR_ZERO_SIZED),
panic!("{}", ERR_ALIGNMENT),
$rel => $ok,
panic!("{}", $notrel),
}
}}
macro_rules! fallback {($a:ty, $b:ty, $rel:expr=>$ok:expr, $fallback:expr) => {
sizes!{$a,$b,
$fallback,
$fallback,
$rel => $ok,
$fallback,
}
}}
unsafe fn filter_map_vec<A, B, F:FnMut(A)->Option<B>>
(mut vec: Vec<A>, mut f: F) -> Vec<B> {
let len = vec.len();
let cap = vec.capacity() * size!(A)/size!(B);
let read = vec.as_mut_ptr();
let write = read as *mut B;
mem::forget(vec);
let mut wrote = 0;
for i in 0..len {
let a = ptr::read(read.offset(i as isize));
let result = handle_unwind_of(|| f(a), || {
for ii in i+1..len {
ptr::drop_in_place(read.offset(ii as isize));
}
mem::drop(Vec::from_raw_parts(write, wrote, cap));
});
if let Some(b) = result {
ptr::write(write.offset(wrote as isize), b);
wrote += 1;
}
}
Vec::from_raw_parts(write, wrote, cap)
}
unsafe fn map_vec<A, B, F:FnMut(A)->B>
(vec: Vec<A>, mut f: F) -> Vec<B> {
filter_map_vec(vec, |a| Some(f(a)) )
}
impl<A> MapVecInPlace<A> for Vec<A> {
#[inline]
fn map<B,F:FnMut(A)->B>(self, f: F) -> Vec<B> {
fallback!{A,B,
|a,b| a%b==0 => map_vec(self, f),
self.into_iter().map(f).collect()
}
}
#[inline]
fn map_in_place<B,F:FnMut(A)->B>(self, f: F) -> Vec<B> {
panicy!{A,B,
|a,b| a%b==0 => map_vec(self, f),
ERR_NEED_DIVISIBLE_SIZE
}
}
#[inline]
fn filter_map<B,F:FnMut(A)->Option<B>>(self, f: F) -> Vec<B> {
fallback!{A,B,
|a,b| a%b==0 => filter_map_vec(self, f),
self.into_iter().filter_map(f).collect()
}
}
#[inline]
fn filter_map_in_place<B,F:FnMut(A)->Option<B>>(self, f: F) -> Vec<B> {
panicy!{A,B,
|a,b| a%b==0 => filter_map_vec(self, f),
ERR_NEED_DIVISIBLE_SIZE
}
}
}
unsafe fn map_slice<A, B, F:FnMut(A)->B>
(boxed: Box<[A]>, f: F) -> Box<[B]> {
map_vec(boxed.into_vec(), f).into_boxed_slice()
}
impl<A> MapSliceInPlace<A> for Box<[A]> {
#[inline]
fn map<B,F:FnMut(A)->B>(self, f: F) -> Box<[B]> {
fallback!{A,B,
|a,b| a==b => map_slice(self, f),
Vec::into_boxed_slice(self.into_vec().into_iter().map(f).collect())
}
}
#[inline]
fn map_in_place<B,F:FnMut(A)->B>(self, f: F) -> Box<[B]> {
panicy!{A,B,
|a,b| a==b => map_slice(self, f),
ERR_NEED_EXACT_SIZE
}
}
}
unsafe fn map_box<A, B, F:FnOnce(A)->B>
(boxed: Box<A>, f: F) -> Box<B> {
let aptr = Box::into_raw(boxed);
let a = ptr::read(aptr);
let b = f(a);
let bptr = aptr as *mut B;
ptr::write(bptr, b);
Box::from_raw(bptr)
}
impl<A> MapBoxInPlace<A> for Box<A> {
#[inline]
fn map<B,F:FnOnce(A)->B>(self, f: F) -> Box<B> {
fallback!{A,B,
|a,b| a==b => map_box(self, f),
Box::from(f(*self))
}
}
#[inline]
fn map_in_place<B,M:FnOnce(A)->B>(self, f: M) -> Box<B> {
panicy!{A,B,
|a,b| a==b => map_box(self, f),
ERR_NEED_EXACT_SIZE
}
}
}