Removes the "bytes read" return part from `decode`
This reverts commit 5fa0c2bd8a.
Conflicts:
Cargo.toml
This commit is contained in:
parent
e28fc57c0f
commit
e3837055d7
|
|
@ -24,7 +24,7 @@ fn main() {
|
|||
// 8 bytes for the length of the vector, 4 bytes per float.
|
||||
assert_eq!(encoded.len(), 8 + 4 * 4);
|
||||
|
||||
let (decoded, _): (World, _) = bincode::decode(&encoded[..]).unwrap();
|
||||
let decoded: World = bincode::decode(&encoded[..]).unwrap();
|
||||
|
||||
assert!(world == decoded);
|
||||
}
|
||||
|
|
|
|||
23
src/lib.rs
23
src/lib.rs
|
|
@ -17,8 +17,8 @@
|
|||
//! // The maximum size of the encoded message.
|
||||
//! let limit = bincode::SizeLimit::Bounded(20);
|
||||
//!
|
||||
//! let encoded: Vec<u8> = bincode::encode(&target, limit).unwrap();
|
||||
//! let (decoded, len): (Option<String>, u64) = bincode::decode(&encoded[..]).unwrap();
|
||||
//! let encoded: Vec<u8> = bincode::encode(&target, limit).unwrap();
|
||||
//! let decoded: Option<String> = bincode::decode(&encoded[..]).unwrap();
|
||||
//! assert_eq!(target, decoded);
|
||||
//! }
|
||||
//! ```
|
||||
|
|
@ -95,12 +95,9 @@ pub fn encode<T: Encodable>(t: &T, size_limit: SizeLimit) -> EncodingResult<Vec<
|
|||
|
||||
/// Decodes a slice of bytes into an object.
|
||||
///
|
||||
/// If successful, this function returns the decoded object along with the number
|
||||
/// of bytes that were read.
|
||||
///
|
||||
/// This method does not have a size-limit because with all the bytes contiguous
|
||||
/// in memory, then nothing is gained by having a limiter.
|
||||
pub fn decode<T: Decodable>(b: &[u8]) -> DecodingResult<(T, u64)> {
|
||||
/// This method does not have a size-limit because if you already have the bytes
|
||||
/// in memory, then you don't gain anything by having a limiter.
|
||||
pub fn decode<T: Decodable>(b: &[u8]) -> DecodingResult<T> {
|
||||
let mut b = b;
|
||||
decode_from(&mut b, SizeLimit::Infinite)
|
||||
}
|
||||
|
|
@ -128,8 +125,7 @@ pub fn encode_into<T: Encodable, W: Write>(t: &T,
|
|||
t.encode(&mut writer::EncoderWriter::new(w))
|
||||
}
|
||||
|
||||
/// Decodes an object directly from a `Read`er. If successful, returns the
|
||||
/// decoded object and the number of bytes read out of the `Read`er.
|
||||
/// Decoes an object directly from a `Buffer`ed Reader.
|
||||
///
|
||||
/// If the provided `SizeLimit` is reached, the decode will bail immediately.
|
||||
/// A SizeLimit can help prevent an attacker from flooding your server with
|
||||
|
|
@ -138,11 +134,8 @@ pub fn encode_into<T: Encodable, W: Write>(t: &T,
|
|||
/// If this returns an `DecodingError`, assume that the buffer that you passed
|
||||
/// in is in an invalid state, as the error could be returned during any point
|
||||
/// in the reading.
|
||||
pub fn decode_from<R: Read, T: Decodable>(r: &mut R, size_limit: SizeLimit) -> DecodingResult<(T, u64)> {
|
||||
let mut decoder_reader = reader::DecoderReader::new(r, size_limit);
|
||||
let value = try!(Decodable::decode(&mut decoder_reader));
|
||||
let bytes_read = decoder_reader.bytes_read();
|
||||
Ok((value, bytes_read))
|
||||
pub fn decode_from<R: Read, T: Decodable>(r: &mut R, size_limit: SizeLimit) -> DecodingResult<T> {
|
||||
Decodable::decode(&mut reader::DecoderReader::new(r, size_limit))
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -115,6 +115,7 @@ impl<'a, R: Read> DecoderReader<'a, R> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the number of bytes read from the contained Reader.
|
||||
pub fn bytes_read(&self) -> u64 {
|
||||
self.read
|
||||
}
|
||||
|
|
|
|||
28
src/test.rs
28
src/test.rs
|
|
@ -29,14 +29,14 @@ fn the_same<V>(element: V)
|
|||
let rf = RefBox::new(v);
|
||||
|
||||
let encoded = encode(&rf, Infinite).unwrap();
|
||||
let (decoded, _): (RefBox<'static, V>, _) = decode(&encoded[..]).unwrap();
|
||||
let decoded: RefBox<'static, V> = decode(&encoded[..]).unwrap();
|
||||
|
||||
decoded.take().deref() == v
|
||||
}
|
||||
|
||||
let size = encoded_size(&element);
|
||||
let encoded = encode(&element, Infinite).unwrap();
|
||||
let (decoded, _) = decode(&encoded[..]).unwrap();
|
||||
let decoded = decode(&encoded[..]).unwrap();
|
||||
assert!(element == decoded);
|
||||
assert!(size == encoded.len() as u64);
|
||||
assert!(ref_box_correct(&element))
|
||||
|
|
@ -215,12 +215,12 @@ fn decoding_errors() {
|
|||
fn too_big_decode() {
|
||||
let encoded = vec![0,0,0,3];
|
||||
let mut encoded_ref = &encoded[..];
|
||||
let decoded: Result<(u32, _), _> = decode_from(&mut encoded_ref, Bounded(3));
|
||||
let decoded: Result<u32, _> = decode_from(&mut encoded_ref, Bounded(3));
|
||||
assert!(decoded.is_err());
|
||||
|
||||
let encoded = vec![0,0,0,3];
|
||||
let mut encoded_ref = &encoded[..];
|
||||
let decoded: Result<(u32, _), _> = decode_from(&mut encoded_ref, Bounded(4));
|
||||
let decoded: Result<u32, _> = decode_from(&mut encoded_ref, Bounded(4));
|
||||
assert!(decoded.is_ok());
|
||||
}
|
||||
|
||||
|
|
@ -228,9 +228,9 @@ fn too_big_decode() {
|
|||
fn too_big_char_decode() {
|
||||
let encoded = vec![0x41];
|
||||
let mut encoded_ref = &encoded[..];
|
||||
let decoded: Result<(char, _), _> = decode_from(&mut encoded_ref, Bounded(1));
|
||||
let decoded: Result<char, _> = decode_from(&mut encoded_ref, Bounded(1));
|
||||
assert!(decoded.is_ok());
|
||||
assert_eq!(decoded.unwrap().0, 'A');
|
||||
assert_eq!(decoded.unwrap(), 'A');
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -277,9 +277,9 @@ fn test_refbox() {
|
|||
// Test 1
|
||||
{
|
||||
let encoded = encode(&Message::M1(RefBox::new(&large_object)), Infinite).unwrap();
|
||||
let decoded: (Message<'static>, _) = decode(&encoded[..]).unwrap();
|
||||
let decoded: Message<'static> = decode(&encoded[..]).unwrap();
|
||||
|
||||
match decoded.0 {
|
||||
match decoded {
|
||||
Message::M1(b) => assert!(b.take().deref() == &large_object),
|
||||
_ => assert!(false)
|
||||
}
|
||||
|
|
@ -288,9 +288,9 @@ fn test_refbox() {
|
|||
// Test 2
|
||||
{
|
||||
let encoded = encode(&Message::M2(RefBox::new(&large_map)), Infinite).unwrap();
|
||||
let decoded: (Message<'static>, _) = decode(&encoded[..]).unwrap();
|
||||
let decoded: Message<'static> = decode(&encoded[..]).unwrap();
|
||||
|
||||
match decoded.0 {
|
||||
match decoded {
|
||||
Message::M2(b) => assert!(b.take().deref() == &large_map),
|
||||
_ => assert!(false)
|
||||
}
|
||||
|
|
@ -301,8 +301,8 @@ fn test_refbox() {
|
|||
fn test_strbox() {
|
||||
let strx: &'static str = "hello world";
|
||||
let encoded = encode(&StrBox::new(strx), Infinite).unwrap();
|
||||
let decoded: (StrBox<'static>, _) = decode(&encoded[..]).unwrap();
|
||||
let stringx: String = decoded.0.take();
|
||||
let decoded: StrBox<'static> = decode(&encoded[..]).unwrap();
|
||||
let stringx: String = decoded.take();
|
||||
assert!(strx == &stringx[..]);
|
||||
}
|
||||
|
||||
|
|
@ -310,8 +310,8 @@ fn test_strbox() {
|
|||
fn test_slicebox() {
|
||||
let slice = [1u32, 2, 3 ,4, 5];
|
||||
let encoded = encode(&SliceBox::new(&slice), Infinite).unwrap();
|
||||
let decoded: (SliceBox<'static, u32>, _) = decode(&encoded[..]).unwrap();
|
||||
let vecx: Vec<u32> = decoded.0.take();
|
||||
let decoded: SliceBox<'static, u32> = decode(&encoded[..]).unwrap();
|
||||
let vecx: Vec<u32> = decoded.take();
|
||||
assert!(slice == &vecx[..]);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue