Skip to content

Commit

Permalink
value: implement serialize for u8 slices
Browse files Browse the repository at this point in the history
  • Loading branch information
koheatel committed Aug 19, 2023
1 parent 932c6b4 commit e12cb12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ impl Value for &str {
}

impl Value for Vec<u8> {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
<&[u8] as Value>::serialize(&self.as_slice(), buf)
}
}

impl Value for &[u8] {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
let val_len: i32 = self.len().try_into().map_err(|_| ValueTooBig)?;
buf.put_i32(val_len);
Expand Down
6 changes: 6 additions & 0 deletions scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ fn u8_array_serialization() {
assert_eq!(serialized(val), vec![0, 0, 0, 4, 1, 1, 1, 1]);
}

#[test]
fn u8_slice_serialization() {
let val = vec![1u8, 1, 1, 1];
assert_eq!(serialized(val.as_slice()), vec![0, 0, 0, 4, 1, 1, 1, 1]);
}

#[test]
fn naive_date_serialization() {
// 1970-01-31 is 2^31
Expand Down

0 comments on commit e12cb12

Please sign in to comment.