Skip to content

Commit 5d4bb9d

Browse files
committed
cargo: add feature flag for num-bigint v0.4
1 parent a851069 commit 5d4bb9d

File tree

9 files changed

+163
-61
lines changed

9 files changed

+163
-61
lines changed

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ futures = "0.3.6"
1010
openssl = "0.10.32"
1111
rustyline = "9"
1212
rustyline-derive = "0.6"
13-
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03"]}
13+
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03", "num-bigint-04"]}
1414
tokio = {version = "1.1.0", features = ["full"]}
1515
tracing = "0.1.25"
1616
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }

scylla-cql/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ uuid = "1.0"
2121
thiserror = "1.0"
2222
bigdecimal = "0.2.0"
2323
num-bigint-03 = { package = "num-bigint", version = "0.3", optional = true }
24+
num-bigint-04 = { package = "num-bigint", version = "0.4", optional = true }
2425
chrono = { version = "0.4.27", default-features = false, optional = true }
2526
lz4_flex = { version = "0.11.1" }
2627
async-trait = "0.1.57"
@@ -41,4 +42,5 @@ secret = ["secrecy"]
4142
time = ["dep:time"]
4243
chrono = ["dep:chrono"]
4344
num-bigint-03 = ["dep:num-bigint-03"]
44-
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]
45+
num-bigint-04 = ["dep:num-bigint-04"]
46+
full-serialization = ["chrono", "time", "secret", "num-bigint-03", "num-bigint-04"]

scylla-cql/src/frame/response/cql_to_rust.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ impl FromCqlVal<CqlValue> for num_bigint_03::BigInt {
159159
}
160160
}
161161

