Skip to content

Commit b76399f

Browse files
committed
varint: store Bytes instead of Vec<u8>
1 parent d541339 commit b76399f

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed

scylla-cql/src/frame/value.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::frame::types;
2-
use bytes::BufMut;
2+
use bytes::{BufMut, Bytes};
33
use std::borrow::Cow;
44
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
55
use std::convert::TryInto;
@@ -231,32 +231,24 @@ impl std::hash::Hash for CqlTimeuuid {
231231
/// The implementation of [`PartialEq`], however, normalizes the underlying bytes
232232
/// before comparison. For details, check [examples](#impl-PartialEq-for-CqlVarint).
233233
#[derive(Clone, Eq, Debug)]
234-
pub struct CqlVarint(Vec<u8>);
234+
pub struct CqlVarint(Bytes);
235235

236236
/// Constructors from bytes
237237
impl CqlVarint {
238238
/// Creates a [`CqlVarint`] from an array of bytes in
239239
/// two's complement big-endian binary representation.
240240
///
241241
/// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
242-
pub fn from_signed_bytes_be(digits: Vec<u8>) -> Self {
243-
Self(digits)
244-
}
245-
246-
/// Creates a [`CqlVarint`] from a slice of bytes in
247-
/// two's complement binary big-endian representation.
248-
///
249-
/// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
250-
pub fn from_signed_bytes_be_slice(digits: &[u8]) -> Self {
251-
Self::from_signed_bytes_be(digits.to_vec())
242+
pub fn from_signed_bytes_be(digits: impl Into<Bytes>) -> Self {
243+
Self(digits.into())
252244
}
253245
}
254246

255247
/// Conversion to bytes
256248
impl CqlVarint {
257249
/// Converts [`CqlVarint`] to an array of bytes in two's
258250
/// complement binary big-endian representation.
259-
pub fn into_signed_bytes_be(self) -> Vec<u8> {
251+
pub fn into_signed_bytes_be(self) -> Bytes {
260252
self.0
261253
}
262254

@@ -269,7 +261,7 @@ impl CqlVarint {
269261

270262
impl CqlVarint {
271263
fn as_normalized_slice(&self) -> &[u8] {
272-
let digits = self.0.as_slice();
264+
let digits = self.as_signed_bytes_be_slice();
273265
if digits.is_empty() {
274266
// num-bigint crate normalizes empty vector to 0.
275267
// We will follow the same approach.
@@ -332,7 +324,7 @@ impl std::hash::Hash for CqlVarint {
332324
#[cfg(feature = "num-bigint-03")]
333325
impl From<num_bigint_03::BigInt> for CqlVarint {
334326
fn from(value: num_bigint_03::BigInt) -> Self {
335-
Self(value.to_signed_bytes_be())
327+
Self::from_signed_bytes_be(value.to_signed_bytes_be())
336328
}
337329
}
338330

@@ -346,7 +338,7 @@ impl From<CqlVarint> for num_bigint_03::BigInt {
346338
#[cfg(feature = "num-bigint-04")]
347339
impl From<num_bigint_04::BigInt> for CqlVarint {
348340
fn from(value: num_bigint_04::BigInt) -> Self {
349-
Self(value.to_signed_bytes_be())
341+
Self::from_signed_bytes_be(value.to_signed_bytes_be())
350342
}
351343
}
352344

@@ -411,7 +403,7 @@ impl CqlDecimal {
411403

412404
/// Converts [`CqlDecimal`] to an array of bytes in two's
413405
/// complement binary big-endian representation and a scale.
414-
pub fn into_signed_be_bytes_and_exponent(self) -> (Vec<u8>, i32) {
406+
pub fn into_signed_be_bytes_and_exponent(self) -> (Bytes, i32) {
415407
(self.int_val.into_signed_bytes_be(), self.scale)
416408
}
417409
}

scylla-cql/src/frame/value_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn cql_varint_serialization() {
137137
];
138138

139139
for b in cases_from_the_spec {
140-
let x = CqlVarint::from_signed_bytes_be_slice(b);
140+
let x = CqlVarint::from_signed_bytes_be(bytes::Bytes::copy_from_slice(b));
141141
let b_with_len = (b.len() as i32)
142142
.to_be_bytes()
143143
.iter()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ impl_emptiable_strict_type!(
217217
CqlVarint,
218218
Varint,
219219
|typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>| {
220-
let val = ensure_not_null_slice::<Self>(typ, v)?;
221-
Ok(CqlVarint::from_signed_bytes_be_slice(val))
220+
let val = ensure_not_null_owned::<Self>(typ, v)?;
221+
Ok(CqlVarint::from_signed_bytes_be(val))
222222
}
223223
);
224224

scylla-cql/src/types/deserialize/value_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn test_varlen_numbers() {
155155
// varint
156156
assert_ser_de_identity(
157157
&ColumnType::Varint,
158-
&CqlVarint::from_signed_bytes_be_slice(b"Ala ma kota"),
158+
&CqlVarint::from_signed_bytes_be(Bytes::from_static(b"Ala ma kota")),
159159
&mut Bytes::new(),
160160
);
161161

scylla/tests/integration/cql_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async fn test_cql_varint() {
200200
.unwrap();
201201

202202
for test in tests {
203-
let cql_varint = CqlVarint::from_signed_bytes_be_slice(&test);
203+
let cql_varint = CqlVarint::from_signed_bytes_be(test);
204204
session
205205
.execute_unpaged(&prepared_insert, (&cql_varint,))
206206
.await

0 commit comments

Comments
 (0)