mirror of https://git.sr.ht/~stygianentity/bincode
Make example work with serde (#131)
* Make example work with serde * Restore asserts
This commit is contained in:
parent
01f13dc1b8
commit
33b07e2bce
23
README.md
23
README.md
|
|
@ -26,38 +26,33 @@ library.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
```rust
|
```rust
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
extern crate bincode;
|
extern crate bincode;
|
||||||
extern crate rustc_serialize;
|
|
||||||
|
|
||||||
use bincode::SizeLimit;
|
use bincode::{serialize, deserialize, SizeLimit};
|
||||||
use bincode::rustc_serialize::{encode, decode};
|
|
||||||
|
|
||||||
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
|
#[derive(Serialize, Deserialize, PartialEq)]
|
||||||
struct Entity {
|
struct Entity {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
|
#[derive(Serialize, Deserialize, PartialEq)]
|
||||||
struct World {
|
struct World(Vec<Entity>);
|
||||||
entities: Vec<Entity>
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let world = World {
|
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
|
||||||
entities: 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.
|
// 8 bytes for the length of the vector, 4 bytes per float.
|
||||||
assert_eq!(encoded.len(), 8 + 4 * 4);
|
assert_eq!(encoded.len(), 8 + 4 * 4);
|
||||||
|
|
||||||
let decoded: World = decode(&encoded[..]).unwrap();
|
let decoded: World = deserialize(&encoded[..]).unwrap();
|
||||||
|
|
||||||
assert!(world == decoded);
|
assert!(world == decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,27 @@
|
||||||
/*
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
extern crate bincode;
|
extern crate bincode;
|
||||||
extern crate
|
|
||||||
|
|
||||||
use bincode::SizeLimit;
|
use bincode::{serialize, deserialize, SizeLimit};
|
||||||
use bincode::rustc_serialize::{encode, decode};
|
|
||||||
|
|
||||||
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
|
#[derive(Serialize, Deserialize, PartialEq)]
|
||||||
struct Entity {
|
struct Entity {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcEncodable, RustcDecodable, PartialEq)]
|
#[derive(Serialize, Deserialize, PartialEq)]
|
||||||
struct World {
|
struct World(Vec<Entity>);
|
||||||
entities: Vec<Entity>
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let world = World {
|
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
|
||||||
entities: 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.
|
// 8 bytes for the length of the vector, 4 bytes per float.
|
||||||
assert_eq!(encoded.len(), 8 + 4 * 4);
|
assert_eq!(encoded.len(), 8 + 4 * 4);
|
||||||
|
|
||||||
let decoded: World = decode(&encoded[..]).unwrap();
|
let decoded: World = deserialize(&encoded[..]).unwrap();
|
||||||
|
|
||||||
assert!(world == decoded);
|
assert!(world == decoded);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
fn main() {}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue