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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::rt;
use std::ops;
#[derive(Debug)]
pub struct Arc<T> {
inner: std::sync::Arc<Inner<T>>,
}
#[derive(Debug)]
struct Inner<T> {
value: T,
obj: rt::Arc,
}
impl<T> Arc<T> {
#[cfg_attr(loom_nightly, track_caller)]
pub fn new(value: T) -> Arc<T> {
let inner = std::sync::Arc::new(Inner {
value,
obj: rt::Arc::new(location!()),
});
Arc { inner }
}
pub fn strong_count(_this: &Self) -> usize {
unimplemented!("no tests checking this? DELETED!")
}
pub fn get_mut(this: &mut Self) -> Option<&mut T> {
if this.inner.obj.get_mut() {
assert_eq!(1, std::sync::Arc::strong_count(&this.inner));
Some(&mut std::sync::Arc::get_mut(&mut this.inner).unwrap().value)
} else {
None
}
}
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&this.inner, &other.inner)
}
pub fn into_raw(this: Self) -> *const T {
use std::mem;
let ptr = &*this as *const _;
mem::forget(this);
ptr as *const T
}
pub unsafe fn from_raw(ptr: *const T) -> Self {
let inner = std::sync::Arc::from_raw(ptr as *const Inner<T>);
Arc { inner }
}
pub fn try_unwrap(_this: Arc<T>) -> Result<T, Arc<T>> {
unimplemented!();
}
}
impl<T> ops::Deref for Arc<T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner.value
}
}
impl<T> Clone for Arc<T> {
fn clone(&self) -> Arc<T> {
self.inner.obj.ref_inc();
Arc {
inner: self.inner.clone(),
}
}
}
impl<T> Drop for Arc<T> {
fn drop(&mut self) {
if self.inner.obj.ref_dec() {
assert_eq!(
1,
std::sync::Arc::strong_count(&self.inner),
"something odd is going on"
);
}
}
}
impl<T: Default> Default for Arc<T> {
fn default() -> Arc<T> {
Arc::new(Default::default())
}
}
impl<T> From<T> for Arc<T> {
fn from(t: T) -> Self {
Arc::new(t)
}
}