|
| 1 | +use crate::imp_prelude::*; |
| 2 | +use crate::IntoDimension; |
| 3 | +use alloc::vec::Vec; |
| 4 | +use borsh::{BorshDeserialize, BorshSerialize}; |
| 5 | +use core::ops::Deref; |
| 6 | + |
| 7 | +/// **Requires crate feature `"borsh"`** |
| 8 | +impl<I> BorshSerialize for Dim<I> |
| 9 | +where |
| 10 | + I: BorshSerialize, |
| 11 | +{ |
| 12 | + fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> { |
| 13 | + <I as BorshSerialize>::serialize(&self.ix(), writer) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/// **Requires crate feature `"borsh"`** |
| 18 | +impl<I> BorshDeserialize for Dim<I> |
| 19 | +where |
| 20 | + I: BorshDeserialize, |
| 21 | +{ |
| 22 | + fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> { |
| 23 | + <I as BorshDeserialize>::deserialize_reader(reader).map(Dim::new) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// **Requires crate feature `"borsh"`** |
| 28 | +impl BorshSerialize for IxDyn { |
| 29 | + fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> { |
| 30 | + let elts = self.ix().deref(); |
| 31 | + // Output length of dimensions. |
| 32 | + <usize as BorshSerialize>::serialize(&elts.len(), writer)?; |
| 33 | + // Followed by actual data. |
| 34 | + for elt in elts { |
| 35 | + <Ix as BorshSerialize>::serialize(elt, writer)?; |
| 36 | + } |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// **Requires crate feature `"borsh"`** |
| 42 | +impl BorshDeserialize for IxDyn { |
| 43 | + fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> { |
| 44 | + // Deserialize the length. |
| 45 | + let len = <usize as BorshDeserialize>::deserialize_reader(reader)?; |
| 46 | + // Deserialize the given number of elements. We assume the source is |
| 47 | + // trusted so we use a capacity hint... |
| 48 | + let mut elts = Vec::with_capacity(len); |
| 49 | + for _ix in 0..len { |
| 50 | + elts.push(<Ix as BorshDeserialize>::deserialize_reader(reader)?); |
| 51 | + } |
| 52 | + Ok(elts.into_dimension()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// **Requires crate feature `"borsh"`** |
| 57 | +impl<A, D, S> BorshSerialize for ArrayBase<S, D> |
| 58 | +where |
| 59 | + A: BorshSerialize, |
| 60 | + D: Dimension + BorshSerialize, |
| 61 | + S: Data<Elem = A>, |
| 62 | +{ |
| 63 | + fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> { |
| 64 | + // Dimensions |
| 65 | + <D as BorshSerialize>::serialize(&self.raw_dim(), writer)?; |
| 66 | + // Followed by length of data |
| 67 | + let iter = self.iter(); |
| 68 | + <usize as BorshSerialize>::serialize(&iter.len(), writer)?; |
| 69 | + // Followed by data itself. |
| 70 | + for elt in iter { |
| 71 | + <A as BorshSerialize>::serialize(elt, writer)?; |
| 72 | + } |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// **Requires crate feature `"borsh"`** |
| 78 | +impl<A, D, S> BorshDeserialize for ArrayBase<S, D> |
| 79 | +where |
| 80 | + A: BorshDeserialize, |
| 81 | + D: BorshDeserialize + Dimension, |
| 82 | + S: DataOwned<Elem = A>, |
| 83 | +{ |
| 84 | + fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> { |
| 85 | + // Dimensions |
| 86 | + let dim = <D as BorshDeserialize>::deserialize_reader(reader)?; |
| 87 | + // Followed by length of data |
| 88 | + let len = <usize as BorshDeserialize>::deserialize_reader(reader)?; |
| 89 | + // Followed by data itself. |
| 90 | + let mut data = Vec::with_capacity(len); |
| 91 | + for _ix in 0..len { |
| 92 | + data.push(<A as BorshDeserialize>::deserialize_reader(reader)?); |
| 93 | + } |
| 94 | + ArrayBase::from_shape_vec(dim, data).map_err(|_shape_err| { |
| 95 | + borsh::io::Error::new( |
| 96 | + borsh::io::ErrorKind::InvalidData, |
| 97 | + "data and dimensions must match in size", |
| 98 | + ) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments