tensorgraph_math/
storage.rs1use std::alloc::Allocator;
2
3use tensorgraph_sys::{
4 device::{cpu::Cpu, DefaultDeviceAllocator, Device, DeviceAllocator},
5 ptr::Ref,
6 DefaultVec, Vec,
7};
8
9pub trait IntoOwned {
11 type Owned;
12 fn into_owned(self) -> Self::Owned;
13}
14
15pub trait Storage: AsRef<Ref<[Self::T], Self::Device>> {
17 type T;
18 type Device: Device;
19}
20
21pub trait StorageMut: Storage + AsMut<Ref<[Self::T], Self::Device>> {}
23
24impl<T, A: DeviceAllocator> Storage for Vec<T, A> {
27 type T = T;
28 type Device = A::Device;
29}
30
31impl<T, A: DeviceAllocator> StorageMut for Vec<T, A> {}
32
33impl<T, A: DeviceAllocator> IntoOwned for Vec<T, A> {
34 type Owned = Self;
35 fn into_owned(self) -> Self::Owned {
36 self
37 }
38}
39
40impl<'a, T, D: Device> Storage for &'a Ref<[T], D> {
43 type T = T;
44 type Device = D;
45}
46
47impl<'a, T: Copy, D: DefaultDeviceAllocator> IntoOwned for &'a Ref<[T], D> {
48 type Owned = DefaultVec<T, D>;
49 fn into_owned(self) -> Self::Owned {
50 self.to_owned()
51 }
52}
53
54impl<'a, T, D: Device> Storage for &'a mut Ref<[T], D> {
57 type T = T;
58 type Device = D;
59}
60
61impl<'a, T, D: Device> StorageMut for &'a mut Ref<[T], D> {}
62
63impl<'a, T: Copy, D: DefaultDeviceAllocator> IntoOwned for &'a mut Ref<[T], D> {
64 type Owned = DefaultVec<T, D>;
65 fn into_owned(self) -> Self::Owned {
66 self.to_owned()
67 }
68}
69
70impl<T, const N: usize> Storage for [T; N] {
73 type T = T;
74 type Device = Cpu;
75}
76
77impl<T, const N: usize> StorageMut for [T; N] {}
78
79impl<T, const N: usize> IntoOwned for [T; N] {
80 type Owned = Self;
81 fn into_owned(self) -> Self::Owned {
82 self
83 }
84}
85
86impl<T, A: Allocator> Storage for std::vec::Vec<T, A> {
89 type T = T;
90 type Device = Cpu;
91}
92
93impl<T, A: Allocator> StorageMut for std::vec::Vec<T, A> {}
94
95impl<T, A: Allocator> IntoOwned for std::vec::Vec<T, A> {
96 type Owned = Self;
97 fn into_owned(self) -> Self::Owned {
98 self
99 }
100}
101
102impl<'a, T> Storage for &'a [T] {
105 type T = T;
106 type Device = Cpu;
107}
108
109impl<'a, T: Copy> IntoOwned for &'a [T] {
110 type Owned = std::vec::Vec<T>;
111 fn into_owned(self) -> Self::Owned {
112 self.to_owned()
113 }
114}
115
116impl<'a, T> Storage for &'a mut [T] {
119 type T = T;
120 type Device = Cpu;
121}
122
123impl<'a, T> StorageMut for &'a mut [T] {}
124
125impl<'a, T: Copy> IntoOwned for &'a mut [T] {
126 type Owned = std::vec::Vec<T>;
127 fn into_owned(self) -> Self::Owned {
128 self.to_owned()
129 }
130}