I'd like to create a method that accepts a Vec of values that all implement serde::ser::Serializable. (The method iterates over the vec, serializes each value to bytes, and writes the bytes to disk).
Is there any way to do this? In Java/Typescript/etc. there might be an interface that all the objects could implement.
Normally, I would create a trait with a method called to_bytes or something like that, but in this case I want to directly pass each value, x to rmp_serde::encode::write_named(&mut writer, x) b/c I want to serialize the values directly to the file (avoid intermediate allocations / copies).
I tried making the parameter vals: Vec<Box<dyn serde::ser::Serialize>>, but that didn't work.
If the method has the generic bound T: serde::ser::Serialize and the parameter is: values: Vec<T>, this won't work since Vecs can only store values of a single type.
fn write_named<W: ?Sized, T: ?Sized>(
wr: &mut W,
val: &T
) -> Result<(), Error>
where
W: Write,
T: Serialize, // <- we target this trait, so this shall be Self
becoming the trait's method (val -> self, T -> Self):
Our objective now is to make it object-safe: it currently isn't yet since remains that &mut impl ?Sized + Write generic parameter. But at this point, it's easy: just replace it with a &mut dyn Write parameter: