diff --git a/README.md b/README.md
index a0a68e0..5e321d9 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,8 @@
-
-[](https://crates.io/crates/bincode)
+[](https://github.com/bincode-org/bincode/actions)
+[](https://crates.io/crates/bincode)
[](https://opensource.org/licenses/MIT)
[](https://matrix.to/#/#bincode:matrix.org)
@@ -30,26 +30,30 @@ library.
## Example
```rust
-use serde::{Serialize, Deserialize};
+use bincode::{config::Configuration, Decode, Encode};
-#[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[derive(Encode, Decode, PartialEq, Debug)]
struct Entity {
x: f32,
y: f32,
}
-#[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[derive(Encode, Decode, PartialEq, Debug)]
struct World(Vec);
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 encoded: Vec = bincode::serialize(&world).unwrap();
+ let encoded: Vec = bincode::encode_to_vec(&world, config).unwrap();
- // 8 bytes for the length of the vector, 4 bytes per float.
- assert_eq!(encoded.len(), 8 + 4 * 4);
+ // The length of the vector is encoded as a varint u64, which in this case gets collapsed to a single byte
+ // 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);
}
diff --git a/src/lib.rs b/src/lib.rs
index 5403aec..512f8a1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -143,3 +143,9 @@ pub fn decode_from_reader(
pub mod spec {
#![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")]
+}