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
use std::{any::TypeId, cmp::Ordering, ptr::NonNull};
use atomic_refcell::AtomicRefCell;
use crate::{borrow::ContextBorrow, Error, IntoAccess, Result};
use hecs::Component;
pub struct Context<'a> {
data: &'a dyn Data,
}
unsafe impl Send for Context<'_> {}
unsafe impl Sync for Context<'_> {}
mod erased_cell;
use erased_cell::*;
impl<'a> Context<'a> {
pub fn new(data: &'a dyn Data) -> Context {
Self { data }
}
pub fn borrow<T>(&'a self) -> Result<T::Target>
where
T: ContextBorrow<'a>,
{
T::borrow(self)
}
pub fn cell<T: IntoAccess>(&'a self) -> Result<&AtomicRefCell<NonNull<u8>>> {
let access = T::access();
self.data
.get(access.id())
.ok_or_else(|| Error::MissingData(access.name()))
}
}
pub trait Data {
fn get(&self, ty: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>>;
}
pub trait IntoData<With>: Send + Sync {
type Target: Data;
unsafe fn into_data(self, with: &mut With) -> Self::Target;
}
impl<With: Component> IntoData<With> for () {
type Target = [ErasedCell; 1];
unsafe fn into_data(self, with: &mut With) -> Self::Target {
[ErasedCell::from_ref(with)]
}
}
macro_rules! tuple_impl {
() => {};
($([$idx: tt => $name: ident]),*) => {
impl<$( $name ), *, With> IntoData<With> for ($(&mut $name,) *)
where
With: Component,
$($name: Component), *
{
type Target = [ErasedCell; 1 + count!($($name )*)];
unsafe fn into_data(self, with: &mut With) -> Self::Target {
let mut val = [
$( ErasedCell::from_ref::<$name>(self.$idx),)*
ErasedCell::from_ref(with),
];
val.sort_unstable();
val
}
}
};
}
impl_for_tuples_idx!(tuple_impl);
impl<const C: usize> Data for [ErasedCell; C] {
fn get(&self, ty: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>> {
let mut low = 0;
let mut high = C - 1;
while low <= high {
let mid = (high - low) / 2 + low;
let val = &self[mid];
match val.cmp_id(ty) {
Ordering::Less => low = mid + 1,
Ordering::Equal => return Some(&val.cell),
Ordering::Greater if mid == 0 => break,
Ordering::Greater => high = mid - 1,
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::{Context, IntoData};
#[test]
fn context() {
let mut a = 64_i32;
let mut b = "Hello, World";
let data = unsafe { (&mut a, &mut b).into_data(&mut ()) };
let context = Context::new(&data);
{
let a = context.borrow::<&i32>().unwrap();
let mut b = context.borrow::<&mut &str>().unwrap();
assert_eq!(*b, "Hello, World");
*b = "Foo Fighters";
drop(b);
let b = context.borrow::<&&str>().unwrap();
assert_eq!(*a, 64);
assert_eq!(*b, "Foo Fighters");
let c = context.borrow::<&f32>();
assert!(c.is_err());
}
}
}