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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::lifetime::{Lifetime, LifetimeRef, LifetimeRefMut, ValueReadAccess, ValueWriteAccess};
use std::ptr::NonNull;
#[derive(Default)]
pub struct Managed<T> {
lifetime: Lifetime,
data: T,
}
impl<T> Managed<T> {
pub fn new(data: T) -> Self {
Self {
lifetime: Default::default(),
data,
}
}
pub fn renew(mut self) -> Self {
self.lifetime = Lifetime::default();
self
}
pub fn lifetime(&self) -> &Lifetime {
&self.lifetime
}
pub fn read(&self) -> Option<ValueReadAccess<T>> {
self.lifetime.read(&self.data)
}
pub fn write(&mut self) -> Option<ValueWriteAccess<T>> {
self.lifetime.write(&mut self.data)
}
pub fn consume(self) -> Result<T, Self> {
if self.lifetime.state().is_in_use() {
Err(self)
} else {
Ok(self.data)
}
}
pub fn borrow(&self) -> Option<ManagedRef<T>> {
Some(ManagedRef::new(&self.data, self.lifetime.borrow()?))
}
pub fn borrow_mut(&mut self) -> Option<ManagedRefMut<T>> {
Some(ManagedRefMut::new(
&mut self.data,
self.lifetime.borrow_mut()?,
))
}
}
pub struct ManagedRef<T> {
lifetime: LifetimeRef,
data: NonNull<T>,
}
unsafe impl<T> Send for ManagedRef<T> where T: Send {}
impl<T> ManagedRef<T> {
pub fn new(data: &T, lifetime: LifetimeRef) -> Self {
Self {
lifetime,
data: unsafe { NonNull::new_unchecked(data as *const T as *mut T) },
}
}
pub unsafe fn new_raw(data: *const T, lifetime: LifetimeRef) -> Self {
Self {
lifetime,
data: NonNull::new_unchecked(data as *mut _),
}
}
pub fn lifetime(&self) -> &LifetimeRef {
&self.lifetime
}
pub fn borrow(&self) -> Option<ManagedRef<T>> {
Some(ManagedRef {
lifetime: self.lifetime.borrow()?,
data: self.data,
})
}
pub fn read(&self) -> Option<ValueReadAccess<T>> {
self.lifetime.read(unsafe { self.data.as_ref() })
}
pub unsafe fn as_ptr(&self) -> Option<*const T> {
if self.lifetime.exists() {
Some(self.data.as_ptr())
} else {
None
}
}
}
pub struct ManagedRefMut<T> {
lifetime: LifetimeRefMut,
data: NonNull<T>,
}
unsafe impl<T> Send for ManagedRefMut<T> where T: Send {}
impl<T> ManagedRefMut<T> {
pub fn new(data: &mut T, lifetime: LifetimeRefMut) -> Self {
Self {
lifetime,
data: unsafe { NonNull::new_unchecked(data as *mut T) },
}
}
pub unsafe fn new_raw(data: *mut T, lifetime: LifetimeRefMut) -> Self {
Self {
lifetime,
data: NonNull::new_unchecked(data),
}
}
pub fn lifetime(&self) -> &LifetimeRefMut {
&self.lifetime
}
pub fn borrow(&self) -> Option<ManagedRef<T>> {
Some(ManagedRef {
lifetime: self.lifetime.borrow()?,
data: self.data,
})
}
pub fn borrow_mut(&self) -> Option<ManagedRefMut<T>> {
Some(ManagedRefMut {
lifetime: self.lifetime.borrow_mut()?,
data: self.data,
})
}
pub fn read(&self) -> Option<ValueReadAccess<T>> {
self.lifetime.read(unsafe { self.data.as_ref() })
}
pub fn write(&mut self) -> Option<ValueWriteAccess<T>> {
self.lifetime.write(unsafe { self.data.as_mut() })
}
pub unsafe fn as_ptr(&self) -> Option<*const T> {
if self.lifetime.exists() {
Some(self.data.as_ptr())
} else {
None
}
}
pub unsafe fn as_mut_ptr(&mut self) -> Option<*mut T> {
if self.lifetime.exists() {
Some(self.data.as_ptr())
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use crate::managed::{Managed, ManagedRef, ManagedRefMut};
fn is_async<T: Send + 'static>() {
println!("{} is send!", std::any::type_name::<T>());
}
#[test]
fn test_managed() {
is_async::<Managed<()>>();
is_async::<ManagedRef<()>>();
is_async::<ManagedRefMut<()>>();
let mut value = Managed::new(42);
let value_ref = value.borrow().unwrap();
assert!(value.borrow().is_some());
assert!(value.borrow_mut().is_none());
drop(value_ref);
assert!(value.borrow().is_some());
assert!(value.borrow_mut().is_some());
*value.write().unwrap() = 40;
assert_eq!(*value.read().unwrap(), 40);
*value.borrow_mut().unwrap().write().unwrap() = 2;
assert_eq!(*value.read().unwrap(), 2);
let value_ref = value.borrow().unwrap();
drop(value);
assert!(value_ref.read().is_none());
}
}