Skip to content

Commit

Permalink
cargo: hide num_bigint v0.3 behind feature flag
Browse files Browse the repository at this point in the history
Hidden public usages of the `num_bigint::BigInt` (version 0.3) behind
`num-bigint-03` feature flag.
  • Loading branch information
muzarski committed Jan 12, 2024
1 parent b045d45 commit 0c0b47f
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ futures = "0.3.6"
openssl = "0.10.32"
rustyline = "9"
rustyline-derive = "0.6"
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time"]}
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03"]}
tokio = {version = "1.1.0", features = ["full"]}
tracing = "0.1.25"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
Expand Down
5 changes: 3 additions & 2 deletions scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ snap = "1.0"
uuid = "1.0"
thiserror = "1.0"
bigdecimal = "0.2.0"
num-bigint = "0.3"
num-bigint-03 = { package = "num-bigint", version = "0.3", optional = true }
chrono = { version = "0.4.27", default-features = false, optional = true }
lz4_flex = { version = "0.11.1" }
async-trait = "0.1.57"
Expand All @@ -40,4 +40,5 @@ harness = false
secret = ["secrecy"]
time = ["dep:time"]
chrono = ["dep:chrono"]
full-serialization = ["chrono", "time", "secret"]
num-bigint-03 = ["dep:num-bigint-03"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]
10 changes: 6 additions & 4 deletions scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::result::{CqlValue, Row};
use crate::frame::value::{Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlVarint};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::{BuildHasher, Hash};
use std::net::IpAddr;
Expand Down Expand Up @@ -147,7 +146,8 @@ impl<const N: usize> FromCqlVal<CqlValue> for [u8; N] {
}
}

