poulpy_hal/delegates/
scratch.rs

1use crate::{
2    api::{
3        ScratchAvailable, ScratchFromBytes, ScratchOwnedAlloc, ScratchOwnedBorrow, TakeLike, TakeMatZnx, TakeScalarZnx,
4        TakeSlice, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, TakeVmpPMat,
5    },
6    layouts::{Backend, DataRef, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat},
7    oep::{
8        ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeLikeImpl, TakeMatZnxImpl,
9        TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl,
10        TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl,
11    },
12};
13
14impl<B> ScratchOwnedAlloc<B> for ScratchOwned<B>
15where
16    B: Backend + ScratchOwnedAllocImpl<B>,
17{
18    fn alloc(size: usize) -> Self {
19        B::scratch_owned_alloc_impl(size)
20    }
21}
22
23impl<B> ScratchOwnedBorrow<B> for ScratchOwned<B>
24where
25    B: Backend + ScratchOwnedBorrowImpl<B>,
26{
27    fn borrow(&mut self) -> &mut Scratch<B> {
28        B::scratch_owned_borrow_impl(self)
29    }
30}
31
32impl<B> ScratchFromBytes<B> for Scratch<B>
33where
34    B: Backend + ScratchFromBytesImpl<B>,
35{
36    fn from_bytes(data: &mut [u8]) -> &mut Scratch<B> {
37        B::scratch_from_bytes_impl(data)
38    }
39}
40
41impl<B> ScratchAvailable for Scratch<B>
42where
43    B: Backend + ScratchAvailableImpl<B>,
44{
45    fn available(&self) -> usize {
46        B::scratch_available_impl(self)
47    }
48}
49
50impl<B> TakeSlice for Scratch<B>
51where
52    B: Backend + TakeSliceImpl<B>,
53{
54    fn take_slice<T>(&mut self, len: usize) -> (&mut [T], &mut Self) {
55        B::take_slice_impl(self, len)
56    }
57}
58
59impl<B> TakeScalarZnx for Scratch<B>
60where
61    B: Backend + TakeScalarZnxImpl<B>,
62{
63    fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) {
64        B::take_scalar_znx_impl(self, n, cols)
65    }
66}
67
68impl<B> TakeSvpPPol<B> for Scratch<B>
69where
70    B: Backend + TakeSvpPPolImpl<B>,
71{
72    fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) {
73        B::take_svp_ppol_impl(self, n, cols)
74    }
75}
76
77impl<B> TakeVecZnx for Scratch<B>
78where
79    B: Backend + TakeVecZnxImpl<B>,
80{
81    fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) {
82        B::take_vec_znx_impl(self, n, cols, size)
83    }
84}
85
86impl<B> TakeVecZnxSlice for Scratch<B>
87where
88    B: Backend + TakeVecZnxSliceImpl<B>,
89{
90    fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec<VecZnx<&mut [u8]>>, &mut Self) {
91        B::take_vec_znx_slice_impl(self, len, n, cols, size)
92    }
93}
94
95impl<B> TakeVecZnxBig<B> for Scratch<B>
96where
97    B: Backend + TakeVecZnxBigImpl<B>,
98{
99    fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) {
100        B::take_vec_znx_big_impl(self, n, cols, size)
101    }
102}
103
104impl<B> TakeVecZnxDft<B> for Scratch<B>
105where
106    B: Backend + TakeVecZnxDftImpl<B>,
107{
108    fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) {
109        B::take_vec_znx_dft_impl(self, n, cols, size)
110    }
111}
112
113impl<B> TakeVecZnxDftSlice<B> for Scratch<B>
114where
115    B: Backend + TakeVecZnxDftSliceImpl<B>,
116{
117    fn take_vec_znx_dft_slice(
118        &mut self,
119        len: usize,
120        n: usize,
121        cols: usize,
122        size: usize,
123    ) -> (Vec<VecZnxDft<&mut [u8], B>>, &mut Self) {
124        B::take_vec_znx_dft_slice_impl(self, len, n, cols, size)
125    }
126}
127
128impl<B> TakeVmpPMat<B> for Scratch<B>
129where
130    B: Backend + TakeVmpPMatImpl<B>,
131{
132    fn take_vmp_pmat(
133        &mut self,
134        n: usize,
135        rows: usize,
136        cols_in: usize,
137        cols_out: usize,
138        size: usize,
139    ) -> (VmpPMat<&mut [u8], B>, &mut Self) {
140        B::take_vmp_pmat_impl(self, n, rows, cols_in, cols_out, size)
141    }
142}
143
144impl<B> TakeMatZnx for Scratch<B>
145where
146    B: Backend + TakeMatZnxImpl<B>,
147{
148    fn take_mat_znx(
149        &mut self,
150        n: usize,
151        rows: usize,
152        cols_in: usize,
153        cols_out: usize,
154        size: usize,
155    ) -> (MatZnx<&mut [u8]>, &mut Self) {
156        B::take_mat_znx_impl(self, n, rows, cols_in, cols_out, size)
157    }
158}
159
160impl<'a, B: Backend, D> TakeLike<'a, B, ScalarZnx<D>> for Scratch<B>
161where
162    B: TakeLikeImpl<'a, B, ScalarZnx<D>, Output = ScalarZnx<&'a mut [u8]>>,
163    D: DataRef,
164{
165    type Output = ScalarZnx<&'a mut [u8]>;
166    fn take_like(&'a mut self, template: &ScalarZnx<D>) -> (Self::Output, &'a mut Self) {
167        B::take_like_impl(self, template)
168    }
169}
170
171impl<'a, B: Backend, D> TakeLike<'a, B, SvpPPol<D, B>> for Scratch<B>
172where
173    B: TakeLikeImpl<'a, B, SvpPPol<D, B>, Output = SvpPPol<&'a mut [u8], B>>,
174    D: DataRef,
175{
176    type Output = SvpPPol<&'a mut [u8], B>;
177    fn take_like(&'a mut self, template: &SvpPPol<D, B>) -> (Self::Output, &'a mut Self) {
178        B::take_like_impl(self, template)
179    }
180}
181
182impl<'a, B: Backend, D> TakeLike<'a, B, VecZnx<D>> for Scratch<B>
183where
184    B: TakeLikeImpl<'a, B, VecZnx<D>, Output = VecZnx<&'a mut [u8]>>,
185    D: DataRef,
186{
187    type Output = VecZnx<&'a mut [u8]>;
188    fn take_like(&'a mut self, template: &VecZnx<D>) -> (Self::Output, &'a mut Self) {
189        B::take_like_impl(self, template)
190    }
191}
192
193impl<'a, B: Backend, D> TakeLike<'a, B, VecZnxBig<D, B>> for Scratch<B>
194where
195    B: TakeLikeImpl<'a, B, VecZnxBig<D, B>, Output = VecZnxBig<&'a mut [u8], B>>,
196    D: DataRef,
197{
198    type Output = VecZnxBig<&'a mut [u8], B>;
199    fn take_like(&'a mut self, template: &VecZnxBig<D, B>) -> (Self::Output, &'a mut Self) {
200        B::take_like_impl(self, template)
201    }
202}
203
204impl<'a, B: Backend, D> TakeLike<'a, B, VecZnxDft<D, B>> for Scratch<B>
205where
206    B: TakeLikeImpl<'a, B, VecZnxDft<D, B>, Output = VecZnxDft<&'a mut [u8], B>>,
207    D: DataRef,
208{
209    type Output = VecZnxDft<&'a mut [u8], B>;
210    fn take_like(&'a mut self, template: &VecZnxDft<D, B>) -> (Self::Output, &'a mut Self) {
211        B::take_like_impl(self, template)
212    }
213}
214
215impl<'a, B: Backend, D> TakeLike<'a, B, MatZnx<D>> for Scratch<B>
216where
217    B: TakeLikeImpl<'a, B, MatZnx<D>, Output = MatZnx<&'a mut [u8]>>,
218    D: DataRef,
219{
220    type Output = MatZnx<&'a mut [u8]>;
221    fn take_like(&'a mut self, template: &MatZnx<D>) -> (Self::Output, &'a mut Self) {
222        B::take_like_impl(self, template)
223    }
224}
225
226impl<'a, B: Backend, D> TakeLike<'a, B, VmpPMat<D, B>> for Scratch<B>
227where
228    B: TakeLikeImpl<'a, B, VmpPMat<D, B>, Output = VmpPMat<&'a mut [u8], B>>,
229    D: DataRef,
230{
231    type Output = VmpPMat<&'a mut [u8], B>;
232    fn take_like(&'a mut self, template: &VmpPMat<D, B>) -> (Self::Output, &'a mut Self) {
233        B::take_like_impl(self, template)
234    }
235}