Skip to content

Commit 8fbc5b2

Browse files
authored
Merge branch 'master' into checked-access-len
2 parents 7d1dcca + aaba38c commit 8fbc5b2

File tree

19 files changed

+86
-65
lines changed

19 files changed

+86
-65
lines changed

rmp-serde/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rmp-serde"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
authors = ["Evgeny Safronov <division494@gmail.com>"]
55
license = "MIT"
66
description = "Serde bindings for RMP"
@@ -13,13 +13,13 @@ edition = "2018"
1313

1414
[dependencies]
1515
byteorder = "1.4.3"
16-
serde = "1.0.133"
16+
serde = "1.0.136"
1717
rmp = { version = "0.8.11", path = "../rmp" }
1818

1919
[dev-dependencies]
2020
rmpv = { path = "../rmpv" }
2121
serde_bytes = "0.11.5"
22-
serde_derive = "1.0.133"
22+
serde_derive = "1.0.136"
2323

2424
[badges]
2525
maintenance = { status = "passively-maintained" }

rmp-serde/benches/buf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
extern crate test;
44

5-
use rmp_serde;
5+
66

77
use serde::{Deserialize, Serialize};
88

rmp-serde/src/decode.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ where
293293
/// Gets a reference to the underlying reader in this decoder.
294294
#[inline(always)]
295295
pub fn get_ref(&self) -> &R {
296-
self.rd.rd
296+
self.rd.whole_slice
297297
}
298298
}
299299