impl FromCqlVal<CqlValue> for BigInt {
#[cfg(feature = "num-bigint-03")]
impl FromCqlVal<CqlValue> for num_bigint_03::BigInt {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
match cql_val {
CqlValue::Varint(cql_varint) => Ok(cql_varint.into()),
Expand Down Expand Up @@ -402,7 +402,6 @@ mod tests {
use crate::frame::value::{Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp};
use crate::macros::FromRow;
use bigdecimal::BigDecimal;
use num_bigint::{BigInt, ToBigInt};
use std::collections::HashSet;
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
Expand Down Expand Up @@ -466,12 +465,15 @@ mod tests {
assert_eq!(Ok(ip_addr), IpAddr::from_cql(CqlValue::Inet(ip_addr)));
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn varint_from_cql() {
use num_bigint_03::ToBigInt;

let big_int = 0.to_bigint().unwrap();
assert_eq!(
Ok(big_int),
BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap().into()))
num_bigint_03::BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap().into()))
);
}

Expand Down
9 changes: 5 additions & 4 deletions scylla-cql/src/frame/response/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ pub fn deser_cql_value(typ: &ColumnType, buf: &mut &[u8]) -> StdResult<CqlValue,
}
Decimal => {
let scale = types::read_int(buf)? as i64;
let int_value = num_bigint::BigInt::from_signed_bytes_be(buf);
let int_value = bigdecimal::num_bigint::BigInt::from_signed_bytes_be(buf);
let big_decimal: BigDecimal = BigDecimal::from((int_value, scale));

CqlValue::Decimal(big_decimal)
Expand Down Expand Up @@ -967,8 +967,6 @@ mod tests {
use crate as scylla;
use crate::frame::value::{Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use num_bigint::ToBigInt;
use scylla::frame::response::result::{ColumnType, CqlValue};
use std::str::FromStr;
use uuid::Uuid;
Expand Down Expand Up @@ -1027,10 +1025,13 @@ mod tests {
assert_eq!(double_serialize, CqlValue::Double(double));
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn test_varint() {
use num_bigint_03::ToBigInt;

struct Test<'a> {
value: BigInt,
value: num_bigint_03::BigInt,
encoding: &'a [u8],
}

Expand Down
14 changes: 8 additions & 6 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::frame::frame_errors::ParseError;
use crate::frame::types;
use bigdecimal::BigDecimal;
use bytes::BufMut;
use num_bigint::BigInt;
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::convert::TryInto;
Expand Down Expand Up @@ -101,15 +100,17 @@ impl CqlVarint {
}
}

impl From<BigInt> for CqlVarint {
fn from(value: BigInt) -> Self {
#[cfg(feature = "num-bigint-03")]
impl From<num_bigint_03::BigInt> for CqlVarint {
fn from(value: num_bigint_03::BigInt) -> Self {
Self(value.to_signed_bytes_be())
}
}

impl From<CqlVarint> for BigInt {
#[cfg(feature = "num-bigint-03")]
impl From<CqlVarint> for num_bigint_03::BigInt {
fn from(val: CqlVarint) -> Self {
BigInt::from_signed_bytes_be(&val.0)
num_bigint_03::BigInt::from_signed_bytes_be(&val.0)
}
}

Expand Down Expand Up @@ -770,7 +771,8 @@ impl Value for CqlVarint {
}
}

impl Value for BigInt {
#[cfg(feature = "num-bigint-03")]
impl Value for num_bigint_03::BigInt {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
let serialized = self.to_signed_bytes_be();
let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
Expand Down
8 changes: 4 additions & 4 deletions scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use super::value::{
};
use bigdecimal::BigDecimal;
use bytes::BufMut;
use num_bigint::BigInt;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::{BuildHasherDefault, Hasher};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
Expand Down Expand Up @@ -98,8 +97,9 @@ fn cql_varint_serialization() {
}
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn bigint_serialization() {
fn bigint03_serialization() {
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[
(0, vec![0x00]),
(1, vec![0x01]),
Expand All @@ -112,7 +112,7 @@ fn bigint_serialization() {
];

for (i, b) in cases_from_the_spec {
let x = BigInt::from(*i);
let x = num_bigint_03::BigInt::from(*i);
let b_with_len = (b.len() as i32)
.to_be_bytes()
.iter()
Expand Down Expand Up @@ -146,7 +146,7 @@ fn bigdecimal_serialization() {
.chain(serialized_digits)
.cloned()
.collect::<Vec<_>>();
let digits = BigInt::from(*digits);
let digits = bigdecimal::num_bigint::BigInt::from(*digits);
let x = BigDecimal::new(digits, exponent as i64);
assert_eq!(serialized(x, ColumnType::Decimal), repr);
}
Expand Down
6 changes: 3 additions & 3 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::net::IpAddr;
use std::sync::Arc;

use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use thiserror::Error;
use uuid::Uuid;

Expand Down Expand Up @@ -235,7 +234,8 @@ impl SerializeCql for CqlVarint {
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
});
}
impl SerializeCql for BigInt {
#[cfg(feature = "num-bigint-03")]
impl SerializeCql for num_bigint_03::BigInt {
impl_serialize_via_writer!(|me, typ, writer| {
exact_type_check!(typ, Varint);
// TODO: The allocation here can be avoided and we can reimplement
Expand Down Expand Up @@ -1507,8 +1507,8 @@ mod tests {
};
use crate::types::serialize::{CellWriter, SerializationError};

use bigdecimal::num_bigint::BigInt;
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use scylla_macros::SerializeCql;

use super::{SerializeCql, UdtSerializationErrorKind, UdtTypeCheckErrorKind};
Expand Down
5 changes: 3 additions & 2 deletions scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ cloud = ["ssl", "scylla-cql/serde", "dep:serde_yaml", "dep:serde", "dep:url", "d
secret = ["scylla-cql/secret"]
chrono = ["scylla-cql/chrono"]
time = ["scylla-cql/time"]
full-serialization = ["chrono", "time", "secret"]
num-bigint-03 = ["scylla-cql/num-bigint-03"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]

[dependencies]
scylla-macros = { version = "0.3.0", path = "../scylla-macros" }
Expand All @@ -37,7 +38,6 @@ rand = "0.8.3"
thiserror = "1.0"
itertools = "0.11.0"
bigdecimal = "0.2.0"
num-bigint = "0.3"
tracing = "0.1.36"
chrono = { version = "0.4.20", default-features = false, features = ["clock"] }
openssl = { version = "0.10.32", optional = true }
Expand All @@ -57,6 +57,7 @@ rand_pcg = "0.3.1"
socket2 = { version = "0.5.3", features = ["all"] }

[dev-dependencies]
num-bigint-03 = { package = "num-bigint", version = "0.3" }
scylla-proxy = { version = "0.0.3", path = "../scylla-proxy" }
ntest = "0.9.0"
criterion = "0.4" # Note: v0.5 needs at least rust 1.70.0
Expand Down
4 changes: 2 additions & 2 deletions scylla/src/transport/cql_types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::transport::session::IntoTypedRows;
use crate::transport::session::Session;
use crate::utils::test_utils::unique_keyspace_name;
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use scylla_cql::frame::value::CqlVarint;
use scylla_cql::types::serialize::value::SerializeCql;
use scylla_macros::SerializeCql;
Expand Down Expand Up @@ -104,6 +103,7 @@ where
}
}

#[cfg(feature = "num-bigint-03")]
#[tokio::test]
async fn test_varint() {
let tests = [
Expand All @@ -119,7 +119,7 @@ async fn test_varint() {
"-123456789012345678901234567890",
];

run_tests::<BigInt>(&tests, "varint").await;
run_tests::<num_bigint_03::BigInt>(&tests, "varint").await;
}

#[tokio::test]
Expand Down

0 comments on commit 0c0b47f

Please sign in to comment.