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
use std::cell::UnsafeCell;
use crate::{AQueue, AResult};
use std::future::Future;
use std::sync::Arc;
pub struct InnerStore<T>(UnsafeCell<T>);
unsafe impl<T> Sync for InnerStore<T>{}
unsafe impl<T> Send for InnerStore<T>{}
impl<T> InnerStore<T>{
#[inline]
fn new(x:T)-> InnerStore<T>{
InnerStore(UnsafeCell::new(x))
}
#[inline]
pub fn get_mut(&self)->&mut T{
unsafe {
&mut *self.0.get()
}
}
#[inline]
pub fn get(&self)->&T {
unsafe {
&*self.0.get()
}
}
}
pub struct Actor<I>{
inner:Arc<InnerStore<I>>,
queue:AQueue
}
impl<I:'static> Actor<I>{
#[inline]
pub fn new(x:I)->Actor<I>{
Actor{
inner:Arc::new(InnerStore::new(x)),
queue:AQueue::new()
}
}
#[inline]
pub async fn inner_call<T,S>(&self, call:impl FnOnce(Arc<InnerStore<I>>)->T+ Send+Sync+'static) ->AResult<S>
where T:Future<Output = AResult<S>> + Send+ Sync+'static,
S:'static {
unsafe {
self.queue.run(call, self.inner.clone()).await
}
}
}