Skip to content

Commit 1335392

Browse files
committed
Add generic impls of DeserializeValue for Box and Arc
1 parent 7cdf381 commit 1335392

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

scylla-cql/src/deserialize/value.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,42 @@ impl<'frame, 'metadata> Iterator for UdtIterator<'frame, 'metadata> {
15241524
}
15251525
}
15261526

1527+
// Container implementations
1528+
1529+
impl<'frame, 'metadata, T: DeserializeValue<'frame, 'metadata>> DeserializeValue<'frame, 'metadata>
1530+
for Box<T>
1531+
{
1532+
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
1533+
T::type_check(typ).map_err(typck_error_replace_rust_name::<Self>)
1534+
}
1535+
1536+
fn deserialize(
1537+
typ: &'metadata ColumnType<'metadata>,
1538+
v: Option<FrameSlice<'frame>>,
1539+
) -> Result<Self, DeserializationError> {
1540+
T::deserialize(typ, v)
1541+
.map(Box::new)
1542+
.map_err(deser_error_replace_rust_name::<Self>)
1543+
}
1544+
}
1545+
1546+
impl<'frame, 'metadata, T: DeserializeValue<'frame, 'metadata>> DeserializeValue<'frame, 'metadata>
1547+
for Arc<T>
1548+
{
1549+
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
1550+
T::type_check(typ).map_err(typck_error_replace_rust_name::<Self>)
1551+
}
1552+
1553+
fn deserialize(
1554+
typ: &'metadata ColumnType<'metadata>,
1555+
v: Option<FrameSlice<'frame>>,
1556+
) -> Result<Self, DeserializationError> {
1557+
T::deserialize(typ, v)
1558+
.map(Arc::new)
1559+
.map_err(deser_error_replace_rust_name::<Self>)
1560+
}
1561+
}
1562+
15271563
// Utilities
15281564

15291565
fn ensure_not_null_frame_slice<'frame, T>(

scylla-cql/src/deserialize/value_tests.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,22 @@ fn test_tuples() {
11571157
);
11581158
}
11591159

1160+
#[test]
1161+
fn test_box() {
1162+
let int = make_bytes(&[0x01, 0x02, 0x03, 0x04]);
1163+
let decoded_int: Box<i32> =
1164+
deserialize::<Box<i32>>(&ColumnType::Native(NativeType::Int), &int).unwrap();
1165+
assert_eq!(*decoded_int, 0x01020304);
1166+
}
1167+
1168+
#[test]
1169+
fn test_arc() {
1170+
let int = make_bytes(&[0x01, 0x02, 0x03, 0x04]);
1171+
let decoded_int: Arc<i32> =
1172+
deserialize::<Arc<i32>>(&ColumnType::Native(NativeType::Int), &int).unwrap();
1173+
assert_eq!(*decoded_int, 0x01020304);
1174+
}
1175+
11601176
pub(crate) fn udt_def_with_fields(
11611177
fields: impl IntoIterator<Item = (impl Into<Cow<'static, str>>, ColumnType<'static>)>,
11621178
) -> ColumnType<'static> {
@@ -2448,6 +2464,60 @@ fn test_null_errors() {
24482464
deserialize::<MaybeEmpty<i32>>(&ser_typ, &bytes).unwrap_err();
24492465
}
24502466

2467+
#[test]
2468+
fn test_box_errors() {
2469+
let v = 123_i32;
2470+
let bytes = serialize(&ColumnType::Native(NativeType::Int), &v);
2471+
2472+
// Incompatible types render type check error.
2473+
assert_type_check_error!(
2474+
&bytes,
2475+
Box<f64>,
2476+
ColumnType::Native(NativeType::Int),
2477+
BuiltinTypeCheckErrorKind::MismatchedType {
2478+
expected: &[ColumnType::Native(NativeType::Double)],
2479+
}
2480+
);
2481+
2482+
// ColumnType is said to be Double (8 bytes expected), but in reality the serialized form has 4 bytes only.
2483+
assert_deser_error!(
2484+
&bytes,
2485+
Box<f64>,
2486+
ColumnType::Native(NativeType::Double),
2487+
BuiltinDeserializationErrorKind::ByteLengthMismatch {
2488+
expected: 8,
2489+
got: 4,
2490+
}
2491+
);
2492+
}
2493+
2494+
#[test]
2495+
fn test_arc_errors() {
2496+
let v = 123_i32;
2497+
let bytes = serialize(&ColumnType::Native(NativeType::Int), &v);
2498+
2499+
// Incompatible types render type check error.
2500+
assert_type_check_error!(
2501+
&bytes,
2502+
Arc<f64>,
2503+
ColumnType::Native(NativeType::Int),
2504+
BuiltinTypeCheckErrorKind::MismatchedType {
2505+
expected: &[ColumnType::Native(NativeType::Double)],
2506+
}
2507+
);
2508+
2509+
// ColumnType is said to be Double (8 bytes expected), but in reality the serialized form has 4 bytes only.
2510+
assert_deser_error!(
2511+
&bytes,
2512+
Arc<f64>,
2513+
ColumnType::Native(NativeType::Double),
2514+
BuiltinDeserializationErrorKind::ByteLengthMismatch {
2515+
expected: 8,
2516+
got: 4,
2517+
}
2518+
);
2519+
}
2520+
24512521
#[test]
24522522
fn test_udt_errors() {
24532523
// Loose ordering

0 commit comments

Comments
 (0)