Skip to content

Commit

Permalink
graph: rework TryFrom<scalar::BigInt> for u64
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Sep 9, 2021
1 parent 6d3eb16 commit e10bb25
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions graph/src/data/store/scalar.rs
@@ -1,3 +1,4 @@
use num_traits::Signed;
use diesel::deserialize::FromSql;
use diesel::serialize::ToSql;
use diesel_derives::{AsExpression, FromSqlRow};
Expand Down Expand Up @@ -271,24 +272,11 @@ pub enum BigDecimalError {
impl<'a> TryFrom<&'a BigInt> for u64 {
type Error = BigIntOutOfRangeError;
fn try_from(value: &'a BigInt) -> Result<u64, BigIntOutOfRangeError> {
let (sign, bytes) = value.to_bytes_le();

if sign == num_bigint::Sign::Minus {
return Err(BigIntOutOfRangeError::Negative);
}

if bytes.len() > 8 {
return Err(BigIntOutOfRangeError::Overflow);
}

// Replace this with u64::from_le_bytes when stabilized
let mut n = 0u64;
let mut shift_dist = 0;
for b in bytes {
n = ((b as u64) << shift_dist) | n;
shift_dist += 8;
if value.0.is_positive() {
(&value.0).try_into().or(Err(BigIntOutOfRangeError::Overflow))
} else {
Err(BigIntOutOfRangeError::Negative)
}
Ok(n)
}
}

Expand Down

0 comments on commit e10bb25

Please sign in to comment.