Make example work with serde (#131)

* Make example work with serde

* Restore asserts
This commit is contained in:
slyrz 2017-03-07 19:14:46 +01:00 committed by Ty Overby
parent 01f13dc1b8
commit 33b07e2bce
2 changed files with 18 additions and 31 deletions

View File

@ -26,38 +26,33 @@ library.
## Example
```rust
#[macro_use]
extern crate serde_derive;
extern crate bincode;
extern crate rustc_serialize;
use bincode::SizeLimit;
use bincode::rustc_serialize::{encode, decode};
use bincode::{serialize, deserialize, SizeLimit};
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
#[derive(Serialize, Deserialize, PartialEq)]
struct Entity {
x: f32,
y: f32,
}
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
struct World {
entities: Vec<Entity>
}
#[derive(Serialize, Deserialize, PartialEq)]
struct World(Vec<Entity>);
fn main() {
let world = World {
entities: vec![Entity {x: 0.0, y: 4.0}, Entity {x: 10.0, y: 20.5}]
};
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
let encoded: Vec<u8> = encode(&world, SizeLimit::Infinite).unwrap();
let encoded: Vec<u8> = serialize(&world, SizeLimit::Infinite).unwrap();
// 8 bytes for the length of the vector, 4 bytes per float.
assert_eq!(encoded.len(), 8 + 4 * 4);
let decoded: World = decode(&encoded[..]).unwrap();
let decoded: World = deserialize(&encoded[..]).unwrap();
assert!(world == decoded);
}
```

View File

@ -1,35 +1,27 @@
/*
#[macro_use]
extern crate serde_derive;
extern crate bincode;
extern crate
use bincode::SizeLimit;
use bincode::rustc_serialize::{encode, decode};
use bincode::{serialize, deserialize, SizeLimit};
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
#[derive(Serialize, Deserialize, PartialEq)]
struct Entity {
x: f32,
y: f32,
}
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
struct World {
entities: Vec<Entity>
}
#[derive(Serialize, Deserialize, PartialEq)]
struct World(Vec<Entity>);
fn main() {
let world = World {
entities: vec![Entity {x: 0.0, y: 4.0}, Entity {x: 10.0, y: 20.5}]
};
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
let encoded: Vec<u8> = encode(&world, SizeLimit::Infinite).unwrap();
let encoded: Vec<u8> = serialize(&world, SizeLimit::Infinite).unwrap();
// 8 bytes for the length of the vector, 4 bytes per float.
assert_eq!(encoded.len(), 8 + 4 * 4);
let decoded: World = decode(&encoded[..]).unwrap();
let decoded: World = deserialize(&encoded[..]).unwrap();
assert!(world == decoded);
}
*/
fn main() {}