Skip to content

Commit 65ae32a

Browse files
committed
serialize/value: serialize Bytes as CQL blob
`Bytes` deserialization from CQL `blob` has already been implemented, so for completeness, we implement serialization of `Bytes` to CQL `blob`. This has been requested in an issue: #1394.
1 parent f172332 commit 65ae32a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

scylla-cql/src/serialize/value.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::net::IpAddr;
1010
use std::ops::Deref as _;
1111
use std::sync::Arc;
1212

13+
use bytes::Bytes;
1314
use thiserror::Error;
1415
use uuid::Uuid;
1516

@@ -326,6 +327,14 @@ impl<const N: usize> SerializeValue for [u8; N] {
326327
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
327328
});
328329
}
330+
impl SerializeValue for Bytes {
331+
impl_serialize_via_writer!(|me, typ, writer| {
332+
exact_type_check!(typ, Blob);
333+
writer
334+
.set_value(me)
335+
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
336+
});
337+
}
329338
impl SerializeValue for IpAddr {
330339
impl_serialize_via_writer!(|me, typ, writer| {
331340
exact_type_check!(typ, Inet);

scylla-cql/src/serialize/value_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::str::FromStr;
2121
use std::sync::Arc;
2222

2323
use assert_matches::assert_matches;
24+
use bytes::Bytes;
2425
use thiserror::Error;
2526
use uuid::Uuid;
2627

@@ -1832,6 +1833,15 @@ fn u8_slice_serialization() {
18321833
);
18331834
}
18341835

1836+
#[test]
1837+
fn bytes_serialization() {
1838+
let val = Bytes::from_static(&[1u8, 1, 1, 1]);
1839+
assert_eq!(
1840+
do_serialize(val, &ColumnType::Native(NativeType::Blob)),
1841+
vec![0, 0, 0, 4, 1, 1, 1, 1]
1842+
);
1843+
}
1844+
18351845
#[test]
18361846
fn cql_date_serialization() {
18371847
assert_eq!(

0 commit comments

Comments
 (0)