admin管理员组文章数量:1125887
I want to serialize data into a buffer, but not starting at the first byte, so I tried passing a slice to the serializer.
The serializer works if you pass the whole Vec
like this:
let mut buffer = Vec::new();
let mut serializer = Serializer::new(&mut buffer);
but it fails to serialize if you pass a slice because it's zero length
let mut buffer = Vec::new();
let mut serializer = Serializer::new(&mut buffer[..]);
I can fix the serializer by resizing the buffer like this:
let mut buffer = Vec::new();
buffer.resize(20, 0);
let mut serializer = Serializer::new(&mut buffer[..]);
but I then have no way to figure out how many bytes were written to the buffer by the serializer.
I feel like I am missing something. Do I need custom implementation of the Write
trait to accomplish this?
I want to serialize data into a buffer, but not starting at the first byte, so I tried passing a slice to the serializer.
The serializer works if you pass the whole Vec
like this:
let mut buffer = Vec::new();
let mut serializer = Serializer::new(&mut buffer);
but it fails to serialize if you pass a slice because it's zero length
let mut buffer = Vec::new();
let mut serializer = Serializer::new(&mut buffer[..]);
I can fix the serializer by resizing the buffer like this:
let mut buffer = Vec::new();
buffer.resize(20, 0);
let mut serializer = Serializer::new(&mut buffer[..]);
but I then have no way to figure out how many bytes were written to the buffer by the serializer.
I feel like I am missing something. Do I need custom implementation of the Write
trait to accomplish this?
1 Answer
Reset to default 0I got this figured out, and was actually quite close.
The important part is that the serializer appends to the buffer, so if you resize the buffer first, then pass it to the serializer, it will append the serialization.
This works, and starts writing the serialized data at the 9th byte:
let mut buffer = Vec::new();
buffer.resize(8, 0);
let mut serializer = Serializer::new(&mut buffer);
本文标签: rustHow to serialize data at an offset from the start of a vectorStack Overflow
版权声明:本文标题:rust - How to serialize data at an offset from the start of a vector - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736678757a1947318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Serializer
? – kmdreko Commented Jan 9 at 2:14