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
use crate::{
component::Component,
entity::Null,
};
use alloc::vec::Vec;
use core::mem::ManuallyDrop;
pub trait Storage {
/// Push the components contained in this heterogeneous list into component columns.
///
/// This consumes the entity, moving the components into their appropriate columns.
///
/// The components are stored within the `Vec<C>`s defined by `components` and `length`. This
/// assumes the components in both the entity and the components columns are in the same order.
///
/// # Safety
/// The components in both the entity and `components` much correspond to the same components
/// in the same order.
///
/// `components`, together with `length`, must define a valid `Vec<C>` for each component.
unsafe fn push_components(self, components: &mut [(*mut u8, usize)], length: usize);
/// Reserve capacity for `additional` components in the component columns.
///
/// # Safety
/// The components in `components` must correspond to the same components in this entity in the
/// same order.
///
/// `components`, together with `length`, must define a valid `Vec<C>` for each component.
unsafe fn reserve_components(
components: &mut [(*mut u8, usize)],
length: usize,
additional: usize,
);
}
impl Storage for Null {
unsafe fn push_components(self, _components: &mut [(*mut u8, usize)], _length: usize) {}
unsafe fn reserve_components(
_components: &mut [(*mut u8, usize)],
_length: usize,
_additional: usize,
) {
}
}
impl<C, E> Storage for (C, E)
where
C: Component,
E: Storage,
{
unsafe fn push_components(self, components: &mut [(*mut u8, usize)], length: usize) {
// SAFETY: `components` is guaranteed by the safety contract of this method to contain a
// column for component `C` as its first value.
let component_column = unsafe { components.get_unchecked_mut(0) };
let mut v = ManuallyDrop::new(
// SAFETY: The `component_column` extracted from `components` is guaranteed to,
// together with `length`, define a valid `Vec<C>` for the current `C`.
unsafe {
Vec::<C>::from_raw_parts(component_column.0.cast::<C>(), length, component_column.1)
},
);
v.push(self.0);
*component_column = (v.as_mut_ptr().cast::<u8>(), v.capacity());
// SAFETY: Since `components` and `length` all meet the safety requirements for the current
// method body, they will meet those same requirements for this method call.
unsafe { E::push_components(self.1, components.get_unchecked_mut(1..), length) };
}
unsafe fn reserve_components(
components: &mut [(*mut u8, usize)],
length: usize,
additional: usize,
) {
// SAFETY: `components` is guaranteed by the safety contract of this method to contain a
// column for component `C` as its first value.
let component_column = unsafe { components.get_unchecked_mut(0) };
let mut v = ManuallyDrop::new(
// SAFETY: The `component_column` extracted from `components` is guaranteed to,
// together with `length`, define a valid `Vec<C>` for the current `C`.
unsafe {
Vec::<C>::from_raw_parts(component_column.0.cast::<C>(), length, component_column.1)
},
);
v.reserve(additional);
*component_column = (v.as_mut_ptr().cast::<u8>(), v.capacity());
// SAFETY: Since `components` and `length` all meet the safety requirements for the current
// method body, they will meet those same requirements for this method call.
unsafe { E::reserve_components(components.get_unchecked_mut(1..), length, additional) };
}
}