mirror of https://git.sr.ht/~stygianentity/bincode
Updated readme.md and added a test for the examples (#417)
* Updated readme.md and added a test for the examples * Removed tests/readme.rs and made the readme doctest a (hidden) part of lib.rs
This commit is contained in:
parent
f09d839d10
commit
0c13c891c5
22
README.md
22
README.md
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
<img align="right" src="./logo.svg" />
|
<img align="right" src="./logo.svg" />
|
||||||
|
|
||||||

|
[](https://github.com/bincode-org/bincode/actions)
|
||||||
[](https://crates.io/crates/bincode)
|
[](https://crates.io/crates/bincode)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
<!-- [](https://blog.rust-lang.org/2020/02/27/Rust-1.41.1.html) -->
|
<!-- [](https://blog.rust-lang.org/2020/02/27/Rust-1.41.1.html) -->
|
||||||
[](https://matrix.to/#/#bincode:matrix.org)
|
[](https://matrix.to/#/#bincode:matrix.org)
|
||||||
|
|
@ -30,26 +30,30 @@ library.
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use serde::{Serialize, Deserialize};
|
use bincode::{config::Configuration, Decode, Encode};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Encode, Decode, PartialEq, Debug)]
|
||||||
struct Entity {
|
struct Entity {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Encode, Decode, PartialEq, Debug)]
|
||||||
struct World(Vec<Entity>);
|
struct World(Vec<Entity>);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let config = Configuration::standard();
|
||||||
|
|
||||||
let world = World(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> = bincode::serialize(&world).unwrap();
|
let encoded: Vec<u8> = bincode::encode_to_vec(&world, config).unwrap();
|
||||||
|
|
||||||
// 8 bytes for the length of the vector, 4 bytes per float.
|
// The length of the vector is encoded as a varint u64, which in this case gets collapsed to a single byte
|
||||||
assert_eq!(encoded.len(), 8 + 4 * 4);
|
// See the documentation on varint for more info for that.
|
||||||
|
// The 4 floats are encoded in 4 bytes each.
|
||||||
|
assert_eq!(encoded.len(), 1 + 4 * 4);
|
||||||
|
|
||||||
let decoded: World = bincode::deserialize(&encoded[..]).unwrap();
|
let decoded: World = bincode::decode_from_slice(&encoded[..], config).unwrap();
|
||||||
|
|
||||||
assert_eq!(world, decoded);
|
assert_eq!(world, decoded);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -143,3 +143,9 @@ pub fn decode_from_reader<D: de::Decode, R: Reader, C: Config>(
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
#![doc = include_str!("../docs/spec.md")]
|
#![doc = include_str!("../docs/spec.md")]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test the examples in readme.md
|
||||||
|
#[cfg(all(feature = "alloc", feature = "derive", doctest))]
|
||||||
|
mod readme {
|
||||||
|
#![doc = include_str!("../readme.md")]
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue