Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize/Deserialize slices without length prefix #208

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions quick-protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ pub mod writer;

pub use crate::errors::{Error, Result};
pub use crate::message::{MessageInfo, MessageRead, MessageWrite};
pub use crate::reader::{deserialize_from_slice, BytesReader};
pub use crate::writer::{serialize_into_slice, BytesWriter, Writer, WriterBackend};
pub use crate::reader::{deserialize_from_slice, deserialize_from_slice_without_len, BytesReader};
pub use crate::writer::{
serialize_into_slice, serialize_into_slice_without_len, BytesWriter, Writer, WriterBackend,
};

#[cfg(feature = "std")]
pub use crate::reader::Reader;
Expand Down
20 changes: 20 additions & 0 deletions quick-protobuf/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,18 @@ impl BytesReader {
self.read_len_varint(bytes, M::from_reader)
}

/// Reads a nested message
///
/// The length is computed from the size of the message `bytes`
#[cfg_attr(std, inline)]
pub fn read_message_without_len<'a, M>(&mut self, bytes: &'a [u8]) -> Result<M>
where
M: MessageRead<'a>,
{
let len = bytes.len();
self.read_len(bytes, M::from_reader, len)
}

/// Reads a nested message
///
/// Reads just the message and does not try to read it's size first.
Expand Down Expand Up @@ -600,11 +612,19 @@ impl Reader {
}

/// Deserialize a `MessageRead from a `&[u8]`
///
/// Must be prefixed with the length of the message
pub fn deserialize_from_slice<'a, M: MessageRead<'a>>(bytes: &'a [u8]) -> Result<M> {
let mut reader = BytesReader::from_bytes(&bytes);
reader.read_message::<M>(&bytes)
}

/// Deserialize a `MessageRead from a `&[u8]` without a length prefix
pub fn deserialize_from_slice_without_len<'a, M: MessageRead<'a>>(bytes: &'a [u8]) -> Result<M> {
let mut reader = BytesReader::from_bytes(&bytes);
reader.read_message_without_len::<M>(&bytes)
}

#[test]
fn test_varint() {
let data = [0x96, 0x01];
Expand Down
27 changes: 25 additions & 2 deletions quick-protobuf/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ impl<W: WriterBackend> Writer<W> {
self.write_bytes(bytes)
}

/// Writes a message which implements `MessageWrite`
/// Writes a message which implements `MessageWrite` prefixed with a `varint` length
#[cfg_attr(std, inline)]
pub fn write_message<M: MessageWrite>(&mut self, m: &M) -> Result<()> {
let len = m.get_size();
self.write_varint(len as u64)?;
m.write_message(self)
}

/// Writes a message which implements `MessageWrite` without adding the length prefix
#[cfg_attr(std, inline)]
pub fn write_message_without_len<M: MessageWrite>(&mut self, m: &M) -> Result<()> {
m.write_message(self)
}

/// Writes another item prefixed with tag
#[cfg_attr(std, inline)]
pub fn write_with_tag<F>(&mut self, tag: u32, mut write: F) -> Result<()>
Expand Down Expand Up @@ -318,7 +324,7 @@ pub fn serialize_into_vec<M: MessageWrite>(message: &M) -> Result<Vec<u8>> {
Ok(v)
}

/// Serialize a `MessageWrite` into a byte slice
/// Serialize a `MessageWrite` into a byte slice with a length prefix
pub fn serialize_into_slice<M: MessageWrite>(message: &M, out: &mut [u8]) -> Result<()> {
let len = message.get_size();
if out.len() < len {
Expand All @@ -332,6 +338,23 @@ pub fn serialize_into_slice<M: MessageWrite>(message: &M, out: &mut [u8]) -> Res
Ok(())
}

/// Serialize a `MessageWrite` into a byte slice without a length prefix
pub fn serialize_into_slice_without_len<M: MessageWrite>(
message: &M,
out: &mut [u8],
) -> Result<usize> {
let len = message.get_size();
if out.len() < len {
return Err(Error::OutputBufferTooSmall);
}
{
let mut writer = Writer::new(BytesWriter::new(out));
writer.write_message_without_len(message)?;
}

Ok(len)
}

/// Writer backend abstraction
pub trait WriterBackend {
/// Write a u8
Expand Down
21 changes: 20 additions & 1 deletion quick-protobuf/tests/write_read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extern crate quick_protobuf;

use quick_protobuf::sizeofs::*;
use quick_protobuf::{deserialize_from_slice, serialize_into_slice, serialize_into_vec};
use quick_protobuf::{
deserialize_from_slice, deserialize_from_slice_without_len, serialize_into_slice,
serialize_into_slice_without_len, serialize_into_vec,
};
use quick_protobuf::{
BytesReader, MessageRead, MessageWrite, Reader, Result, Writer, WriterBackend,
};
Expand Down Expand Up @@ -166,6 +169,22 @@ fn wr_message_slice() {
assert_eq!(v, deserialize_from_slice(&buf).unwrap());
}

#[test]
fn wr_message_slice_without_len() {
let original = TestMessage {
id: Some(63),
val: vec![53, 5, 76, 743, 23, 753],
};

let len = original.get_size();
let mut serialized = vec![0u8; len];
let serialized_len = serialize_into_slice_without_len(&original, &mut serialized).unwrap();

let deserialized = deserialize_from_slice_without_len(&serialized).unwrap();
assert_eq!(len, serialized_len);
assert_eq!(original, deserialized);
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
struct TestMessageBorrow<'a> {
id: Option<u32>,
Expand Down