Skip to content

Commit 4909d7e

Browse files
committed
from_row: code generation for unnamed fields
This commit adjusts the `from_row_derive` macro function that implements `FromRow` for the structs, so that it works for structs with unnamed fields as well.
1 parent d34d9f1 commit 4909d7e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

scylla-macros/src/from_row.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,32 @@ pub(crate) fn from_row_derive(tokens_input: TokenStream) -> Result<TokenStream,
4040
};
4141
(fill_struct_code, fields.named.len())
4242
}
43-
crate::parser::StructFields::Unnamed(_fields) => todo!(),
43+
crate::parser::StructFields::Unnamed(fields) => {
44+
let set_fields_code = fields.unnamed.iter().map(|field| {
45+
let field_type = &field.ty;
46+
47+
quote_spanned! {field.span() =>
48+
{
49+
let (col_ix, col_value) = vals_iter
50+
.next()
51+
.unwrap(); // vals_iter size is checked before this code is reached, so
52+
// it is safe to unwrap
53+
54+
<#field_type as FromCqlVal<::std::option::Option<CqlValue>>>::from_cql(col_value)
55+
.map_err(|e| FromRowError::BadCqlVal {
56+
err: e,
57+
column: col_ix,
58+
})?
59+
},
60+
}
61+
});
62+
63+
// This generates: ( {<field1_code>}, {<field2_code>}, ... )
64+
let fill_struct_code = quote! {
65+
(#(#set_fields_code)*)
66+
};
67+
(fill_struct_code, fields.unnamed.len())
68+
}
4469
};
4570

4671
let generated = quote! {

0 commit comments

Comments
 (0)