erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
886 stars 189 forks source link

Provide a way to create a message from a vector with an offset + length #308

Open TheButlah opened 3 years ago

TheButlah commented 3 years ago

Several serialization formats (for example, flatbuffers), allocate a buffer that they serialize data into, but don't necessary fill the entire buffer with the data. The data might only be a subset of the buffer, starting at some offset for a certain number of elements. It would be awesome to have a way to construct a Message from a Vec<u8> with an associated offset and size.

Without such a function, the data either has to be copied into a new Vec<u8> that holds only the intended data, or you have to implement Sendable and call zmq_msg_init_data with a callback and unsafe code. Being a bit of a Rust newbie, I don't like the idea of using unsafe, nevertheless giving a callback that C will call, that is in charge of destructing my type.

Could a function like the following be added?

impl Message{
    fn from_vec_range(buffer: Vec<u8>, start_idx: usize, size: usize) -> Self;
}