pub fn push_array<'a, I, D>(
        &mut self,
        dims: &[ArrayDimension],
        iter: I,
    ) -> Result<(), InvalidArrayError>
    where
        I: IntoIterator<Item = D>,
        D: Borrow<Datum<'a>>,
    {
        // Arrays are encoded as follows.
        //
        // u8    ndims
        // u64   dim_0 lower bound
        // u64   dim_0 length
        // ...
        // u64   dim_n lower bound
        // u64   dim_n length
        // u64   element data size in bytes
        // u8    element data, where elements are encoded in row-major order

        if dims.len() > usize::from(MAX_ARRAY_DIMENSIONS) {
            return Err(InvalidArrayError::TooManyDimensions(dims.len()));
        }

        let start = self.data.len();
        self.data.push(Tag::Array as u8);

        // Write dimension information.
        self.data
            .push(dims.len().try_into().expect("ndims verified to fit in u8"));
        for dim in dims {
            self.data
                .extend_from_slice(&u64::cast_from(dim.lower_bound).to_le_bytes());
            self.data
                .extend_from_slice(&u64::cast_from(dim.length).to_le_bytes());
        }

        // Write elements.
        let off = self.data.len();
        self.data.extend_from_slice(&[0; size_of::<u64>()]);
        let mut nelements = 0;
        for datum in iter {
            self.push(*datum.borrow());
            nelements += 1;
        }
        let len = u64::cast_from(self.data.len() - off - size_of::<u64>());
        self.data[off..off + size_of::<u64>()].copy_from_slice(&len.to_le_bytes());

        // Check that the number of elements written matches the dimension
        // information.
        let cardinality = match dims {
            [] => 0,
            dims => dims.iter().map(|d| d.length).product(),
        };
        if nelements != cardinality {
            self.data.truncate(start);
            return Err(InvalidArrayError::WrongCardinality {
                actual: nelements,
                expected: cardinality,
            });
        }

        Ok(())
    }
/// Convenience function to construct an array from an iter of `Datum`s.
///
/// Returns an error if the number of elements in `iter` does not match
/// the cardinality of the array as described by `dims`, or if the
/// number of dimensions exceeds [`MAX_ARRAY_DIMENSIONS`]. If an error
/// occurs, the packer's state will be unchanged.
Convenience function to construct an array from an iter of `Datum`s.
Convenience function to construct an array from an iter of ` Datum ` s .
pub fn push_array<'a, I, D>(
        &mut self,
        dims: &[ArrayDimension],
        iter: I,
    ) -> Result<(), InvalidArrayError>
    where
        I: IntoIterator<Item = D>,
        D: Borrow<Datum<'a>>,
    {
        // Arrays are encoded as follows.
        //
        // u8    ndims
        // u64   dim_0 lower bound
        // u64   dim_0 length
        // ...
        // u64   dim_n lower bound
        // u64   dim_n length
        // u64   element data size in bytes
        // u8    element data, where elements are encoded in row-major order
        if dims.len() > usize::from(MAX_ARRAY_DIMENSIONS) {
            return Err(InvalidArrayError::TooManyDimensions(dims.len()));
        }
        let start = self.data.len();
        self.data.push(Tag::Array as u8);
        // Write dimension information.
        self.data
            .push(dims.len().try_into().expect("ndims verified to fit in u8"));
        for dim in dims {
            self.data
                .extend_from_slice(&u64::cast_from(dim.lower_bound).to_le_bytes());
            self.data
                .extend_from_slice(&u64::cast_from(dim.length).to_le_bytes());
        }
        // Write elements.
        let off = self.data.len();
        self.data.extend_from_slice(&[0; size_of::<u64>()]);
        let mut nelements = 0;
        for datum in iter {
            self.push(*datum.borrow());
            nelements += 1;
        }
        let len = u64::cast_from(self.data.len() - off - size_of::<u64>());
        self.data[off..off + size_of::<u64>()].copy_from_slice(&len.to_le_bytes());
        // Check that the number of elements written matches the dimension
        // information.
        let cardinality = match dims {
            [] => 0,
            dims => dims.iter().map(|d| d.length).product(),
        };
        if nelements != cardinality {
            self.data.truncate(start);
            return Err(InvalidArrayError::WrongCardinality {
                actual: nelements,
                expected: cardinality,
            });
        }
        Ok(())
    }
pub fn push_array < ' a , I , D > ( & mut self , dims : & [ ArrayDimension ] , iter : I , ) -> Result < ( ) , InvalidArrayError > where I : IntoIterator < Item = D > , D : Borrow < Datum < ' a > > , { // Arrays are encoded as follows. // // u8    ndims // u64   dim_0 lower bound // u64   dim_0 length // ... // u64   dim_n lower bound // u64   dim_n length // u64   element data size in bytes // u8    element data, where elements are encoded in row-major order if dims . len ( ) > usize :: from ( MAX_ARRAY_DIMENSIONS ) { return Err ( InvalidArrayError :: TooManyDimensions ( dims . len ( ) ) ) ; } let start = self . data . len ( ) ; self . data . push ( Tag :: Array as u8 ) ; // Write dimension information. self . data . push ( dims . len ( ) . try_into ( ) . expect ( " " ) ) ; for dim in dims { self . data . extend_from_slice ( & u64 :: cast_from ( dim . lower_bound ) . to_le_bytes ( ) ) ; self . data . extend_from_slice ( & u64 :: cast_from ( dim . length ) . to_le_bytes ( ) ) ; } // Write elements. let off = self . data . len ( ) ; self . data . extend_from_slice ( & [ 0 ; size_of :: < u64 > ( ) ] ) ; let mut nelements = 0 ; for datum in iter { self . push ( * datum . borrow ( ) ) ; nelements += 1 ; } let len = u64 :: cast_from ( self . data . len ( ) - off - size_of :: < u64 > ( ) ) ; self . data [ off .. off + size_of :: < u64 > ( ) ] . copy_from_slice ( & len . to_le_bytes ( ) ) ; // Check that the number of elements written matches the dimension // information. let cardinality = match dims { [ ] => 0 , dims => dims . iter ( ) . map ( | d | d . length ) . product ( ) , } ; if nelements != cardinality { self . data . truncate ( start ) ; return Err ( InvalidArrayError :: WrongCardinality { actual : nelements , expected : cardinality , } ) ; } Ok ( ( ) ) }
Convenience function to construct an array from an iter of `Datum`s.
Convenience function to construct an array from an iter of ` Datum ` s .
&mut self
dims
&[ArrayDimension]
iter
I
&mut self
dims
&[ArrayDimension]
iter
I