diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index 5f692d53b6..280d5d055b 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -1000,11 +1000,14 @@ fn serialize_values( let mut writer = BufBackedRowWriter::new(&mut new_serialized); ::serialize(&vl, &ctx, &mut writer).unwrap(); let value_count: u16 = writer.value_count().try_into().unwrap(); + let is_empty = writer.value_count() == 0; // Prepend with value count, like `ValueList` does new_serialized[0..2].copy_from_slice(&value_count.to_be_bytes()); assert_eq!(old_serialized, new_serialized); + assert_eq!(::is_empty(&vl), is_empty); + assert_eq!(serialized.is_empty(), is_empty); serialized } @@ -1016,10 +1019,13 @@ fn serialize_values_only_new(vl: T, columns: &[ColumnSpec]) -> let mut writer = BufBackedRowWriter::new(&mut serialized); ::serialize(&vl, &ctx, &mut writer).unwrap(); let value_count: u16 = writer.value_count().try_into().unwrap(); + let is_empty = writer.value_count() == 0; // Prepend with value count, like `ValueList` does serialized[0..2].copy_from_slice(&value_count.to_be_bytes()); + assert_eq!(::is_empty(&vl), is_empty); + serialized } diff --git a/scylla-cql/src/types/serialize/row.rs b/scylla-cql/src/types/serialize/row.rs index c2d8a45246..b5fd862cee 100644 --- a/scylla-cql/src/types/serialize/row.rs +++ b/scylla-cql/src/types/serialize/row.rs @@ -53,6 +53,8 @@ pub trait SerializeRow { ctx: &RowSerializationContext<'_>, writer: &mut W, ) -> Result<(), SerializationError>; + + fn is_empty(&self) -> bool; } macro_rules! fallback_impl_contents { @@ -69,6 +71,10 @@ macro_rules! fallback_impl_contents { ) -> Result<(), SerializationError> { serialize_legacy_row(self, ctx, writer) } + #[inline] + fn is_empty(&self) -> bool { + SerializedValues::is_empty(self) + } }; } @@ -96,6 +102,11 @@ macro_rules! impl_serialize_row_for_unit { // Row is empty - do nothing Ok(()) } + + #[inline] + fn is_empty(&self) -> bool { + true + } }; } @@ -152,6 +163,11 @@ macro_rules! impl_serialize_row_for_slice { } Ok(()) } + + #[inline] + fn is_empty(&self) -> bool { + <[T]>::is_empty(self.as_ref()) + } }; } @@ -227,6 +243,11 @@ macro_rules! impl_serialize_row_for_map { Ok(()) } + + #[inline] + fn is_empty(&self) -> bool { + Self::is_empty(self) + } }; } @@ -258,6 +279,11 @@ impl SerializeRow for &T { ) -> Result<(), SerializationError> { ::serialize(self, ctx, writer) } + + #[inline] + fn is_empty(&self) -> bool { + ::is_empty(self) + } } impl SerializeRow for SerializedValues { @@ -325,6 +351,11 @@ macro_rules! impl_tuple { )* Ok(()) } + + #[inline] + fn is_empty(&self) -> bool { + $length == 0 + } } }; } @@ -428,6 +459,14 @@ macro_rules! impl_serialize_row_via_value_list { ) -> ::std::result::Result<(), $crate::types::serialize::SerializationError> { $crate::types::serialize::row::serialize_legacy_row(self, ctx, writer) } + + #[inline] + fn is_empty(&self) -> bool { + match $crate::frame::value::ValueList::serialized(self) { + Ok(s) => s.is_empty(), + Err(e) => false + } + } } }; }