Skip to content

Commit

Permalink
Merge branch 'master' into checked-access-len
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Apr 19, 2022
2 parents 7d1dcca + aaba38c commit 8fbc5b2
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 65 deletions.
6 changes: 3 additions & 3 deletions rmp-serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rmp-serde"
version = "1.0.0"
version = "1.1.0"
authors = ["Evgeny Safronov <division494@gmail.com>"]
license = "MIT"
description = "Serde bindings for RMP"
Expand All @@ -13,13 +13,13 @@ edition = "2018"

[dependencies]
byteorder = "1.4.3"
serde = "1.0.133"
serde = "1.0.136"
rmp = { version = "0.8.11", path = "../rmp" }

[dev-dependencies]
rmpv = { path = "../rmpv" }
serde_bytes = "0.11.5"
serde_derive = "1.0.133"
serde_derive = "1.0.136"

[badges]
maintenance = { status = "passively-maintained" }
2 changes: 1 addition & 1 deletion rmp-serde/benches/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate test;

use rmp_serde;


use serde::{Deserialize, Serialize};

Expand Down
52 changes: 31 additions & 21 deletions rmp-serde/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
/// Gets a reference to the underlying reader in this decoder.
#[inline(always)]
pub fn get_ref(&self) -> &R {
self.rd.rd
self.rd.whole_slice
}
}

Expand Down Expand Up @@ -399,7 +399,7 @@ enum ExtDeserializerState {
#[derive(Debug)]
struct ExtDeserializer<'a, R, C> {
rd: &'a mut R,
config: C,
_config: C,
len: u32,
state: ExtDeserializerState,
}
Expand All @@ -408,7 +408,7 @@ impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> ExtDeserializer<'a, R
fn new(d: &'a mut Deserializer<R, C>, len: u32) -> Self {
ExtDeserializer {
rd: &mut d.rd,
config: d.config,
_config: d.config,
len,
state: ExtDeserializerState::New,
}
Expand Down Expand Up @@ -925,8 +925,11 @@ impl<R: Read> 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);
self.rd.read_exact(&mut self.buf[..])?;
self.buf.clear();
let read = self.rd.by_ref().take(len as u64).read_to_end(&mut self.buf)?;
if read != len {
return Err(io::ErrorKind::UnexpectedEof.into());
}

Ok(Reference::Copied(&self.buf[..]))
}
Expand All @@ -947,15 +950,22 @@ impl<R: Read> Read for ReadReader<R> {
/// Borrowed reader wrapper.
#[derive(Debug)]
pub struct ReadRefReader<'a, R: ?Sized> {
rd: &'a R,
whole_slice: &'a R,
buf: &'a [u8],
}

impl<'a, T> ReadRefReader<'a, T> {
/// Returns the part that hasn't been consumed yet
pub fn remaining_slice(&self) -> &'a [u8] {
self.buf
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> ReadRefReader<'a, T> {
#[inline]
fn new(rd: &'a T) -> Self {
Self {
rd,
whole_slice: rd,
buf: rd.as_ref(),
}
}
Expand Down Expand Up @@ -1011,21 +1021,10 @@ where R: Read,
Deserialize::deserialize(&mut Deserializer::new(rd))
}

/// Deserializes a byte slice into the desired type.
///
/// It just calls the more generic `from_read_ref`.
#[inline(always)]
pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
where
T: Deserialize<'a>
{
from_read_ref(input)
}

/// Deserialize an instance of type `T` from a reference I/O reader of MessagePack.
/// Deserialize a temporary scope-bound instance of type `T` from a slice, with zero-copy if possible.
///
/// Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the
/// data from the reader itself. For example, strings and byte-arrays won't be not copied.
/// data from the slice itself. For example, strings and byte-arrays won't copied.
///
/// # Errors
///
Expand All @@ -1047,9 +1046,20 @@ where
/// age: u8,
/// }
///
/// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_read_ref(&buf).unwrap());
/// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice(&buf).unwrap());
/// ```
#[inline(always)]
#[allow(deprecated)]
pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
where
T: Deserialize<'a>
{
from_read_ref(input)
}

#[inline]
#[doc(hidden)]
#[deprecated(note = "use from_slice")]
pub fn from_read_ref<'a, R, T>(rd: &'a R) -> Result<T, Error>
where
R: AsRef<[u8]> + ?Sized,
Expand Down
14 changes: 11 additions & 3 deletions rmp-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ use std::str::{self, Utf8Error};
use serde::de;
use serde::{Deserialize, Serialize};

pub use crate::decode::{from_read, from_read_ref, Deserializer};
pub use crate::decode::{from_read, Deserializer};
#[allow(deprecated)]
pub use crate::decode::from_read_ref;
pub use crate::encode::{to_vec, to_vec_named, Serializer};

