-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rs
56 lines (52 loc) · 1.31 KB
/
util.rs
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
use std::cell::RefCell;
use std::ops::Deref;
/// A `Sync`able `RefCell`.
#[derive(Default, Debug, Clone)]
pub struct SyncRef<T> {
val: RefCell<T>,
}
impl<T> SyncRef<T> {
pub fn new(val: T) -> Self {
SyncRef {
val: RefCell::new(val),
}
}
pub fn get_mut(&mut self) -> &mut T {
self.val.get_mut()
}
pub fn as_cell(&mut self) -> &RefCell<T> {
&self.val
}
pub unsafe fn as_cell_unsafe(&self) -> &RefCell<T> {
&self.val
}
}
// Trying to impl Deref/DerefMut provokes odd curiosities.
unsafe impl<T: Send> Send for SyncRef<T> {}
unsafe impl<T> Sync for SyncRef<T> {}
// FIXME: Ugh, this is probably unsound.
/// A `&mut T` that pretends it's a `&T`.
pub struct MutButRef<'a, T>(&'a mut T);
impl<'a, T> MutButRef<'a, T> {
pub fn new(t: &'a mut T) -> Self {
Self(t)
}
pub unsafe fn get_mut(&mut self) -> &mut T {
// Obviously nothing unsafe is happening here,
// but users of MutButRef require the guarantee.
self.0
}
}
impl<'a, T> Deref for MutButRef<'a, T> {
type Target = T;
fn deref(&self) -> &T {
self.0
}
}
pub mod die {
pub static BAD_ITER_LEN: &str = "Iterator must know its exact Id length";
#[cold]
pub fn bad_iter_len() -> ! {
panic!("{}", BAD_ITER_LEN)
}
}