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
#![warn(missing_docs)]
#![doc(html_root_url = "https://docs.rs/bitstring-trees/0.1.0")]
extern crate bitstring;
pub mod map;
pub mod set;
fn replace_at<T, F>(location: &mut T, with: F)
where
T: Sized,
F: FnOnce(T) -> T,
{
use std::mem;
use std::panic::*;
use std::process;
let with = AssertUnwindSafe(with);
let old = AssertUnwindSafe(
mem::replace(location, unsafe{mem::uninitialized()})
);
let new = catch_unwind(move || {
AssertUnwindSafe(with.0(old.0))
}).unwrap_or_else(move |_e| {
process::abort();
});
let tmp = mem::replace(location, new.0);
mem::forget(tmp);
}
fn replace_at_and_fallback<T, F, G>(location: &mut T, with: F, fallback: G)
where
T: Sized,
F: FnOnce(T) -> T,
G: FnOnce() -> T,
{
use std::mem;
use std::panic::*;
use std::process;
let with = AssertUnwindSafe(with);
let fallback = AssertUnwindSafe(fallback);
let old = AssertUnwindSafe(
mem::replace(location, unsafe{mem::uninitialized()})
);
let (new, panic_err) = catch_unwind(move || {
(AssertUnwindSafe(with.0(old.0)), None)
}).unwrap_or_else(move |e| {
let e = AssertUnwindSafe(e);
catch_unwind(move || {
(AssertUnwindSafe(fallback.0()), Some(e.0))
}).unwrap_or_else(move |_e| {
process::abort();
})
});
let tmp = mem::replace(location, new.0);
mem::forget(tmp);
if let Some(panic_err) = panic_err {
resume_unwind(panic_err);
}
}