Skip to content

Commit

Permalink
Merge 58320b8 into 781e5ca
Browse files Browse the repository at this point in the history
  • Loading branch information
3Hren committed Sep 18, 2017
2 parents 781e5ca + 58320b8 commit 7b4d338
Show file tree
Hide file tree
Showing 16 changed files with 861 additions and 264 deletions.
13 changes: 13 additions & 0 deletions rmp-serde/CHANGELOG.md
Expand Up @@ -3,6 +3,19 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]
### Added:
- Add `Ext` trait for `Serializer` that allows to wrap a serializer with another one, that overrides exactly one serialization policy. For example using `with_struct_map` method it is possible to serialize structs as a MessagePack map with field names, overriding default serialization policy, which emits structs as a tuple.
- Add `UnderlyingWrite` trait for `Serializer` and its wrappers to be able to obtain the underlying writer.
- Add missing `Debug` implementations.

### Changed:
- Serialize newtype structs by serializing its inner type without wrapping into a tuple.
- Function `encode::to_vec_named` now accepts unsized values.
- Renamed `decode::Read` trait to `decode::ReadSlice` to avoid clashing with `std::io::Read` and to specify more precisely what it does.

### Removed:
- Type parameter `VariantWriter` is no longer a type member of `Serializer`. Instead a `Serializer` can be wrapped by another serializer using `with_struct_map`, `with_struct_tuple` etc. methods.

## 0.13.7 - 2017-09-13
### Changed:
- `Raw` and `RawRef` are now serializable.
Expand Down
2 changes: 1 addition & 1 deletion rmp-serde/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rmp-serde"
version = "0.13.7"
version = "0.14.0"
authors = ["Evgeny Safronov <division494@gmail.com>"]
license = "MIT"
description = "Serde bindings for RMP"
Expand Down
94 changes: 51 additions & 43 deletions rmp-serde/src/decode.rs
@@ -1,6 +1,8 @@
//! Generic MessagePack deserialization.

use std::error;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Cursor};
use std::io::{self, Cursor, ErrorKind, Read};
use std::str::{self, Utf8Error};

