rstsr_core/storage/
device.rs1use crate::prelude_dev::*;
2
3pub trait DeviceBaseAPI {
4 fn same_device(&self, other: &Self) -> bool;
5 fn default_order(&self) -> FlagOrder;
6 fn set_default_order(&mut self, order: FlagOrder);
7}
8
9pub trait DeviceRawAPI<T>: DeviceBaseAPI + Clone {
10 type Raw;
11}
12
13#[derive(Debug)]
14pub struct Storage<R, T, B>
15where
16 B: DeviceRawAPI<T>,
17{
18 pub(crate) data: R,
19 pub(crate) device: B,
20 _phantom: PhantomData<T>,
21}
22
23pub trait DeviceStorageAPI<T>: DeviceRawAPI<T> {
24 fn len<R>(storage: &Storage<R, T, Self>) -> usize
25 where
26 R: DataAPI<Data = Self::Raw>;
27 fn is_empty<R>(storage: &Storage<R, T, Self>) -> bool
28 where
29 R: DataAPI<Data = Self::Raw>,
30 {
31 Self::len::<R>(storage) == 0
32 }
33 fn to_cpu_vec<R>(storage: &Storage<R, T, Self>) -> Result<Vec<T>>
34 where
35 Self::Raw: Clone,
36 R: DataAPI<Data = Self::Raw>;
37 fn into_cpu_vec<R>(storage: Storage<R, T, Self>) -> Result<Vec<T>>
38 where
39 Self::Raw: Clone,
40 R: DataCloneAPI<Data = Self::Raw>;
41 fn get_index<R>(storage: &Storage<R, T, Self>, index: usize) -> T
42 where
43 T: Clone,
44 R: DataAPI<Data = Self::Raw>;
45 fn get_index_ptr<R>(storage: &Storage<R, T, Self>, index: usize) -> *const T
46 where
47 R: DataAPI<Data = Self::Raw>;
48 fn get_index_mut_ptr<R>(storage: &mut Storage<R, T, Self>, index: usize) -> *mut T
49 where
50 R: DataMutAPI<Data = Self::Raw>;
51 fn set_index<R>(storage: &mut Storage<R, T, Self>, index: usize, value: T)
52 where
53 R: DataMutAPI<Data = Self::Raw>;
54}
55
56impl<R, T, B> Storage<R, T, B>
57where
58 B: DeviceStorageAPI<T>,
59 R: DataAPI<Data = B::Raw>,
60{
61 pub fn device(&self) -> &B {
62 &self.device
63 }
64
65 pub fn device_mut(&mut self) -> &mut B {
66 &mut self.device
67 }
68
69 pub fn data(&self) -> &R {
70 &self.data
71 }
72
73 pub fn data_mut(&mut self) -> &mut R {
74 &mut self.data
75 }
76
77 pub fn into_raw_parts(self) -> (R, B) {
78 (self.data, self.device)
79 }
80
81 pub fn new(data: R, device: B) -> Self {
82 Self { data, device, _phantom: PhantomData }
83 }
84
85 pub fn len(&self) -> usize {
86 B::len(self)
87 }
88
89 pub fn is_empty(&self) -> bool {
90 B::is_empty(self)
91 }
92
93 pub fn to_cpu_vec(&self) -> Result<Vec<T>>
94 where
95 B::Raw: Clone,
96 R: DataCloneAPI<Data = B::Raw>,
97 {
98 B::to_cpu_vec(self)
99 }
100
101 pub fn into_cpu_vec(self) -> Result<Vec<T>>
102 where
103 B::Raw: Clone,
104 R: DataCloneAPI<Data = B::Raw>,
105 {
106 B::into_cpu_vec(self)
107 }
108
109 #[inline]
110 pub fn get_index(&self, index: usize) -> T
111 where
112 T: Clone,
113 {
114 B::get_index(self, index)
115 }
116
117 #[inline]
118 pub fn get_index_ptr(&self, index: usize) -> *const T {
119 B::get_index_ptr(self, index)
120 }
121
122 #[inline]
123 pub fn get_index_mut_ptr(&mut self, index: usize) -> *mut T
124 where
125 R: DataMutAPI<Data = B::Raw>,
126 {
127 B::get_index_mut_ptr(self, index)
128 }
129
130 #[inline]
131 pub fn set_index(&mut self, index: usize, value: T)
132 where
133 R: DataMutAPI<Data = B::Raw>,
134 {
135 B::set_index(self, index, value)
136 }
137}
138
139impl<R, T, B> Storage<R, T, B>
140where
141 R: DataAPI<Data = B::Raw>,
142 B: DeviceRawAPI<T>,
143{
144 pub fn raw(&self) -> &B::Raw {
145 self.data.raw()
146 }
147}
148
149impl<R, T, B> Storage<R, T, B>
150where
151 R: DataMutAPI<Data = B::Raw>,
152 B: DeviceRawAPI<T>,
153{
154 pub fn raw_mut(&mut self) -> &mut B::Raw {
155 self.data.raw_mut()
156 }
157}
158
159pub trait DeviceAPI<T>: DeviceBaseAPI + DeviceRawAPI<T> + DeviceStorageAPI<T> + Clone + Default {}