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
use std::any::Any;
use std::sync::Arc;
use std::sync::LockResult;
use std::mem;
use constructed::{Instance, AnyInstance, MaybeMutexGuard};
#[derive(Debug)]
pub struct Scope<T> {
obj: Instance<T>,
childs: Vec<Box<Any>>,
}
impl<T: Any> Scope<T> {
pub fn from_any_instance(obj: AnyInstance, childs: Vec<Box<Any>>) -> Scope<T> {
Scope {
obj: obj.downcast(),
childs: childs,
}
}
pub fn explode(self) -> T {
mem::drop(self.childs);
match self.obj {
Instance::Isolated(obj) => obj,
Instance::Shared(arc) => {
Arc::try_unwrap(arc)
.ok()
.expect("expected arc to be last remaining")
.into_inner()
.expect("expected to lock value before exploding")
}
}
}
pub fn lock<'a>(&'a mut self) -> LockResult<MaybeMutexGuard<'a, T>> {
self.obj.lock()
}
pub fn get_instance(&self) -> &Instance<T> {
&self.obj
}
}
unsafe impl<T> Send for Scope<T> {}
unsafe impl<T> Sync for Scope<T> {}