pub use crate::decode::from_slice;
Expand Down Expand Up @@ -108,7 +110,8 @@ impl Raw {
Self { s: Ok(v) }
}

/// Converts a vector of bytes to a `Raw`.
/// DO NOT USE. See <https://github.com/3Hren/msgpack-rust/issues/305>
#[deprecated(note = "This implementation is unsound and dangerous. See https://github.com/3Hren/msgpack-rust/issues/305")]
pub fn from_utf8(v: Vec<u8>) -> Self {
match String::from_utf8(v) {
Ok(v) => Raw::new(v),
Expand Down Expand Up @@ -184,6 +187,8 @@ impl Serialize for Raw {
{
let s = match self.s {
Ok(ref s) => s.as_str(),
// FIXME: this is invalid. It should use a newtype hack instead.
// https://github.com/3Hren/msgpack-rust/issues/305
Err((ref b, ..)) => unsafe { mem::transmute(&b[..]) },
};

Expand Down Expand Up @@ -266,7 +271,8 @@ impl<'a> RawRef<'a> {
Self { s: Ok(v) }
}

/// Converts a vector of bytes to a `RawRef`.
/// DO NOT USE. See <https://github.com/3Hren/msgpack-rust/issues/305>
#[deprecated(note = "This implementation is unsound and dangerous. See https://github.com/3Hren/msgpack-rust/issues/305")]
pub fn from_utf8(v: &'a [u8]) -> Self {
match str::from_utf8(v) {
Ok(v) => RawRef::new(v),
Expand Down Expand Up @@ -326,6 +332,8 @@ impl<'a> Serialize for RawRef<'a> {
{
let s = match self.s {
Ok(ref s) => s,
// FIXME: this is invalid. It should use a newtype hack instead.
// https://github.com/3Hren/msgpack-rust/issues/305
Err((ref b, ..)) => unsafe { mem::transmute(b) },
};

Expand Down
1 change: 1 addition & 0 deletions rmp-serde/tests/decode_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ fn pass_from_slice() {
}

#[test]
#[allow(deprecated)]
fn pass_from_ref() {
let buf = [0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x8];

Expand Down
2 changes: 2 additions & 0 deletions rmp-serde/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ fn pass_raw_valid_utf8() {
}

#[test]
#[allow(deprecated)]
fn pass_raw_invalid_utf8() {
// >>> msgpack.dumps(msgpack.dumps([200, []]))
// '\xa4\x92\xcc\xc8\x90'
Expand All @@ -350,6 +351,7 @@ fn pass_raw_ref_valid_utf8() {
}

#[test]
#[allow(deprecated)]
fn pass_raw_ref_invalid_utf8() {
// >>> msgpack.dumps(msgpack.dumps([200, []]))
// '\xa4\x92\xcc\xc8\x90'
Expand Down
2 changes: 1 addition & 1 deletion rmp/src/decode/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a> Bytes<'a> {
/// Get a reference to the remaining bytes in the buffer.
#[inline]
pub fn remaining_slice(&self) -> &'a [u8] {
&self.bytes
self.bytes
}
/// Return the position of the input buffer.
///
Expand Down
2 changes: 1 addition & 1 deletion rmp/src/decode/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn read_str_data<'r, R>(rd: &mut R,
Ok(decoded) => Ok(decoded),
Err(err) => Err(DecodeStringError::InvalidUtf8(buf, err)),
},
Err(err) => Err(DecodeStringError::InvalidDataRead(From::from(err))),
Err(err) => Err(DecodeStringError::InvalidDataRead(err)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion rmp/tests/func/decode/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ fn from_bool_true() {
let buf = [0xc3];
let mut cur = Cursor::new(&buf[..]);

assert_eq!(true, read_bool(&mut cur).unwrap());
assert!(read_bool(&mut cur).unwrap());
assert_eq!(1, cur.position());
}
2 changes: 1 addition & 1 deletion rmp/tests/func/decode/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn interrupt_safe() {
self.state_ = 1;
Err(Error::new(ErrorKind::Interrupted, "interrupted"))
} else {
assert!(buf.len() > 0);
assert!(!buf.is_empty());

buf[0] = 0xc0;
Ok(1)
Expand Down
20 changes: 10 additions & 10 deletions rmp/tests/func/decode/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ fn from_str_strfix() {
let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
let mut cur = Cursor::new(buf);

let mut out: &mut [u8] = &mut [0u8; 16];
let out: &mut [u8] = &mut [0u8; 16];

assert_eq!("le message", read_str(&mut cur, &mut out).unwrap());
assert_eq!("le message", read_str(&mut cur, out).unwrap());
assert_eq!(11, cur.position());
}

Expand All @@ -148,9 +148,9 @@ fn from_str_strfix_extra_data() {
let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00];
let mut cur = Cursor::new(buf);

let mut out: &mut [u8] = &mut [0u8; 16];
let out: &mut [u8] = &mut [0u8; 16];

assert_eq!("le message", read_str(&mut cur, &mut out).unwrap());
assert_eq!("le message", read_str(&mut cur, out).unwrap());
assert_eq!(11, cur.position());
}

Expand All @@ -159,9 +159,9 @@ fn from_str_strfix_exact_buffer() {
let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
let mut cur = Cursor::new(buf);

let mut out: &mut [u8] = &mut [0u8; 10];
let out: &mut [u8] = &mut [0u8; 10];

assert_eq!("le message", read_str(&mut cur, &mut out).unwrap());
assert_eq!("le message", read_str(&mut cur, out).unwrap());
assert_eq!(11, cur.position());
}

Expand All @@ -171,9 +171,9 @@ fn from_str_strfix_invalid_utf8() {
let buf: &[u8] = &[0xa2, 0xc3, 0x28];
let mut cur = Cursor::new(buf);

let mut out: &mut [u8] = &mut [0u8; 16];
let out: &mut [u8] = &mut [0u8; 16];

match read_str(&mut cur, &mut out) {
match read_str(&mut cur, out) {
Err(DecodeStringError::InvalidUtf8(raw, _)) => {
assert_eq!(&[0xc3, 0x28], raw);
}
Expand All @@ -188,9 +188,9 @@ fn from_str_strfix_buffer_too_small() {
let buf: &[u8] = &[0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
let mut cur = Cursor::new(buf);

let mut out: &mut [u8] = &mut [0u8; 9];
let out: &mut [u8] = &mut [0u8; 9];

match read_str(&mut cur, &mut out) {
match read_str(&mut cur, out) {
Err(DecodeStringError::BufferSizeTooSmall(10)) => (),
other => panic!("unexpected result: {:?}", other)
}
Expand Down
4 changes: 2 additions & 2 deletions rmpv-tests/benches/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(test)]

extern crate rmp_serde as rmps;
use rmpv;

extern crate test;

use test::Bencher;
Expand Down Expand Up @@ -42,7 +42,7 @@ fn from_complex_read_value_ref(b: &mut Bencher) {
#[bench]
fn from_complex_zero_copy_decode(b: &mut Bencher) {
b.iter(|| {
let res: ValueRef<'_> = rmps::from_slice(&COMPLEX[..]).unwrap();
let res: ValueRef<'_> = rmps::from_slice(COMPLEX).unwrap();
test::black_box(res);
});
b.bytes = COMPLEX.len() as u64;
Expand Down
4 changes: 2 additions & 2 deletions rmpv-tests/tests/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ fn pass_ext_struct() {
{
let value = match self {
ExtStruct::One(data) => {
let tag = 1 as i8;
let tag = 1_i8;
let byte_buf = ByteBuf::from(vec![*data]);

(tag, byte_buf)
}
ExtStruct::Two(data) => {
let tag = 2 as i8;
let tag = 2_i8;
let byte_buf = ByteBuf::from(vec![*data]);

(tag, byte_buf)
Expand Down
10 changes: 5 additions & 5 deletions rmpv/src/decode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,27 @@ fn read_value_inner<R>(rd: &mut R, depth: usize) -> Result<Value, Error> where R
Value::Binary(vec)
}
Marker::FixExt1 => {
let len = 1 as usize;
let len = 1_usize;
let (ty, vec) = read_ext_body(rd, len, depth)?;
Value::Ext(ty, vec)
}
Marker::FixExt2 => {
let len = 2 as usize;
let len = 2_usize;
let (ty, vec) = read_ext_body(rd, len, depth)?;
Value::Ext(ty, vec)
}
Marker::FixExt4 => {
let len = 4 as usize;
let len = 4_usize;
let (ty, vec) = read_ext_body(rd, len, depth)?;
Value::Ext(ty, vec)
}
Marker::FixExt8 => {
let len = 8 as usize;
let len = 8_usize;
let (ty, vec) = read_ext_body(rd, len, depth)?;
Value::Ext(ty, vec)
}
Marker::FixExt16 => {
let len = 16 as usize;
let len = 16_usize;
let (ty, vec) = read_ext_body(rd, len, depth)?;
Value::Ext(ty, vec)
}
Expand Down
4 changes: 2 additions & 2 deletions rmpv/src/encode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub fn write_value<W>(wr: &mut W, val: &Value) -> Result<(), Error>
}
Value::String(Utf8String { ref s }) => {
match *s {
Ok(ref val) => write_str(wr, &val)?,
Ok(ref val) => write_str(wr, val)?,
Err(ref err) => write_bin(wr, &err.0)?,
}
}
Value::Binary(ref val) => {
write_bin(wr, &val)?;
write_bin(wr, val)?;
}
Value::Array(ref vec) => {
write_array_len(wr, vec.len() as u32)?;
Expand Down
Loading

0 comments on commit 8fbc5b2

Please sign in to comment.