Created
May 31, 2016 03:19
-
-
Save fero23/d835f85ebe729f87f0e17279aa50ae81 to your computer and use it in GitHub Desktop.
Comparison of bytevec and bincode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate bincode; | |
extern crate rustc_serialize; | |
#[macro_use] | |
extern crate bytevec; | |
use bincode::SizeLimit; | |
use bincode::rustc_serialize::{encode, decode}; | |
use bytevec::{ByteDecodable, ByteEncodable}; | |
bytevec_impls! { | |
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] | |
struct Container { | |
s: String | |
} | |
} | |
fn main() { | |
{ | |
let c1 = Container { s: "hello".to_string() }; | |
let bytes = encode(&c1, SizeLimit::Infinite).unwrap(); | |
let c2: Result<Container, _> = decode(&bytes[..bytes.len() - 2]); | |
println!("bincode result: {:?}", c2); | |
assert!(c2.is_ok()); | |
} | |
{ | |
let c1 = Container { s: "hello".to_string() }; | |
let bytes = c1.encode().unwrap(); | |
let c2 = Container::decode(&bytes[..bytes.len() - 2]); | |
println!("bytevec result: {:?}", c2); | |
assert!(c2.is_err()); | |
} | |
} | |
/* Results: | |
bincode result: Ok(Container { s: "hel" }) | |
bytevec result: Err(BadSizeDecodeError { wanted: EqualTo(9), actual: 7 }) | |
// Cargo.toml: | |
[dependencies] | |
bincode = "*" | |
rustc-serialize = "0.3.19" | |
bytevec = "*" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment