Skip to content

Commit 668f4e1

Browse files
authored
Merge pull request #1397 from wprzytula/serialize-bytes
serialize: support `Bytes` -> CQL `blob` conversion
2 parents f172332 + 3cf3bf2 commit 668f4e1

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

docs/source/data-types/blob.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Blob
2-
`Blob` is represented as `Vec<u8>`
2+
`Blob` is represented as one of:
3+
- `&[u8]`,
4+
- `Vec<u8>`,
5+
- `bytes::Bytes`,
6+
- `[u8; N]` (only serialization supported).
37

48

59
```rust
@@ -26,4 +30,4 @@ while let Some((blob_value,)) = stream.try_next().await? {
2630
}
2731
# Ok(())
2832
# }
29-
```
33+
```

docs/source/data-types/data-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Database types and their Rust equivalents:
1818
* `Double` <----> `f64`
1919
* `Ascii`, `Text`, `Varchar` <----> `&str`, `String`, `Box<str>`, `Arc<str>`
2020
* `Counter` <----> `value::Counter`
21-
* `Blob` <----> `Vec<u8>`
21+
* `Blob` <----> `&[u8]`, `Vec<u8>`, `Bytes`, (and `[u8; N]` for serialization only)
2222
* `Inet` <----> `std::net::IpAddr`
2323
* `Uuid` <----> `uuid::Uuid`
2424
* `Timeuuid` <----> `value::CqlTimeuuid`

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)