Skip to content

Make ReadRefReader.buf public #300

@wbenny

Description

@wbenny

Hi,
I want to deserialize stream of MessagePack structures with zero-copy.

But when I create ReadRefReader with:

    let contents = std::fs::read(&path).unwrap();
    let mut deserializer = Deserializer::from_read_ref(&contents);

... and then deserialize a first struct:

    let msg = Message::deserialize(&mut deserializer)?;

I have no way of knowing where did the deserializer end.

However, if I would add this into the impl<'de, R> Deserializer<ReadRefReader<'de, R>>:

    pub fn get_buf(&self) -> &'de [u8] {
        self.rd.buf
    }

... and used it like this:

    let mut buf = &contents[..];

    let mut result = Vec::new();

    loop {
        let mut deserializer = Deserializer::from_read_ref(&buf[..]);

        if let Ok(event) = Message::deserialize(&mut deserializer) {
            result.push(event);
        }
        else {
            break;
        }

        buf = deserializer.get_buf();
    }

It would work.

So the question is - am I missing something? Is there a way how to deserialize streaming MessagePack structs from buffer reference? If not - would it be possible to add the get_buf() method? And if not... could you think of a better way how to implement this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions