use crate::{Pointer, PointerType, Position, Read, ReadType, StaticSize, Write, WriteType}; use ndarray::prelude::*; use num::ToPrimitive; impl<'a, T> ReadType<'a> for Array1 where T: 'a + ReadType<'a>, T::ReadType: StaticSize + Read<'a>, { type ReadType = Pointer>; } impl<'a, T> Read<'a> for Array1 where T: StaticSize + Read<'a>, { type Output = Array1; fn read(bytes: &'a [u8], position: crate::Position) -> Self::Output { let len = PointerType::read(bytes, position.cast()) .to_usize() .unwrap(); let mut position = position.offset::(PointerType::STATIC_SIZE); let mut output = Vec::with_capacity(len); for _ in 0..len { output.push(T::read(bytes, position)); position = position.offset::(T::STATIC_SIZE); } Array1::from_vec(output) } } impl WriteType for Array1 where T: WriteType, { type WriteType = Position>; } impl Write for Array1 where T: Write, T::Output: Sized, { type Output = Array1; fn write(&self, writer: &mut crate::Writer) -> crate::Position { let position = writer.position(); let len = self.len() as PointerType; writer.write(&len); for value in self.iter() { writer.write(value); } position } } impl<'a, T> ReadType<'a> for Array2 where T: 'a + ReadType<'a>, T::ReadType: StaticSize + Read<'a>, { type ReadType = Pointer>; } impl<'a, T> Read<'a> for Array2 where T: StaticSize + Read<'a>, { type Output = Array2; fn read(bytes: &'a [u8], position: crate::Position) -> Self::Output { let nrows = PointerType::read(bytes, position.cast()) .to_usize() .unwrap(); let position = position.offset::(PointerType::STATIC_SIZE); let ncols = PointerType::read(bytes, position.cast()) .to_usize() .unwrap(); let mut position = position.offset::(PointerType::STATIC_SIZE); let mut output = Vec::with_capacity(nrows * ncols); for _ in 0..nrows * ncols { output.push(T::read(bytes, position)); position = position.offset::(T::STATIC_SIZE); } Array2::from_shape_vec((nrows, ncols), output).unwrap() } } impl WriteType for Array2 where T: WriteType, { type WriteType = Position>; } impl Write for Array2 where T: Write, T::Output: Sized, { type Output = Array2; fn write(&self, writer: &mut crate::Writer) -> crate::Position { let position = writer.position(); let nrows = self.nrows() as PointerType; writer.write(&nrows); let ncols = self.ncols() as PointerType; writer.write(&ncols); for value in self.iter() { writer.write(value); } position } }