I'm trying to modify ormsgpack so that it will serialize the extension types I need, starting with distinguishing tuples from lists: tuples are extension type 0.
This works:
impl Serialize for TupleSerializer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut buf = std::io::BufWriter::new(vec![]);
let mut ser = rmp_serde::Serializer::new(&mut buf);
let len = ffi!(PyTuple_GET_SIZE(self.ptr)) as usize;
let mut seq = ser.serialize_seq(Some(len)).unwrap();
if len > 0 {
for i in 0..=len - 1 {
let elem = nonnull!(ffi!(PyTuple_GET_ITEM(self.ptr, i as isize)));
seq.serialize_element(&PyObjectSerializer::new(
elem.as_ptr(),
self.opts,
self.default_calls,
self.recursion + 1,
self.default,
))
.unwrap();
}
}
let _ = seq.end();
serializer.serialize_newtype_struct(
rmp_serde::MSGPACK_EXT_STRUCT_NAME,
&(0i8, ByteBuf::from(buf.buffer())),
)
}
}
But it performs badly, because it's copying stuff in and out of that buffer.
I want to just write a couple bytes, 0xd6 and 0x00, before serializing a sequence the normal way, like for lists. How do I do this?