diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index 75c557d7b..b91843bb7 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -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; @@ -231,7 +231,7 @@ 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); +pub struct CqlVarint(Bytes); /// Constructors from bytes impl CqlVarint { @@ -239,16 +239,8 @@ impl CqlVarint { /// 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) -> 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) -> Self { + Self(digits.into()) } } @@ -256,7 +248,7 @@ impl CqlVarint { 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 { + pub fn into_signed_bytes_be(self) -> Bytes { self.0 } @@ -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. @@ -332,7 +324,7 @@ impl std::hash::Hash for CqlVarint { #[cfg(feature = "num-bigint-03")] impl From 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()) } } @@ -346,7 +338,7 @@ impl From for num_bigint_03::BigInt { #[cfg(feature = "num-bigint-04")] impl From 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()) } } @@ -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, i32) { + pub fn into_signed_be_bytes_and_exponent(self) -> (Bytes, i32) { (self.int_val.into_signed_bytes_be(), self.scale) } } diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index 62d998cbf..086fb6dca 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -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() diff --git a/scylla-cql/src/types/deserialize/value.rs b/scylla-cql/src/types/deserialize/value.rs index 2ff5d228e..64a11c94f 100644 --- a/scylla-cql/src/types/deserialize/value.rs +++ b/scylla-cql/src/types/deserialize/value.rs @@ -217,8 +217,8 @@ impl_emptiable_strict_type!( CqlVarint, Varint, |typ: &'metadata ColumnType<'metadata>, v: Option>| { - let val = ensure_not_null_slice::(typ, v)?; - Ok(CqlVarint::from_signed_bytes_be_slice(val)) + let val = ensure_not_null_owned::(typ, v)?; + Ok(CqlVarint::from_signed_bytes_be(val)) } ); diff --git a/scylla-cql/src/types/deserialize/value_tests.rs b/scylla-cql/src/types/deserialize/value_tests.rs index e02915588..c7587af1e 100644 --- a/scylla-cql/src/types/deserialize/value_tests.rs +++ b/scylla-cql/src/types/deserialize/value_tests.rs @@ -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(), ); diff --git a/scylla/tests/integration/cql_types.rs b/scylla/tests/integration/cql_types.rs index 112591428..e574c7303 100644 --- a/scylla/tests/integration/cql_types.rs +++ b/scylla/tests/integration/cql_types.rs @@ -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