rstsr_core/storage/
device.rs

1use 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 data(&self) -> &R {
66        &self.data
67    }
68
69    pub fn data_mut(&mut self) -> &mut R {
70        &mut self.data
71    }
72
73    pub fn into_raw_parts(self) -> (R, B) {
74        (self.data, self.device)
75    }
76
77    pub fn new(data: R, device: B) -> Self {
78        Self { data, device, _phantom: PhantomData }
79    }
80
81    pub fn len(&self) -> usize {
82        B::len(self)
83    }
84
85    pub fn is_empty(&self) -> bool {
86        B::is_empty(self)
87    }
88
89    pub fn to_cpu_vec(&self) -> Result<Vec<T>>
90    where
91        B::Raw: Clone,
92        R: DataCloneAPI<Data = B::Raw>,
93    {
94        B::to_cpu_vec(self)
95    }
96
97    pub fn into_cpu_vec(self) -> Result<Vec<T>>
98    where
99        B::Raw: Clone,
100        R: DataCloneAPI<Data = B::Raw>,
101    {
102        B::into_cpu_vec(self)
103    }
104
105    #[inline]
106    pub fn get_index(&self, index: usize) -> T
107    where
108        T: Clone,
109    {
110        B::get_index(self, index)
111    }
112
113    #[inline]
114    pub fn get_index_ptr(&self, index: usize) -> *const T {
115        B::get_index_ptr(self, index)
116    }
117
118    #[inline]
119    pub fn get_index_mut_ptr(&mut self, index: usize) -> *mut T
120    where
121        R: DataMutAPI<Data = B::Raw>,
122    {
123        B::get_index_mut_ptr(self, index)
124    }
125
126    #[inline]
127    pub fn set_index(&mut self, index: usize, value: T)
128    where
129        R: DataMutAPI<Data = B::Raw>,
130    {
131        B::set_index(self, index, value)
132    }
133}
134
135impl<R, T, B> Storage<R, T, B>
136where
137    R: DataAPI<Data = B::Raw>,
138    B: DeviceRawAPI<T>,
139{
140    pub fn raw(&self) -> &B::Raw {
141        self.data.raw()
142    }
143}
144
145impl<R, T, B> Storage<R, T, B>
146where
147    R: DataMutAPI<Data = B::Raw>,
148    B: DeviceRawAPI<T>,
149{
150    pub fn raw_mut(&mut self) -> &mut B::Raw {
151        self.data.raw_mut()
152    }
153}
154
155pub trait DeviceAPI<T>: DeviceBaseAPI + DeviceRawAPI<T> + DeviceStorageAPI<T> + Clone + Default {}