Skip to content

Commit

Permalink
Drop unnecessary byteorder dependency (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
psychon authored Aug 18, 2023
1 parent 29d3e2e commit a6d5caf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ all-features = true
arbitrary = { default-features = false, optional = true, version = "1.0" }
arrayvec = { default-features = false, version = "0.7" }
borsh = { default-features = false, optional = true, version = "0.10.0" }
byteorder = { default-features = false, optional = true, version = "1.0" }
bytes = { default-features = false, optional = true, version = "1.0" }
diesel1 = { default-features = false, optional = true, package = "diesel", version = "1.0" }
diesel2 = { default-features = false, optional = true, package = "diesel", version = "2.1" }
Expand Down Expand Up @@ -60,8 +59,8 @@ db-diesel1-mysql = ["diesel1/mysql", "std"]
db-diesel1-postgres = ["diesel1/postgres", "std"]
db-diesel2-mysql = ["diesel2/mysql", "std"]
db-diesel2-postgres = ["diesel2/postgres", "std"]
db-postgres = ["dep:byteorder", "dep:bytes", "dep:postgres", "std"]
db-tokio-postgres = ["dep:byteorder", "dep:bytes", "dep:postgres", "std", "dep:tokio-postgres"]
db-postgres = ["dep:bytes", "dep:postgres", "std"]
db-tokio-postgres = ["dep:bytes", "dep:postgres", "std", "dep:tokio-postgres"]
legacy-ops = []
maths = []
maths-nopanic = ["maths"]
Expand All @@ -80,7 +79,7 @@ serde-str = ["serde-with-str"]
serde-with-arbitrary-precision = ["serde", "serde_json/arbitrary_precision", "serde_json/std"]
serde-with-float = ["serde"]
serde-with-str = ["serde"]
std = ["arrayvec/std", "borsh?/std", "byteorder?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"]
std = ["arrayvec/std", "borsh?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"]
tokio-pg = ["db-tokio-postgres"] # Backwards compatability

[[bench]]
Expand Down
21 changes: 13 additions & 8 deletions src/postgres/driver.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::postgres::common::*;
use crate::Decimal;
use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, BytesMut};
use postgres::types::{to_sql_checked, FromSql, IsNull, ToSql, Type};
use std::io::Cursor;
use std::io::{Cursor, Read};

fn read_two_bytes(cursor: &mut Cursor<&[u8]>) -> std::io::Result<[u8; 2]> {
let mut result = [0; 2];
cursor.read_exact(&mut result)?;
Ok(result)
}

impl<'a> FromSql<'a> for Decimal {
// Decimals are represented as follows:
Expand Down Expand Up @@ -61,17 +66,17 @@ impl<'a> FromSql<'a> for Decimal {

fn from_sql(_: &Type, raw: &[u8]) -> Result<Decimal, Box<dyn std::error::Error + 'static + Sync + Send>> {
let mut raw = Cursor::new(raw);
let num_groups = raw.read_u16::<BigEndian>()?;
let weight = raw.read_i16::<BigEndian>()?; // 10000^weight
// Sign: 0x0000 = positive, 0x4000 = negative, 0xC000 = NaN
let sign = raw.read_u16::<BigEndian>()?;
let num_groups = u16::from_be_bytes(read_two_bytes(&mut raw)?);
let weight = i16::from_be_bytes(read_two_bytes(&mut raw)?); // 10000^weight
// Sign: 0x0000 = positive, 0x4000 = negative, 0xC000 = NaN
let sign = u16::from_be_bytes(read_two_bytes(&mut raw)?);
// Number of digits (in base 10) to print after decimal separator
let scale = raw.read_u16::<BigEndian>()?;
let scale = u16::from_be_bytes(read_two_bytes(&mut raw)?);

// Read all of the groups
let mut groups = Vec::new();
for _ in 0..num_groups as usize {
groups.push(raw.read_u16::<BigEndian>()?);
groups.push(u16::from_be_bytes(read_two_bytes(&mut raw)?));
}

Ok(Self::from_postgres(PostgresDecimal {
Expand Down

0 comments on commit a6d5caf

Please sign in to comment.