@@ -399,7 +399,7 @@ enum ExtDeserializerState {
399399
#[derive(Debug)]
400400
struct ExtDeserializer<'a, R, C> {
401401
rd: &'a mut R,
402-
config: C,
402+
_config: C,
403403
len: u32,
404404
state: ExtDeserializerState,
405405
}
@@ -408,7 +408,7 @@ impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> ExtDeserializer<'a, R
408408
fn new(d: &'a mut Deserializer<R, C>, len: u32) -> Self {
409409
ExtDeserializer {
410410
rd: &mut d.rd,
411-
config: d.config,
411+
_config: d.config,
412412
len,
413413
state: ExtDeserializerState::New,
414414
}
@@ -925,8 +925,11 @@ impl<R: Read> ReadReader<R> {
925925
impl<'de, R: Read> ReadSlice<'de> for ReadReader<R> {
926926
#[inline]
927927
fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error> {
928-
self.buf.resize(len, 0u8);
929-
self.rd.read_exact(&mut self.buf[..])?;
928+
self.buf.clear();
929+
let read = self.rd.by_ref().take(len as u64).read_to_end(&mut self.buf)?;
930+
if read != len {
931+
return Err(io::ErrorKind::UnexpectedEof.into());
932+
}
930933

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

957+
impl<'a, T> ReadRefReader<'a, T> {
958+
/// Returns the part that hasn't been consumed yet
959+
pub fn remaining_slice(&self) -> &'a [u8] {
960+
self.buf
961+
}
962+
}
963+
954964
impl<'a, T: AsRef<[u8]> + ?Sized> ReadRefReader<'a, T> {
955965
#[inline]
956966
fn new(rd: &'a T) -> Self {
957967
Self {
958-
rd,
968+
whole_slice: rd,
959969
buf: rd.as_ref(),
960970
}
961971
}
@@ -1011,21 +1021,10 @@ where R: Read,
10111021
Deserialize::deserialize(&mut Deserializer::new(rd))
10121022
}
10131023

1014-
/// Deserializes a byte slice into the desired type.
1015-
///
1016-
/// It just calls the more generic `from_read_ref`.
1017-
#[inline(always)]
1018-
pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
1019-
where
1020-
T: Deserialize<'a>
1021-
{
1022-
from_read_ref(input)
1023-
}
1024-
1025-
/// Deserialize an instance of type `T` from a reference I/O reader of MessagePack.
1024+
/// Deserialize a temporary scope-bound instance of type `T` from a slice, with zero-copy if possible.
10261025
///
10271026
/// Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the
1028-
/// data from the reader itself. For example, strings and byte-arrays won't be not copied.
1027+
/// data from the slice itself. For example, strings and byte-arrays won't copied.
10291028
///
10301029
/// # Errors
10311030
///
@@ -1047,9 +1046,20 @@ where
10471046
/// age: u8,
10481047
/// }
10491048
///
1050-
/// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_read_ref(&buf).unwrap());
1049+
/// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice(&buf).unwrap());
10511050
/// ```
1051+
#[inline(always)]
1052+
#[allow(deprecated)]
1053+
pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
1054+
where
1055+
T: Deserialize<'a>
1056+
{
1057+
from_read_ref(input)
1058+
}
1059+
10521060
#[inline]
1061+
#[doc(hidden)]
1062+
#[deprecated(note = "use from_slice")]
10531063
pub fn from_read_ref<'a, R, T>(rd: &'a R) -> Result<T, Error>
10541064
where
10551065
R: AsRef<[u8]> + ?Sized,

rmp-serde/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ use std::str::{self, Utf8Error};
6868
use serde::de;
6969
use serde::{Deserialize, Serialize};
7070

71-
pub use crate::decode::{from_read, from_read_ref, Deserializer};
71+
pub use crate::decode::{from_read, Deserializer};
72+
#[allow(deprecated)]
73+
pub use crate::decode::from_read_ref;
7274
pub use crate::encode::{to_vec, to_vec_named, Serializer};
7375

7476
pub use crate::decode::from_slice;
@@ -108,7 +110,8 @@ impl Raw {
108110
Self { s: Ok(v) }
109111
}
110112

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

@@ -266,7 +271,8 @@ impl<'a> RawRef<'a> {
266271
Self { s: Ok(v) }
267272
}
268273

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

rmp-serde/tests/decode_derive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ fn pass_from_slice() {
523523
}
524524

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

rmp-serde/tests/encode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ fn pass_raw_valid_utf8() {
328328
}
329329

330330
#[test]
331+
#[allow(deprecated)]
331332
fn pass_raw_invalid_utf8() {
332333
// >>> msgpack.dumps(msgpack.dumps([200, []]))
333334
// '\xa4\x92\xcc\xc8\x90'
@@ -350,6 +351,7 @@ fn pass_raw_ref_valid_utf8() {
350351
}
351352

352353
#[test]
354+
#[allow(deprecated)]
353355
fn pass_raw_ref_invalid_utf8() {
354356
// >>> msgpack.dumps(msgpack.dumps([200, []]))
355357
// '\xa4\x92\xcc\xc8\x90'

rmp/src/decode/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a> Bytes<'a> {
6363
/// Get a reference to the remaining bytes in the buffer.
6464
#[inline]
6565
pub fn remaining_slice(&self) -> &'a [u8] {
66-
&self.bytes
66+
self.bytes
6767
}
6868
/// Return the position of the input buffer.
6969
///

rmp/src/decode/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn read_str_data<'r, R>(rd: &mut R,
137137
Ok(decoded) => Ok(decoded),
138138
Err(err) => Err(DecodeStringError::InvalidUtf8(buf, err)),
139139
},
140-
Err(err) => Err(DecodeStringError::InvalidDataRead(From::from(err))),
140+
Err(err) => Err(DecodeStringError::InvalidDataRead(err)),
141141
}
142142
}
143143

rmp/tests/func/decode/bool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn from_bool_true() {
1616
let buf = [0xc3];
1717
let mut cur = Cursor::new(&buf[..]);
1818

19-
assert_eq!(true, read_bool(&mut cur).unwrap());
19+
assert!(read_bool(&mut cur).unwrap());
2020
assert_eq!(1, cur.position());
2121
}

rmp/tests/func/decode/null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn interrupt_safe() {
4949
self.state_ = 1;
5050
Err(Error::new(ErrorKind::Interrupted, "interrupted"))
5151
} else {
52-
assert!(buf.len() > 0);
52+
assert!(!buf.is_empty());
5353

5454
buf[0] = 0xc0;
5555
Ok(1)

0 commit comments

Comments
 (0)