Repository: diesel-rs/diesel
File: diesel/src/pg/types/floats/mod.rs (Rust)

Code before:
        let weight = match *self {
            PgNumeric::Positive { weight, .. } | PgNumeric::Negative { weight, .. } => weight,
            PgNumeric::NaN => 0,
        };
        let scale = match *self {
            PgNumeric::Positive { scale, .. } | PgNumeric::Negative { scale, .. } => scale,
            PgNumeric::NaN => 0,
        };
        out.write_u16::<NetworkEndian>(digits.len() as u16)?;
        out.write_i16::<NetworkEndian>(weight)?;
        out.write_u16::<NetworkEndian>(sign)?;
        out.write_u16::<NetworkEndian>(scale)?;
        for digit in digits.iter() {
            out.write_i16::<NetworkEndian>(*digit)?;
        }

        Ok(IsNull::No)
    }
}

#[cfg(feature = "postgres_backend")]
impl FromSql<sql_types::Float, Pg> for f32 {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        let mut bytes = value.as_bytes();

        #[cfg(debug_assertions)]
        {
            if bytes.len() < 4 {
                return deserialize::Result::Err("Received less than 4 bytes while decoding an f32. Was a numeric accidentally marked as float?".into());
            }

            if bytes.len() > 4 {
                return deserialize::Result::Err("Received more than 4 bytes while decoding an f32. Was a double accidentally marked as float?".into());
            }
        }

        bytes
            .read_f32::<NetworkEndian>()
            .map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)
    }
}

#[cfg(feature = "postgres_backend")]
impl FromSql<sql_types::Double, Pg> for f64 {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        let mut bytes = value.as_bytes();

        #[cfg(debug_assertions)]
        {
            if bytes.len() < 8 {
                return deserialize::Result::Err("Received less than 8 bytes while decoding an f64. Was a float accidentally marked as double?".into());

Review comment (bug):
I would prefer returning a straight error here instead. I doubt that this will actually break anyone. (And if it does it uncovers bugs in their application)

Code after:
        let weight = match *self {
            PgNumeric::Positive { weight, .. } | PgNumeric::Negative { weight, .. } => weight,
            PgNumeric::NaN => 0,
        };
        let scale = match *self {
            PgNumeric::Positive { scale, .. } | PgNumeric::Negative { scale, .. } => scale,
            PgNumeric::NaN => 0,
        };
        out.write_u16::<NetworkEndian>(digits.len() as u16)?;
        out.write_i16::<NetworkEndian>(weight)?;
        out.write_u16::<NetworkEndian>(sign)?;
        out.write_u16::<NetworkEndian>(scale)?;
        for digit in digits.iter() {
            out.write_i16::<NetworkEndian>(*digit)?;
        }

        Ok(IsNull::No)
    }
}

#[cfg(feature = "postgres_backend")]
impl FromSql<sql_types::Float, Pg> for f32 {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        let mut bytes = value.as_bytes();

        if bytes.len() < 4 {
            return deserialize::Result::Err(
                "Received less than 4 bytes while decoding an f32. \
                 Was a numeric accidentally marked as float?"
                    .into(),
            );
        }

        if bytes.len() > 4 {
            return deserialize::Result::Err(
                "Received more than 4 bytes while decoding an f32. \
                 Was a double accidentally marked as float?"
                    .into(),
            );
        }

        bytes
            .read_f32::<NetworkEndian>()
            .map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)
    }
}

#[cfg(feature = "postgres_backend")]
impl FromSql<sql_types::Double, Pg> for f64 {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        let mut bytes = value.as_bytes();