Skip to content

Commit 435ffe5

Browse files
committed
Merge pull request #33 from 3Hren/pr/extend-en-de
Extend Encoder/Decoder interface
2 parents dcc689d + f1a183e commit 435ffe5

6 files changed

Lines changed: 119 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
- Initial support for [Serde](https://github.com/serde-rs/serde) serializer and deserializer.
88
- Efficient bytes serialization with Serde.
99
- Efficient binaries deserialization with Serde using `ByteBuf`.
10+
- Rust serialize Decoder now can provide the underlying reader both by reference or by value, destroying itself in the
11+
last case.
1012

1113
### Changed
1214
- Renamed `PositiveFixnum` marker to `FixPos`.

src/decode.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,20 @@ impl<R: Read> Decoder<R> {
18101810
}
18111811
}
18121812

1813-
// TODO: Add an ability to borrow underlying reader and to destruct this decoder.
1813+
/// Gets a reference to the underlying reader in this decoder.
1814+
pub fn get_ref(&self) -> &R {
1815+
&self.rd
1816+
}
1817+
1818+
/// Gets a mutable reference to the underlying reader in this decoder.
1819+
pub fn get_mut(&mut self) -> &mut R {
1820+
&mut self.rd
1821+
}
1822+
1823+
/// Consumes this decoder returning the underlying reader.
1824+
pub fn into_inner(self) -> R {
1825+
self.rd
1826+
}
18141827
}
18151828

18161829
/// Unstable: docs; examples; incomplete
@@ -2200,11 +2213,26 @@ impl<R: Read> Deserializer<R> {
22002213
}
22012214
}
22022215

2216+
/// Gets a reference to the underlying reader in this decoder.
2217+
pub fn get_ref(&self) -> &R {
2218+
&self.rd
2219+
}
2220+
2221+
/// Gets a mutable reference to the underlying reader in this decoder.
2222+
pub fn get_mut(&mut self) -> &mut R {
2223+
&mut self.rd
2224+
}
2225+
2226+
/// Consumes this decoder returning the underlying reader.
2227+
pub fn into_inner(self) -> R {
2228+
self.rd
2229+
}
2230+
22032231
fn read_str<V>(&mut self, len: u32, mut visitor: V) -> Result<V::Value>
22042232
where V: serde::de::Visitor
22052233
{
22062234
self.buf.clear();
2207-
self.buf.extend((0 .. len).map(|_| 0));
2235+
self.buf.extend((0..len).map(|_| 0));
22082236
visitor.visit_str(try!(read_str_data(&mut self.rd, len, &mut self.buf[..])))
22092237
}
22102238

@@ -2242,8 +2270,6 @@ impl<R: Read> Deserializer<R> {
22422270

22432271
visitor.visit_bytes(&mut self.buf[..])
22442272
}
2245-
2246-
// TODO: Add an ability to borrow underlying reader and to destruct this decoder.
22472273
}
22482274

22492275
/// Unstable: docs; examples; incomplete

src/encode.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ impl From<ValueWriteError> for Error {
972972
///
973973
/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
974974
/// operation is retried.
975-
// TODO: Docs. Examples.
975+
// TODO: Docs. Examples, variant encoding policy.
976976
pub struct Encoder<'a> {
977977
wr: &'a mut Write,
978978
}
@@ -984,8 +984,6 @@ impl<'a> Encoder<'a> {
984984
wr: wr,
985985
}
986986
}
987-
988-
// TODO: An ability to borrow the `Write`.
989987
}
990988

991989
impl<'a> serialize::Encoder for Encoder<'a> {
@@ -1285,8 +1283,6 @@ impl<'a> Serializer<'a> {
12851283
wr: wr,
12861284
}
12871285
}
1288-
1289-
// TODO: An ability to borrow the `Write`.
12901286
}
12911287

12921288
impl<'a> serde::Serializer for Serializer<'a> {

tests/func/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
mod encode;
22
mod decode;
3+
mod rserialize;
4+
mod serde;

tests/func/rserialize.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::io::Cursor;
2+
3+
use rustc_serialize::Decodable;
4+
5+
use msgpack::Decoder;
6+
7+
#[test]
8+
fn pass_decoder_get_ref() {
9+
let buf = [0xc0];
10+
let cur = Cursor::new(&buf[..]);
11+
12+
let mut decoder = Decoder::new(cur);
13+
14+
assert_eq!((), Decodable::decode(&mut decoder).ok().unwrap());
15+
assert_eq!(1, decoder.get_ref().position());
16+
}
17+
18+
#[test]
19+
fn pass_decoder_get_mut() {
20+
let buf = [0xc0];
21+
let cur = Cursor::new(&buf[..]);
22+
23+
let mut decoder = Decoder::new(cur);
24+
25+
assert_eq!((), Decodable::decode(&mut decoder).ok().unwrap());
26+
decoder.get_mut().set_position(0);
27+
28+
assert_eq!((), Decodable::decode(&mut decoder).ok().unwrap());
29+
}
30+
31+
#[test]
32+
fn pass_decoder_into_inner() {
33+
let buf = [0xc0];
34+
let cur = Cursor::new(&buf[..]);
35+
36+
let mut decoder = Decoder::new(cur);
37+
38+
assert_eq!((), Decodable::decode(&mut decoder).ok().unwrap());
39+
let cur = decoder.into_inner();
40+
41+
assert_eq!(1, cur.position());
42+
}

tests/func/serde.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::io::Cursor;
2+
3+
use serde::Deserialize;
4+
5+
use msgpack::Deserializer;
6+
7+
#[test]
8+
fn pass_deserializer_get_ref() {
9+
let buf = [0xc0];
10+
let cur = Cursor::new(&buf[..]);
11+
12+
let mut de = Deserializer::new(cur);
13+
14+
assert_eq!((), Deserialize::deserialize(&mut de).ok().unwrap());
15+
assert_eq!(1, de.get_ref().position());
16+
}
17+
18+
#[test]
19+
fn pass_deserializer_get_mut() {
20+
let buf = [0xc0];
21+
let cur = Cursor::new(&buf[..]);
22+
23+
let mut de = Deserializer::new(cur);
24+
25+
assert_eq!((), Deserialize::deserialize(&mut de).ok().unwrap());
26+
de.get_mut().set_position(0);
27+
28+
assert_eq!((), Deserialize::deserialize(&mut de).ok().unwrap());
29+
}
30+
31+
#[test]
32+
fn pass_deserializer_into_inner() {
33+
let buf = [0xc0];
34+
let cur = Cursor::new(&buf[..]);
35+
36+
let mut de = Deserializer::new(cur);
37+
38+
assert_eq!((), Deserialize::deserialize(&mut de).ok().unwrap());
39+
let cur = de.into_inner();
40+
41+
assert_eq!(1, cur.position());
42+
}

0 commit comments

Comments
 (0)