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
use std::cell::UnsafeCell;
use std::mem;
pub struct MoveCell<T>(UnsafeCell<Option<T>>);
impl<T> MoveCell<T> {
pub fn new() -> MoveCell<T> {
MoveCell(UnsafeCell::new(None))
}
pub fn with(val: T) -> MoveCell<T> {
MoveCell(UnsafeCell::new(Some(val)))
}
pub fn from(opt: Option<T>) -> MoveCell<T> {
MoveCell(UnsafeCell::new(opt))
}
unsafe fn as_mut(&self) -> &mut Option<T> {
&mut *self.0.get()
}
unsafe fn as_ref(&self) -> &Option<T> {
& *self.0.get()
}
pub fn put(&self, val: T) -> Option<T> {
mem::replace(unsafe { self.as_mut() }, Some(val))
}
pub fn take(&self) -> Option<T> {
unsafe { self.as_mut().take() }
}
pub fn clone_inner(&self) -> Option<T> where T: Clone {
let inner = self.take();
inner.clone().map(|inner| self.put(inner));
inner
}
pub fn has_value(&self) -> bool {
unsafe { self.as_ref().is_some() }
}
}
impl<T> Default for MoveCell<T> {
fn default() -> Self {
MoveCell::new()
}
}