Originally from the User Slack
@Mattia: Is there a straightforward way to serialize a Rust enum into a text
inside scylla? Since we can not just derive the macro
@Mattia:
impl SerializeValue for MyCustomEnum {
fn serialize<'b>(
&self,
typ: &scylla::cluster::metadata::ColumnType,
writer: scylla::serialize::writers::CellWriter<'b>,
) -> Result<
scylla::serialize::writers::WrittenCellProof<'b>,
scylla::errors::SerializationError,
> {
let s = self.to_string();
<String as SerializeValue>::serialize(&s, typ, writer)
}
}
Does this makes sense? When MyCustomEnum
also implements Display
@Karol_Baryła: This impl looks correct. You could possibly avoid the allocation caused by creating a String
, but its hard to tell without seeing more of your code.
@Mattia: I have standard enums like so:
enum Foo {
Bar,
..,
}
Do you have any pointer on how I could avoid the allocation?
@Karol_Baryła: Can’t you implement as_str
method, returning a string literal different for each case? I don’t see why you need a String
.
@Mattia: You’re right, thanks for the heads up