162+
#[cfg(feature = "num-bigint-04")]
163+
impl FromCqlVal<CqlValue> for num_bigint_04::BigInt {
164+
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
165+
match cql_val {
166+
CqlValue::Varint(cql_varint) => Ok(cql_varint.into()),
167+
_ => Err(FromCqlValError::BadCqlType),
168+
}
169+
}
170+
}
171+
162172
#[cfg(feature = "chrono")]
163173
impl FromCqlVal<CqlValue> for NaiveDate {
164174
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
@@ -470,7 +480,7 @@ mod tests {
470480

471481
#[cfg(feature = "num-bigint-03")]
472482
#[test]
473-
fn varint_from_cql() {
483+
fn varint03_from_cql() {
474484
use num_bigint_03::ToBigInt;
475485

476486
let big_int = 0.to_bigint().unwrap();
@@ -480,6 +490,18 @@ mod tests {
480490
);
481491
}
482492

493+
#[cfg(feature = "num-bigint-04")]
494+
#[test]
495+
fn varint04_from_cql() {
496+
use num_bigint_04::ToBigInt;
497+
498+
let big_int = 0.to_bigint().unwrap();
499+
assert_eq!(
500+
Ok(big_int),
501+
num_bigint_04::BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap().into()))
502+
);
503+
}
504+
483505
#[test]
484506
fn decimal_from_cql() {
485507
let decimal = BigDecimal::from_str("123.4").unwrap();

scylla-cql/src/frame/response/result.rs

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,16 +1027,14 @@ mod tests {
10271027
assert_eq!(double_serialize, CqlValue::Double(double));
10281028
}
10291029

1030-
#[cfg(feature = "num-bigint-03")]
1031-
#[test]
1032-
fn test_varint() {
1033-
use num_bigint_03::ToBigInt;
1034-
1035-
struct Test<'a> {
1036-
value: num_bigint_03::BigInt,
1037-
encoding: &'a [u8],
1038-
}
1030+
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
1031+
struct VarintTestCase {
1032+
value: i32,
1033+
encoding: Vec<u8>,
1034+
}
10391035

1036+
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
1037+
fn varint_test_cases_from_spec() -> Vec<VarintTestCase> {
10401038
/*
10411039
Table taken from CQL Binary Protocol v4 spec
10421040
@@ -1051,44 +1049,65 @@ mod tests {
10511049
-128 | 0x80
10521050
-129 | 0xFF7F
10531051
*/
1054-
let tests = [
1055-
Test {
1056-
value: 0.to_bigint().unwrap(),
1057-
encoding: &[0x00],
1052+
vec![
1053+
VarintTestCase {
1054+
value: 0,
1055+
encoding: vec![0x00],
10581056
},
1059-
Test {
1060-
value: 1.to_bigint().unwrap(),
1061-
encoding: &[0x01],
1057+
VarintTestCase {
1058+
value: 1,
1059+
encoding: vec![0x01],
10621060
},
1063-
Test {
1064-
value: 127.to_bigint().unwrap(),
1065-
encoding: &[0x7F],
1061+
VarintTestCase {
1062+
value: 127,
1063+
encoding: vec![0x7F],
10661064
},
1067-
Test {
1068-
value: 128.to_bigint().unwrap(),
1069-
encoding: &[0x00, 0x80],
1065+
VarintTestCase {
1066+
value: 128,
1067+
encoding: vec![0x00, 0x80],
10701068
},
1071-
Test {
1072-
value: 129.to_bigint().unwrap(),
1073-
encoding: &[0x00, 0x81],
1069+
VarintTestCase {
1070+
value: 129,
1071+
encoding: vec![0x00, 0x81],
10741072
},
1075-
Test {
1076-
value: (-1).to_bigint().unwrap(),
1077-
encoding: &[0xFF],
1073+
VarintTestCase {
1074+
value: -1,
1075+
encoding: vec![0xFF],
10781076
},
1079-
Test {
1080-
value: (-128).to_bigint().unwrap(),
1081-
encoding: &[0x80],
1077+
VarintTestCase {
1078+
value: -128,
1079+
encoding: vec![0x80],
10821080
},
1083-
Test {
1084-
value: (-129).to_bigint().unwrap(),
1085-
encoding: &[0xFF, 0x7F],
1081+
VarintTestCase {
1082+
value: -129,
1083+
encoding: vec![0xFF, 0x7F],
10861084
},
1087-
];
1085+
]
1086+
}
1087+
1088+
#[cfg(feature = "num-bigint-03")]
1089+
#[test]
1090+
fn test_bigint03() {
1091+
use num_bigint_03::ToBigInt;
1092+
1093+
let tests = varint_test_cases_from_spec();
1094+
1095+
for t in tests.iter() {
1096+
let value = super::deser_cql_value(&ColumnType::Varint, &mut &*t.encoding).unwrap();
1097+
assert_eq!(CqlValue::Varint(t.value.to_bigint().unwrap().into()), value);
1098+
}
1099+
}
1100+
1101+
#[cfg(feature = "num-bigint-04")]
1102+
#[test]
1103+
fn test_bigint04() {
1104+
use num_bigint_04::ToBigInt;
1105+
1106+
let tests = varint_test_cases_from_spec();
10881107

10891108
for t in tests.iter() {
10901109
let value = super::deser_cql_value(&ColumnType::Varint, &mut &*t.encoding).unwrap();
1091-
assert_eq!(CqlValue::Varint(t.value.clone().into()), value);
1110+
assert_eq!(CqlValue::Varint(t.value.to_bigint().unwrap().into()), value);
10921111
}
10931112
}
10941113

scylla-cql/src/frame/value.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,20 @@ impl From<CqlVarint> for num_bigint_03::BigInt {
319319
}
320320
}
321321

322+
#[cfg(feature = "num-bigint-04")]
323+
impl From<num_bigint_04::BigInt> for CqlVarint {
324+
fn from(value: num_bigint_04::BigInt) -> Self {
325+
Self(value.to_signed_bytes_be())
326+
}
327+
}
328+
329+
#[cfg(feature = "num-bigint-04")]
330+
impl From<CqlVarint> for num_bigint_04::BigInt {
331+
fn from(val: CqlVarint) -> Self {
332+
num_bigint_04::BigInt::from_signed_bytes_be(&val.0)
333+
}
334+
}
335+
322336
/// Compares two [`CqlVarint`] values after normalization.
323337
///
324338
/// # Example
@@ -1021,6 +1035,19 @@ impl Value for num_bigint_03::BigInt {
10211035
}
10221036
}
10231037

1038+
#[cfg(feature = "num-bigint-04")]
1039+
impl Value for num_bigint_04::BigInt {
1040+
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1041+
let serialized = self.to_signed_bytes_be();
1042+
let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
1043+
1044+
buf.put_i32(serialized_len);
1045+
buf.extend_from_slice(&serialized);
1046+
1047+
Ok(())
1048+
}
1049+
}
1050+
10241051
impl Value for &str {
10251052
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
10261053
let str_bytes: &[u8] = self.as_bytes();

scylla-cql/src/frame/value_tests.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ fn cql_varint_serialization() {
147147
}
148148
}
149149

150-
#[cfg(feature = "num-bigint-03")]
151-
#[test]
152-
fn bigint03_serialization() {
153-
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[
150+
fn varint_test_cases_from_spec() -> Vec<(i64, Vec<u8>)> {
151+
vec![
154152
(0, vec![0x00]),
155153
(1, vec![0x01]),
156154
(127, vec![0x7F]),
@@ -159,10 +157,18 @@ fn bigint03_serialization() {
159157
(-1, vec![0xFF]),
160158
(-128, vec![0x80]),
161159
(-129, vec![0xFF, 0x7F]),
162-
];
160+
]
161+
}
162+
163+
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
164+
fn generic_num_bigint_serialization<B>()
165+
where
166+
B: From<i64> + Value + SerializeCql,
167+
{
168+
let cases_from_the_spec: &[(i64, Vec<u8>)] = &varint_test_cases_from_spec();
163169

164170
for (i, b) in cases_from_the_spec {
165-
let x = num_bigint_03::BigInt::from(*i);
171+
let x = B::from(*i);
166172
let b_with_len = (b.len() as i32)
167173
.to_be_bytes()
168174
.iter()
@@ -173,19 +179,22 @@ fn bigint03_serialization() {
173179
}
174180
}
175181

182+
#[cfg(feature = "num-bigint-03")]
183+
#[test]
184+
fn bigint03_serialization() {
185+
generic_num_bigint_serialization::<num_bigint_03::BigInt>()
186+
}
187+
188+
#[cfg(feature = "num-bigint-04")]
189+
#[test]
190+
fn bigint04_serialization() {
191+
generic_num_bigint_serialization::<num_bigint_04::BigInt>()
192+
}
193+
176194
#[test]
177195
fn bigdecimal_serialization() {
178196
// Bigint cases
179-
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[
180-
(0, vec![0x00]),
181-
(1, vec![0x01]),
182-
(127, vec![0x7F]),
183-
(128, vec![0x00, 0x80]),
184-
(129, vec![0x00, 0x81]),
185-
(-1, vec![0xFF]),
186-
(-128, vec![0x80]),
187-
(-129, vec![0xFF, 0x7F]),
188-
];
197+
let cases_from_the_spec: &[(i64, Vec<u8>)] = &varint_test_cases_from_spec();
189198

190199
for exponent in -10_i32..10_i32 {
191200
for (digits, serialized_digits) in cases_from_the_spec {

scylla-cql/src/types/serialize/value.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ impl SerializeCql for num_bigint_03::BigInt {
253253
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
254254
});
255255
}
256+
#[cfg(feature = "num-bigint-04")]
257+
impl SerializeCql for num_bigint_04::BigInt {
258+
impl_serialize_via_writer!(|me, typ, writer| {
259+
exact_type_check!(typ, Varint);
260+
// TODO: See above comment for num-bigint-03.
261+
writer
262+
.set_value(me.to_signed_bytes_be().as_slice())
263+
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
264+
});
265+
}
256266
impl SerializeCql for &str {
257267
impl_serialize_via_writer!(|me, typ, writer| {
258268
exact_type_check!(typ, Ascii, Text);

scylla/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ secret = ["scylla-cql/secret"]
2121
chrono = ["scylla-cql/chrono"]
2222
time = ["scylla-cql/time"]
2323
num-bigint-03 = ["scylla-cql/num-bigint-03"]
24-
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]
24+
num-bigint-04 = ["scylla-cql/num-bigint-04"]
25+
full-serialization = ["chrono", "time", "secret", "num-bigint-03", "num-bigint-04"]
2526

2627
[dependencies]
2728
scylla-macros = { version = "0.3.0", path = "../scylla-macros" }
@@ -58,6 +59,7 @@ socket2 = { version = "0.5.3", features = ["all"] }
5859

5960
[dev-dependencies]
6061
num-bigint-03 = { package = "num-bigint", version = "0.3" }
62+
num-bigint-04 = { package = "num-bigint", version = "0.4" }
6163
scylla-proxy = { version = "0.0.3", path = "../scylla-proxy" }
6264
ntest = "0.9.0"
6365
criterion = "0.4" # Note: v0.5 needs at least rust 1.70.0

scylla/src/transport/cql_types_test.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ where
103103
}
104104
}
105105

106-
#[cfg(feature = "num-bigint-03")]
107-
#[tokio::test]
108-
async fn test_varint() {
109-
let tests = [
106+
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
107+
fn varint_test_cases() -> Vec<&'static str> {
108+
vec![
110109
"0",
111110
"1",
112111
"127",
@@ -117,11 +116,23 @@ async fn test_varint() {
117116
"-129",
118117
"123456789012345678901234567890",
119118
"-123456789012345678901234567890",
120-
];
119+
]
120+
}
121121

122+
#[cfg(feature = "num-bigint-03")]
123+
#[tokio::test]
124+
async fn test_varint03() {
125+
let tests = varint_test_cases();
122126
run_tests::<num_bigint_03::BigInt>(&tests, "varint").await;
123127
}
124128

129+
#[cfg(feature = "num-bigint-04")]
130+
#[tokio::test]
131+
async fn test_varint04() {
132+
let tests = varint_test_cases();
133+
run_tests::<num_bigint_04::BigInt>(&tests, "varint").await;
134+
}
135+
125136
#[tokio::test]
126137
async fn test_cql_varint() {
127138
let tests = [

0 commit comments

Comments
 (0)