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
#![warn(missing_docs)]
#![doc(html_root_url = "https://docs.rs/bitstring-trees/0.1.1")]
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::{
panic::*,
process,
};
let with = AssertUnwindSafe(with);
let old = AssertUnwindSafe(unsafe { std::ptr::read(location) });
let new = catch_unwind(move || AssertUnwindSafe(with.0(old.0))).unwrap_or_else(move |_e| {
process::abort();
});
unsafe { std::ptr::write(location, new.0) }
}
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::{
panic::*,
process,
};
let with = AssertUnwindSafe(with);
let fallback = AssertUnwindSafe(fallback);
let old = AssertUnwindSafe(unsafe { std::ptr::read(location) });
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();
},
)
});
unsafe { std::ptr::write(location, new.0) }
if let Some(panic_err) = panic_err {
resume_unwind(panic_err);
}
}