From b5fbbf9b734a4a6702c210aba50687a066f23406 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Sun, 17 Mar 2024 14:20:49 -0400 Subject: [PATCH] Update spec for `Option` encoding (#702) * Update spec for Option encoding * Fix header rank --- docs/spec.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/spec.md b/docs/spec.md index 02d3095b..d7456a2d 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -86,6 +86,23 @@ assert_eq!(encoded.as_slice(), &[ ]); ``` +### Options +`Option` is always serialized using a single byte for the discriminant, even in `Fixint` encoding (which normally uses a `u32` for discriminant). + +```rust +let data: Option = Some(123); +let encoded = bincode::encode_to_vec(data, bincode::config::legacy()).unwrap(); +assert_eq!(encoded.as_slice(), &[ + 1, 123, 0, 0, 0 // the Some(..) tag is the leading 1 +]); + +let data: Option = None; +let encoded = bincode::encode_to_vec(data, bincode::config::legacy()).unwrap(); +assert_eq!(encoded.as_slice(), &[ + 0 // the None tag is simply 0 +]); +``` + # Collections Collections are encoded with their length value first, following by each entry of the collection. The length value is based on your `IntEncoding`.