use std::marker::PhantomData; use crate::{ alloc_aligned, layouts::{Backend, Data, DataMut, DataRef, DataView, DataViewMut, ZnxInfos, ZnxView}, oep::CnvPVecBytesOfImpl, }; pub struct CnvPVecR { data: D, n: usize, size: usize, cols: usize, _phantom: PhantomData, } impl ZnxInfos for CnvPVecR { fn cols(&self) -> usize { self.cols } fn n(&self) -> usize { self.n } fn rows(&self) -> usize { 1 } fn size(&self) -> usize { self.size } } impl DataView for CnvPVecR { type D = D; fn data(&self) -> &Self::D { &self.data } } impl DataViewMut for CnvPVecR { fn data_mut(&mut self) -> &mut Self::D { &mut self.data } } impl ZnxView for CnvPVecR { type Scalar = BE::ScalarPrep; } impl>, B: Backend> CnvPVecR where B: CnvPVecBytesOfImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { let data: Vec = alloc_aligned::(B::bytes_of_cnv_pvec_right_impl(n, cols, size)); Self { data: data.into(), n, size, cols, _phantom: PhantomData, } } pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); assert!(data.len() == B::bytes_of_cnv_pvec_right_impl(n, cols, size)); Self { data: data.into(), n, size, cols, _phantom: PhantomData, } } } impl CnvPVecR { pub fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self { Self { data, n, cols, size, _phantom: PhantomData, } } } pub struct CnvPVecL { data: D, n: usize, size: usize, cols: usize, _phantom: PhantomData, } impl ZnxInfos for CnvPVecL { fn cols(&self) -> usize { self.cols } fn n(&self) -> usize { self.n } fn rows(&self) -> usize { 1 } fn size(&self) -> usize { self.size } } impl DataView for CnvPVecL { type D = D; fn data(&self) -> &Self::D { &self.data } } impl DataViewMut for CnvPVecL { fn data_mut(&mut self) -> &mut Self::D { &mut self.data } } impl ZnxView for CnvPVecL { type Scalar = BE::ScalarPrep; } impl>, B: Backend> CnvPVecL where B: CnvPVecBytesOfImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { let data: Vec = alloc_aligned::(B::bytes_of_cnv_pvec_left_impl(n, cols, size)); Self { data: data.into(), n, size, cols, _phantom: PhantomData, } } pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); assert!(data.len() == B::bytes_of_cnv_pvec_left_impl(n, cols, size)); Self { data: data.into(), n, size, cols, _phantom: PhantomData, } } } impl CnvPVecL { pub fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self { Self { data, n, cols, size, _phantom: PhantomData, } } } pub trait CnvPVecRToRef { fn to_ref(&self) -> CnvPVecR<&[u8], BE>; } impl CnvPVecRToRef for CnvPVecR { fn to_ref(&self) -> CnvPVecR<&[u8], BE> { CnvPVecR { data: self.data.as_ref(), n: self.n, size: self.size, cols: self.cols, _phantom: self._phantom, } } } pub trait CnvPVecRToMut { fn to_mut(&mut self) -> CnvPVecR<&mut [u8], BE>; } impl CnvPVecRToMut for CnvPVecR { fn to_mut(&mut self) -> CnvPVecR<&mut [u8], BE> { CnvPVecR { data: self.data.as_mut(), n: self.n, size: self.size, cols: self.cols, _phantom: self._phantom, } } } pub trait CnvPVecLToRef { fn to_ref(&self) -> CnvPVecL<&[u8], BE>; } impl CnvPVecLToRef for CnvPVecL { fn to_ref(&self) -> CnvPVecL<&[u8], BE> { CnvPVecL { data: self.data.as_ref(), n: self.n, size: self.size, cols: self.cols, _phantom: self._phantom, } } } pub trait CnvPVecLToMut { fn to_mut(&mut self) -> CnvPVecL<&mut [u8], BE>; } impl CnvPVecLToMut for CnvPVecL { fn to_mut(&mut self) -> CnvPVecL<&mut [u8], BE> { CnvPVecL { data: self.data.as_mut(), n: self.n, size: self.size, cols: self.cols, _phantom: self._phantom, } } }