Skip to content

Commit da98438

Browse files
committed
Decoder: an ability to borrow the underlying R.
Or to consume the decoder obtaining the underlying reader.
1 parent dcc689d commit da98438

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/decode.rs

Lines changed: 14 additions & 1 deletion
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

src/encode.rs

Lines changed: 1 addition & 3 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> {

tests/func/mod.rs

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

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+
}

0 commit comments

Comments
 (0)