Repository: launchbadge/sqlx
File: sqlx-postgres/src/types/cube.rs (Rust)

Code before:
            }
            (CUBE_TYPE_DEFAULT, CUBE_DIMENSION_ONE) => {
                let x_start = 4;
                let y_start = x_start + BYTE_WIDTH;
                let x = get_f64_from_bytes(&bytes, x_start)?;
                let y = get_f64_from_bytes(&bytes, y_start)?;
                Ok(PgCube::OneDimensionInterval(x, y))
            }
            (CUBE_TYPE_DEFAULT, dim) => Ok(PgCube::MultiDimension(deserialize_matrix(
                &bytes,
                START_INDEX,
                dim,
            )?)),
            (flag, dimension) => Err(Error::Decode(
                format!(
                    "Could not deserialise cube with flag {} and dimension {}: {:?}",
                    flag, dimension, bytes
                )
                .into(),
            )),
        }
    }
}

impl PgCube {
    fn serialize(&self) -> Vec<u8> {
        let mut buff: Vec<u8> = vec![];
        match self {
            PgCube::Point(value) => {
                buff.extend_from_slice(&[
                    CUBE_TYPE_ZERO_VOLUME as u8,
                    0,
                    0,
                    CUBE_DIMENSION_ONE as u8,
                ]);
                buff.extend_from_slice(&value.to_be_bytes());
            }
            PgCube::ZeroVolume(values) => {
                let dimension = values.len() as u8;
                buff.extend_from_slice(&[CUBE_TYPE_ZERO_VOLUME as u8, 0, 0]);
                buff.extend_from_slice(&dimension.to_be_bytes());
                let bytes = values
                    .into_iter()
                    .flat_map(|v| v.to_be_bytes())
                    .collect::<Vec<u8>>();
                buff.extend_from_slice(&bytes);
            }
            PgCube::OneDimensionInterval(x, y) => {
                buff.extend_from_slice(&[0, 0, 0, CUBE_DIMENSION_ONE as u8]);
                buff.extend_from_slice(&x.to_be_bytes());
                buff.extend_from_slice(&y.to_be_bytes());

Review comment (suggestion):
It's redundant to serialize this to a `Vec` just to copy it to the buffer in the `Encode` impl. This should take a `&mut Vec<u8>` as an argument.

Code after:
    match (cube_type, dimensionality) {
        (CUBE_TYPE_ZERO_VOLUME, CUBE_DIMENSION_ONE) => {
            let point = get_f64_from_bytes(bytes, 4)?;
            Ok(PgCube::Point(point))
        }
        (CUBE_TYPE_ZERO_VOLUME, _) => {
            Ok(PgCube::ZeroVolume(deserialize_vector(bytes, START_INDEX)?))
        }
        (CUBE_TYPE_DEFAULT, CUBE_DIMENSION_ONE) => {
            let x_start = 4;
            let y_start = x_start + BYTE_WIDTH;
            let x = get_f64_from_bytes(bytes, x_start)?;
            let y = get_f64_from_bytes(bytes, y_start)?;
            Ok(PgCube::OneDimensionInterval(x, y))
        }
        (CUBE_TYPE_DEFAULT, dim) => Ok(PgCube::MultiDimension(deserialize_matrix(
            bytes,
            START_INDEX,
            dim,
        )?)),
        (flag, dimension) => Err(Error::Decode(
            format!(
                "Could not deserialise cube with flag {} and dimension {}: {:?}",
                flag, dimension, bytes
            )
            .into(),
        )),
    }
}

impl PgCube {
    fn serialize(&self, buff: &mut PgArgumentBuffer) -> Result<(), Error> {
        match self {
            PgCube::Point(value) => {
                buff.extend(&[CUBE_TYPE_ZERO_VOLUME as u8, 0, 0, CUBE_DIMENSION_ONE as u8]);
                buff.extend_from_slice(&value.to_be_bytes());
            }
            PgCube::ZeroVolume(values) => {
                let dimension = values.len() as u8;
                buff.extend_from_slice(&[CUBE_TYPE_ZERO_VOLUME as u8, 0, 0]);
                buff.extend_from_slice(&dimension.to_be_bytes());
                let bytes = values
                    .iter()
                    .flat_map(|v| v.to_be_bytes())
                    .collect::<Vec<u8>>();
                buff.extend_from_slice(&bytes);
            }
            PgCube::OneDimensionInterval(x, y) => {
                buff.extend_from_slice(&[0, 0, 0, CUBE_DIMENSION_ONE as u8]);
                buff.extend_from_slice(&x.to_be_bytes());
                buff.extend_from_slice(&y.to_be_bytes());