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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::{
alloc::Allocator,
marker::PhantomData,
mem::MaybeUninit,
ops::{Deref, DerefMut, Index, IndexMut},
};
use crate::{
device::{cpu::Cpu, DefaultDeviceAllocator, Device, DevicePtr},
vec::Vec,
};
pub struct Ref<T: ?Sized, D: Device> {
_device: PhantomData<D>,
inner: T,
}
impl<T: ?Sized, D: Device> Ref<T, D> {
pub unsafe fn from_ptr<'a>(ptr: D::Ptr<T>) -> &'a Self {
&*(ptr.as_raw() as *const Self)
}
pub unsafe fn from_ptr_mut<'a>(ptr: D::Ptr<T>) -> &'a mut Self {
&mut *(ptr.as_raw() as *mut Self)
}
}
impl<T, D: Device> Ref<[T], D> {
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn as_ptr(&self) -> D::Ptr<T> {
D::Ptr::from_raw(&self.inner as *const [T] as *mut T)
}
pub fn as_slice_ptr(&self) -> D::Ptr<[T]> {
D::Ptr::from_raw(&self.inner as *const [T] as *mut [T])
}
}
impl<'a, T, D: Device> Ref<[MaybeUninit<T>], D> {
pub unsafe fn assume_init(&self) -> &Ref<[T], D> {
&*(MaybeUninit::slice_assume_init_ref(&self.inner) as *const [T] as *const _)
}
pub unsafe fn assume_init_mut(&mut self) -> &mut Ref<[T], D> {
&mut *(MaybeUninit::slice_assume_init_mut(&mut self.inner) as *mut [T] as *mut _)
}
}
impl<T: Copy, D: Device> Ref<[T], D> {
pub fn copy_from_slice(&mut self, from: &Self) {
D::copy(from, self)
}
pub fn copy_from_host(&mut self, from: &[T]) {
D::copy_from_host(from, self)
}
pub fn copy_to_host(&self, to: &mut [T]) {
D::copy_to_host(self, to)
}
}
impl<T: Copy, D: Device> Ref<[MaybeUninit<T>], D> {
pub fn init_from_slice(&mut self, from: &Ref<[T], D>) {
unsafe {
let ptr = self as *mut _ as *mut Ref<[T], D>;
(*ptr).copy_from_slice(from)
}
}
pub fn init_from_host(&mut self, from: &[T]) {
unsafe {
let ptr = self as *mut _ as *mut Ref<[T], D>;
(*ptr).copy_from_host(from)
}
}
}
impl<T: Copy, D: DefaultDeviceAllocator> ToOwned for Ref<[T], D> {
type Owned = Vec<T, D::Alloc>;
fn to_owned(&self) -> Self::Owned {
unsafe {
let mut v = Vec::with_capacity_in(self.len(), D::Alloc::default());
let buf = &mut v.space_capacity_mut()[..self.len()];
buf.init_from_slice(self);
v.set_len(self.len());
v
}
}
}
impl<T> Deref for Ref<[T], Cpu> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for Ref<[T], Cpu> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<'a, T, D: Device> AsRef<Ref<[T], D>> for &'a Ref<[T], D> {
fn as_ref(&self) -> &Ref<[T], D> {
self
}
}
impl<'a, T, D: Device> AsRef<Ref<[T], D>> for &'a mut Ref<[T], D> {
fn as_ref(&self) -> &Ref<[T], D> {
self
}
}
impl<'a, T, D: Device> AsMut<Ref<[T], D>> for &'a mut Ref<[T], D> {
fn as_mut(&mut self) -> &mut Ref<[T], D> {
self
}
}
impl<T, D: Device, S> Index<S> for Ref<[T], D>
where
[T]: Index<S, Output = [T]>,
{
type Output = Self;
fn index(&self, index: S) -> &Self::Output {
unsafe { &*(&self.inner[index] as *const [T] as *const Self) }
}
}
impl<T, D: Device, S> IndexMut<S> for Ref<[T], D>
where
[T]: IndexMut<S, Output = [T]>,
{
fn index_mut(&mut self, index: S) -> &mut Self::Output {
unsafe { &mut *(&mut self.inner[index] as *mut [T] as *mut Self) }
}
}
impl<T, const N: usize> AsRef<Ref<[T], Cpu>> for [T; N] {
fn as_ref(&self) -> &Ref<[T], Cpu> {
unsafe { &*(self.as_slice() as *const [T] as *const Ref<[T], Cpu>) }
}
}
impl<T, const N: usize> AsMut<Ref<[T], Cpu>> for [T; N] {
fn as_mut(&mut self) -> &mut Ref<[T], Cpu> {
unsafe { &mut *(self.as_mut_slice() as *mut [T] as *mut Ref<[T], Cpu>) }
}
}
impl<T, A: Allocator> AsRef<Ref<[T], Cpu>> for std::vec::Vec<T, A> {
fn as_ref(&self) -> &Ref<[T], Cpu> {
unsafe { &*(self.as_slice() as *const [T] as *const Ref<[T], Cpu>) }
}
}
impl<T, A: Allocator> AsMut<Ref<[T], Cpu>> for std::vec::Vec<T, A> {
fn as_mut(&mut self) -> &mut Ref<[T], Cpu> {
unsafe { &mut *(self.as_mut_slice() as *mut [T] as *mut Ref<[T], Cpu>) }
}
}