Skip to content

Commit

Permalink
support serialization flattening for references
Browse files Browse the repository at this point in the history
  • Loading branch information
nrxus committed Dec 24, 2024
1 parent 509c335 commit 6a5674f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
21 changes: 21 additions & 0 deletions scylla-cql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ pub mod _macro_internal {
fn partial(&self) -> Self::Partial<'_>;
}

impl<T: SerializeRowByName + ?Sized> SerializeRowByName for &T {
type Partial<'d>
= T::Partial<'d>
where
Self: 'd;

fn partial(&self) -> Self::Partial<'_> {
<T as SerializeRowByName>::partial(self)
}
}

/// How to serialize a row column-by-column
///
/// For now this trait is an implementation detail of `#[derive(SerializeRow)]` when
Expand Down Expand Up @@ -124,6 +135,16 @@ pub mod _macro_internal {
) -> Result<(), SerializationError>;
}

impl<T: SerializeRowInOrder + ?Sized> SerializeRowInOrder for &T {
fn serialize_in_order(
&self,
columns: &mut self::ser::row::ByColumn<'_, '_>,
writer: &mut RowWriter<'_>,
) -> Result<(), SerializationError> {
<T as SerializeRowInOrder>::serialize_in_order(&self, columns, writer)
}
}

pub mod ser {
pub mod row {
use super::super::{
Expand Down
23 changes: 21 additions & 2 deletions scylla-macros/src/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,30 @@ impl Generator for ColumnSortingGenerator<'_> {
struct_name.span(),
);
let mut partial_generics = self.ctx.generics.clone();
let partial_lt: syn::LifetimeParam = syn::parse_quote!('scylla_ser_partial);
let partial_lt: syn::Lifetime = syn::parse_quote!('scylla_ser_partial);

if !self.ctx.fields.is_empty() {
// all fields have to outlive the partial struct lifetime
partial_generics
.params
.iter_mut()
.filter_map(|p| match p {
syn::GenericParam::Lifetime(lt) => Some(lt),
_ => None,
})
.for_each(|lt| {
lt.bounds.push(partial_lt.clone());
});

// now add the partial struct lifetime
partial_generics
.params
.push(syn::GenericParam::Lifetime(partial_lt.clone()));
.push(syn::GenericParam::Lifetime(syn::LifetimeParam {
attrs: vec![],
lifetime: partial_lt.clone(),
colon_token: None,
bounds: syn::punctuated::Punctuated::new(),
}));
}

let (partial_impl_generics, partial_ty_generics, partial_where_clause) =
Expand Down

0 comments on commit 6a5674f

Please sign in to comment.