use byteorder::{self, ReadBytesExt};
Expand All @@ -16,22 +18,22 @@ use rmp::decode::{MarkerReadError, DecodeStringError, ValueReadError, NumValueRe
/// Enum representing errors that can occur while decoding MessagePack data.
#[derive(Debug)]
pub enum Error {
/// The enclosed I/O error occured while trying to read a MessagePack
/// The enclosed I/O error occurred while trying to read a MessagePack
/// marker.
InvalidMarkerRead(io::Error),
/// The enclosed I/O error occured while trying to read the encoded
/// The enclosed I/O error occurred while trying to read the encoded
/// MessagePack data.
InvalidDataRead(io::Error),
/// A mismatch occured between the decoded and expected value types.
/// A mismatch occurred between the decoded and expected value types.
TypeMismatch(Marker),
/// A numeric cast failed due to an out-of-range error.
OutOfRange,
/// A decoded array did not have the enclosed expected length.
LengthMismatch(u32),
/// An otherwise uncategorized error occured. See the enclosed `String` for
/// An otherwise uncategorized error occurred. See the enclosed `String` for
/// details.
Uncategorized(String),
/// A general error occured while deserializing the expected type. See the
/// A general error occurred while deserializing the expected type. See the
/// enclosed `String` for details.
Syntax(String),
/// An encoded string could not be parsed as UTF-8.
Expand Down Expand Up @@ -123,13 +125,15 @@ impl<'a> From<DecodeStringError<'a>> for Error {
///
/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
/// operation is retried.
#[derive(Debug)]
pub struct Deserializer<R> {
rd: R,
marker: Option<Marker>,
depth: usize,
}

impl<'de> Deserializer<SliceReader<'de>> {
/// Constructs a new `Deserializer` from the given byte slice.
pub fn from_slice(slice: &'de [u8]) -> Self {
Deserializer {
rd: SliceReader::new(slice),
Expand All @@ -144,21 +148,23 @@ impl<'de> Deserializer<SliceReader<'de>> {
}
}

impl<R: io::Read> Deserializer<ReadReader<R>> {
impl<R: Read> Deserializer<ReadReader<R>> {
#[doc(hidden)]
#[deprecated(note="use `Deserializer::new` instead")]
pub fn from_read(rd: R) -> Self {
Deserializer {
Self::new(rd)
}

/// Constructs a new `Deserializer` by consuming the given reader.
pub fn new(rd: R) -> Self {
Self {
rd: ReadReader::new(rd),
// Cached marker in case of deserializing options.
marker: None,
depth: 1024,
}
}

/// Constructs a new deserializer by consuming the given reader.
pub fn new(rd: R) -> Self {
Self::from_read(rd)
}

/// Gets a reference to the underlying reader in this decoder.
pub fn get_ref(&self) -> &R {
&self.rd.inner
Expand All @@ -182,7 +188,7 @@ impl<R: AsRef<[u8]>> Deserializer<ReadReader<Cursor<R>>> {
}
}

impl<'de, R: Read<'de>> Deserializer<R> {
impl<'de, R: ReadSlice<'de>> Deserializer<R> {
/// Changes the maximum nesting depth that is allowed
pub fn set_max_depth(&mut self, depth: usize) {
self.depth = depth;
Expand Down Expand Up @@ -245,19 +251,19 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
}

fn read_u8<'de, R: Read<'de>>(rd: &mut R) -> Result<u8, Error> {
fn read_u8<'de, R: ReadSlice<'de>>(rd: &mut R) -> Result<u8, Error> {
rd.read_u8().map_err(Error::InvalidDataRead)
}

fn read_u16<'de, R: Read<'de>>(rd: &mut R) -> Result<u16, Error> {
fn read_u16<'de, R: ReadSlice<'de>>(rd: &mut R) -> Result<u16, Error> {
rd.read_u16::<byteorder::BigEndian>().map_err(Error::InvalidDataRead)
}

fn read_u32<'de, R: Read<'de>>(rd: &mut R) -> Result<u32, Error> {
fn read_u32<'de, R: ReadSlice<'de>>(rd: &mut R) -> Result<u32, Error> {
rd.read_u32::<byteorder::BigEndian>().map_err(Error::InvalidDataRead)
}

impl<'de, 'a, R: Read<'de>> serde::Deserializer<'de> for &'a mut Deserializer<R> {
impl<'de, 'a, R: ReadSlice<'de>> serde::Deserializer<'de> for &'a mut Deserializer<R> {
type Error = Error;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -334,7 +340,6 @@ impl<'de, 'a, R: Read<'de>> serde::Deserializer<'de> for &'a mut Deserializer<R>
self.read_bytes(len, visitor)
}
Marker::Reserved => Err(Error::TypeMismatch(Marker::Reserved)),
// TODO: Make something with exts.
marker => Err(Error::TypeMismatch(marker)),
}
}
Expand Down Expand Up @@ -364,10 +369,7 @@ impl<'de, 'a, R: Read<'de>> serde::Deserializer<'de> for &'a mut Deserializer<R>
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error>
where V: Visitor<'de>
{
match read_array_len(&mut self.rd)? {
1 => visitor.visit_newtype_struct(self),
n => Err(Error::LengthMismatch(n as u32)),
}
visitor.visit_newtype_struct(self)
}

forward_to_deserialize_any! {
Expand All @@ -392,7 +394,7 @@ impl<'a, R: 'a> SeqAccess<'a, R> {
}
}

impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
impl<'de, 'a, R: ReadSlice<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
type Error = Error;

fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
Expand Down Expand Up @@ -425,7 +427,7 @@ impl<'a, R: 'a> MapAccess<'a, R> {
}
}

impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
impl<'de, 'a, R: ReadSlice<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
type Error = Error;

fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
Expand Down Expand Up @@ -455,7 +457,7 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
/// # Note
///
/// We use default behaviour for new type, which decodes enums with a single value as a tuple.
pub struct VariantAccess<'a, R: 'a> {
struct VariantAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}

Expand All @@ -467,7 +469,7 @@ impl<'a, R: 'a> VariantAccess<'a, R> {
}
}

impl<'de, 'a, R: Read<'de>> de::EnumAccess<'de> for VariantAccess<'a, R> {
impl<'de, 'a, R: ReadSlice<'de>> de::EnumAccess<'de> for VariantAccess<'a, R> {
type Error = Error;
type Variant = Self;

Expand All @@ -482,7 +484,7 @@ impl<'de, 'a, R: Read<'de>> de::EnumAccess<'de> for VariantAccess<'a, R> {
}
}

impl<'de, 'a, R: Read<'de>> de::VariantAccess<'de> for VariantAccess<'a, R> {
impl<'de, 'a, R: ReadSlice<'de>> de::VariantAccess<'de> for VariantAccess<'a, R> {
type Error = Error;

fn unit_variant(self) -> Result<(), Error> {
Expand All @@ -493,7 +495,7 @@ impl<'de, 'a, R: Read<'de>> de::VariantAccess<'de> for VariantAccess<'a, R> {
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed<'de>
{
read_array_len(&mut self.de.rd)?;
// read_array_len(&mut self.de.rd)?;
seed.deserialize(self.de)
}

Expand All @@ -516,52 +518,57 @@ pub enum Reference<'b, 'c, T: ?Sized + 'static> {
Copied(&'c T),
}

pub trait Read<'de>: io::Read {
/// Extends the `Read` trait by <...>.
///
/// Used to allow zero-copy reading.
pub trait ReadSlice<'de>: Read {
fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error>;
}

#[derive(Debug)]
pub struct SliceReader<'a> {
inner: &'a [u8],
}

impl<'a> SliceReader<'a> {
fn new(slice: &'a [u8]) -> Self {
SliceReader {
Self {
inner: slice,
}
}
}

impl<'de> Read<'de> for SliceReader<'de> {
impl<'de> ReadSlice<'de> for SliceReader<'de> {
#[inline]
fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error> {
if len > self.inner.len() {
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected EOF"))
return Err(ErrorKind::UnexpectedEof.into());
}
let (a, b) = self.inner.split_at(len);
self.inner = b;
Ok(Reference::Borrowed(a))
}
}

impl<'a> io::Read for SliceReader<'a> {
impl<'a> Read for SliceReader<'a> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.inner.read(buf)
}

#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), io::Error> {
self.inner.read_exact(buf)
}
}

