Skip to content

Commit

Permalink
varint: store Bytes instead of Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Dec 11, 2024
1 parent d541339 commit b76399f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
26 changes: 9 additions & 17 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::frame::types;
use bytes::BufMut;
use bytes::{BufMut, Bytes};
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::convert::TryInto;
Expand Down Expand Up @@ -231,32 +231,24 @@ impl std::hash::Hash for CqlTimeuuid {
/// The implementation of [`PartialEq`], however, normalizes the underlying bytes
/// before comparison. For details, check [examples](#impl-PartialEq-for-CqlVarint).
#[derive(Clone, Eq, Debug)]
pub struct CqlVarint(Vec<u8>);
pub struct CqlVarint(Bytes);

/// Constructors from bytes
impl CqlVarint {
/// Creates a [`CqlVarint`] from an array of bytes in
/// two's complement big-endian binary representation.
///
/// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
pub fn from_signed_bytes_be(digits: Vec<u8>) -> Self {
Self(digits)
}

/// Creates a [`CqlVarint`] from a slice of bytes in
/// two's complement binary big-endian representation.
///
/// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
pub fn from_signed_bytes_be_slice(digits: &[u8]) -> Self {
Self::from_signed_bytes_be(digits.to_vec())
pub fn from_signed_bytes_be(digits: impl Into<Bytes>) -> Self {
Self(digits.into())
}
}

/// Conversion to bytes
impl CqlVarint {
/// Converts [`CqlVarint`] to an array of bytes in two's
/// complement binary big-endian representation.
pub fn into_signed_bytes_be(self) -> Vec<u8> {
pub fn into_signed_bytes_be(self) -> Bytes {
self.0
}

Expand All @@ -269,7 +261,7 @@ impl CqlVarint {

impl CqlVarint {
fn as_normalized_slice(&self) -> &[u8] {
let digits = self.0.as_slice();
let digits = self.as_signed_bytes_be_slice();
if digits.is_empty() {
// num-bigint crate normalizes empty vector to 0.
// We will follow the same approach.
Expand Down Expand Up @@ -332,7 +324,7 @@ impl std::hash::Hash for CqlVarint {
#[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())
Self::from_signed_bytes_be(value.to_signed_bytes_be())
}
}

Expand All @@ -346,7 +338,7 @@ impl From<CqlVarint> for num_bigint_03::BigInt {
#[cfg(feature = "num-bigint-04")]
impl From<num_bigint_04::BigInt> for CqlVarint {
fn from(value: num_bigint_04::BigInt) -> Self {
Self(value.to_signed_bytes_be())
Self::from_signed_bytes_be(value.to_signed_bytes_be())
}
}

Expand Down Expand Up @@ -411,7 +403,7 @@ impl CqlDecimal {

/// Converts [`CqlDecimal`] to an array of bytes in two's
/// complement binary big-endian representation and a scale.
pub fn into_signed_be_bytes_and_exponent(self) -> (Vec<u8>, i32) {
pub fn into_signed_be_bytes_and_exponent(self) -> (Bytes, i32) {
(self.int_val.into_signed_bytes_be(), self.scale)
}
}
Expand Down
2 changes: 1 addition & 1 deletion scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn cql_varint_serialization() {
];

for b in cases_from_the_spec {
let x = CqlVarint::from_signed_bytes_be_slice(b);
let x = CqlVarint::from_signed_bytes_be(bytes::Bytes::copy_from_slice(b));
let b_with_len = (b.len() as i32)
.to_be_bytes()
.iter()
Expand Down
4 changes: 2 additions & 2 deletions scylla-cql/src/types/deserialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl_emptiable_strict_type!(
CqlVarint,
Varint,
|typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>| {
let val = ensure_not_null_slice::<Self>(typ, v)?;
Ok(CqlVarint::from_signed_bytes_be_slice(val))
let val = ensure_not_null_owned::<Self>(typ, v)?;
Ok(CqlVarint::from_signed_bytes_be(val))
}
);

Expand Down
2 changes: 1 addition & 1 deletion scylla-cql/src/types/deserialize/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn test_varlen_numbers() {
// varint
assert_ser_de_identity(
&ColumnType::Varint,
&CqlVarint::from_signed_bytes_be_slice(b"Ala ma kota"),
&CqlVarint::from_signed_bytes_be(Bytes::from_static(b"Ala ma kota")),
&mut Bytes::new(),
);

Expand Down
2 changes: 1 addition & 1 deletion scylla/tests/integration/cql_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async fn test_cql_varint() {
.unwrap();

for test in tests {
let cql_varint = CqlVarint::from_signed_bytes_be_slice(&test);
let cql_varint = CqlVarint::from_signed_bytes_be(test);
session
.execute_unpaged(&prepared_insert, (&cql_varint,))
.await
Expand Down

0 comments on commit b76399f

Please sign in to comment.