3Hren / 3Hren/msgpack-rust

Is it possible to get the number of bytes read immediately after deserializing from a slice?

Open
#325 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

I'm trying to use `rmp_serde` to send and receive entire messages (`enum`s or `struct`s) via a `BytesMut` buffer that gets populated and emptied by a different part of the system (in this case a `TcpStream`).

I don't have any custom framing setup so I'm wondering if I could use `rmp_serde` to tell me the exact size of the serialized representation of the data (i.e. the count of bytes it deserialized) immediately after it has successfully parsed a portion of the stream into the specified type.

If there already exists an approach I apologize for having missed it. Please feel free to point me in the right direction.

I'm picturing an API like:

```rust
/// Deserialize a slice into a deserializable data type and return a count of the bytes deserialized if the deserialization was successful.
pub fn from_slice_with_size<'a, T>(input: &'a [u8]) -> (Result, Option)
where
T: Deserialize<'a>
{
...
}
```

Here's an example usage:

```rust
use serde::{
Serialize,
Deserialize
};

#[derive(Serialize, Deserialize)
pub enum Foo {
Bar(String),
Baz
}

pub struct Container {
pub buffer: BytesMut
}

impl Container {
...
fn read_foo(&mut self) -> Result, Box> {
if let Ok(foo) = rmp_serde::from_slice(&self.buffer) {
// Currently, to get the byte count I have to serialize it again.
// This has to be slower than keeping track of the bytes deserialized
// while deserializing.
let bytes_serialized = rmp_serde::encode::to_vec(&foo)?.len();
self.buffer.advance(bytes_serialized);
Ok(Some(foo))
}
}

/// This doesn't work because there is no `from_slice_with_size` method but
/// it'd be neat if there was something that keeps track
/// of and outputs the size of the bytes deserialized.
fn read_foo_with_size(&mut self) -> Result, Box> {
if let (Ok(foo), Some(size)) = rmp_serde::from_slice_with_size(&self.buffer) {
self.buffer.advance(size);
Ok(Some(foo))
}
}
...
// Some other part takes a `&mut self` and populates the buffer.
fn fill_up_buffer(&mut self) {
stream.read_buf(&mut self.buffer).unwrap();
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.