pub struct ReadReader<R: io::Read> {
#[derive(Debug)]
pub struct ReadReader<R: Read> {
inner: R,
buf: Vec<u8>
}

impl<R: io::Read> ReadReader<R> {
impl<R: Read> ReadReader<R> {
fn new(rd: R) -> Self {
ReadReader {
inner: rd,
Expand All @@ -570,7 +577,7 @@ impl<R: io::Read> ReadReader<R> {
}
}

impl<'de, R: io::Read> Read<'de> for ReadReader<R> {
impl<'de, R: Read> ReadSlice<'de> for ReadReader<R> {
#[inline]
fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error> {
self.buf.resize(len, 0u8);
Expand All @@ -581,7 +588,7 @@ impl<'de, R: io::Read> Read<'de> for ReadReader<R> {
}
}

impl<R: io::Read> io::Read for ReadReader<R> {
impl<R: Read> Read for ReadReader<R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
Expand Down Expand Up @@ -610,15 +617,16 @@ fn test_slice_read() {
/// by `T`. It can also fail if the structure is correct but `T`'s implementation of `Deserialize`
/// decides that something is wrong with the data, for example required struct fields are missing.
pub fn from_read<R, T>(rd: R) -> Result<T, Error>
where R: io::Read,
T: DeserializeOwned
where R: Read,
T: DeserializeOwned
{
Deserialize::deserialize(&mut Deserializer::new(rd))
}

/// Deserializes a byte slice into the desired type.
pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
where T: serde::Deserialize<'a>
where
T: Deserialize<'a>
{
let mut de = Deserializer::from_slice(input);
serde::Deserialize::deserialize(&mut de)
Expand Down

0 comments on commit 7b4d338

Please sign in to comment.