poulpy/poulpy-hal/src/api/scratch.rs
poulpy-hal/src/api/scratch.rsRaw
1use crate::layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat};
2
3/// Allocates a new [crate::layouts::ScratchOwned] of `size` aligned bytes.
4pub trait ScratchOwnedAlloc<B: Backend> {
5 fn alloc(size: usize) -> Self;
6}
7
8/// Borrows a slice of bytes into a [Scratch].
9pub trait ScratchOwnedBorrow<B: Backend> {
10 fn borrow(&mut self) -> &mut Scratch<B>;
11}
12
13/// Wrap an array of mutable borrowed bytes into a [Scratch].
14pub trait ScratchFromBytes<B: Backend> {
15 fn from_bytes(data: &mut [u8]) -> &mut Scratch<B>;
16}
17
18/// Returns how many bytes left can be taken from the scratch.
19pub trait ScratchAvailable {
20 fn available(&self) -> usize;
21}
22
23/// Takes a slice of bytes from a [Scratch] and return a new [Scratch] minus the taken array of bytes.
24pub trait TakeSlice {
25 fn take_slice<T>(&mut self, len: usize) -> (&mut [T], &mut Self);
26}
27
28/// Take a slice of bytes from a [Scratch], wraps it into a [ScalarZnx] and returns it
29/// as well as a new [Scratch] minus the taken array of bytes.
30pub trait TakeScalarZnx {
31 fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self);
32}
33
34/// Take a slice of bytes from a [Scratch], wraps it into a [SvpPPol] and returns it
35/// as well as a new [Scratch] minus the taken array of bytes.
36pub trait TakeSvpPPol<B: Backend> {
37 fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self);
38}
39
40/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnx] and returns it
41/// as well as a new [Scratch] minus the taken array of bytes.
42pub trait TakeVecZnx {
43 fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self);
44}
45
46/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnx] aand returns it
47/// as well as a new [Scratch] minus the taken array of bytes.
48pub trait TakeVecZnxSlice {
49 fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec<VecZnx<&mut [u8]>>, &mut Self);
50}
51
52/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxBig] and returns it
53/// as well as a new [Scratch] minus the taken array of bytes.
54pub trait TakeVecZnxBig<B: Backend> {
55 fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self);
56}
57
58/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxDft] and returns it
59/// as well as a new [Scratch] minus the taken array of bytes.
60pub trait TakeVecZnxDft<B: Backend> {
61 fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self);
62}
63
64/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnxDft] and returns it
65/// as well as a new [Scratch] minus the taken array of bytes.
66pub trait TakeVecZnxDftSlice<B: Backend> {
67 fn take_vec_znx_dft_slice(
68 &mut self,
69 len: usize,
70 n: usize,
71 cols: usize,
72 size: usize,
73 ) -> (Vec<VecZnxDft<&mut [u8], B>>, &mut Self);
74}
75
76/// Take a slice of bytes from a [Scratch], wraps it into a [VmpPMat] and returns it
77/// as well as a new [Scratch] minus the taken array of bytes.
78pub trait TakeVmpPMat<B: Backend> {
79 fn take_vmp_pmat(
80 &mut self,
81 n: usize,
82 rows: usize,
83 cols_in: usize,
84 cols_out: usize,
85 size: usize,
86 ) -> (VmpPMat<&mut [u8], B>, &mut Self);
87}
88
89/// Take a slice of bytes from a [Scratch], wraps it into a [MatZnx] and returns it
90/// as well as a new [Scratch] minus the taken array of bytes.
91pub trait TakeMatZnx {
92 fn take_mat_znx(
93 &mut self,
94 n: usize,
95 rows: usize,
96 cols_in: usize,
97 cols_out: usize,
98 size: usize,
99 ) -> (MatZnx<&mut [u8]>, &mut Self);
100}