Macro scylla_cql::impl_serialize_cql_via_value

source ·
macro_rules! impl_serialize_cql_via_value {
    ($t:ident$(<$($targ:tt $(: $tbound:tt)?),*>)?) => { ... };
}
Expand description

Implements the SerializeCql trait for a type, provided that the type already implements the legacy Value trait.

§Note

The translation from one trait to another encounters a performance penalty and does not utilize the stronger guarantees of SerializeCql. Before resorting to this macro, you should consider other options instead:

  • If the impl was generated using the Value procedural macro, you should switch to the SerializeCql procedural macro. The new macro behaves differently by default, so please read its documentation first!
  • If the impl was written by hand, it is still preferable to rewrite it manually. You have an opportunity to make your serialization logic type-safe and potentially improve performance.

Basically, you should consider using the macro if you have a hand-written impl and the moment it is not easy/not desirable to rewrite it.

§Example

struct NoGenerics {}
impl Value for NoGenerics {
    fn serialize<'b>(&self, _buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
        Ok(())
    }
}
impl_serialize_cql_via_value!(NoGenerics);

// Generic types are also supported. You must specify the bounds if the
// struct/enum contains any.
struct WithGenerics<T, U: Clone>(T, U);
impl<T: Value, U: Clone + Value> Value for WithGenerics<T, U> {
    fn serialize<'b>(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
        self.0.serialize(buf)?;
        self.1.clone().serialize(buf)?;
        Ok(())
    }
}
impl_serialize_cql_via_value!(WithGenerics<T, U: Clone>);