Skip to content

Commit 1af115e

Browse files
committed
improved compile-time error when fields cannot be flattened
This uses `diagnostic::on_unimplemented` to hint the users to derive `SerializeRow` with the correct flavor
1 parent 62c0cba commit 1af115e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

scylla-cql/src/_macro_internal.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub use crate::serialize::{CellValueBuilder, CellWriter, RowWriter, Serializatio
3838
///
3939
/// For now this trait is an implementation detail of `#[derive(SerializeRow)]` when
4040
/// serializing by name
41+
#[diagnostic::on_unimplemented(
42+
message = "`{Self}` cannot be flattened here",
43+
label = "`{Self}` is not a struct that derives `SerializeRow` with `match_by_name` flavor",
44+
note = "There are two common reasons for that:
45+
- `{Self}` does not use `#[derive(SerializeRow)]`
46+
- `{Self}` uses `#[scylla(flavor = \"enforce_order\")]`"
47+
)]
4148
pub trait SerializeRowByName {
4249
/// A type that can handle serialization of this struct column-by-column
4350
type Partial<'d>: PartialSerializeRowByName
@@ -48,6 +55,7 @@ pub trait SerializeRowByName {
4855
fn partial(&self) -> Self::Partial<'_>;
4956
}
5057

58+
#[diagnostic::do_not_recommend]
5159
impl<T: SerializeRowByName + ?Sized> SerializeRowByName for &T {
5260
type Partial<'d>
5361
= T::Partial<'d>
@@ -83,6 +91,13 @@ pub trait PartialSerializeRowByName {
8391
///
8492
/// For now this trait is an implementation detail of `#[derive(SerializeRow)]` when
8593
/// serializing in order
94+
#[diagnostic::on_unimplemented(
95+
message = "`{Self}` cannot be flattened here",
96+
label = "`{Self}` is not a struct that derives `SerializeRow` with `enforce_order` flavor",
97+
note = "There are two common reasons for that:
98+
- `{Self}` does not use `#[derive(SerializeRow)]`
99+
- `{Self}` uses `#[scylla(flavor = \"match_by_name\")]` (which is the default)"
100+
)]
86101
pub trait SerializeRowInOrder {
87102
fn serialize_in_order(
88103
&self,
@@ -91,6 +106,7 @@ pub trait SerializeRowInOrder {
91106
) -> Result<(), SerializationError>;
92107
}
93108

109+
#[diagnostic::do_not_recommend]
94110
impl<T: SerializeRowInOrder + ?Sized> SerializeRowInOrder for &T {
95111
fn serialize_in_order(
96112
&self,

0 commit comments

Comments
 (0)