Skip to content

Commit 72bf5ce

Browse files
mkeeterVictorKoenders
authored andcommitted
Update spec for Option<T> encoding
1 parent 9255d49 commit 72bf5ce

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/spec.md

+17
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ assert_eq!(encoded.as_slice(), &[
8686
]);
8787
```
8888

89+
## Options
90+
`Option<T>` is always serialized using a single byte for the discriminant, even in `Fixint` encoding (which normally uses a `u32` for discriminant).
91+
92+
```rust
93+
let data: Option<u32> = Some(123);
94+
let encoded = bincode::encode_to_vec(data, bincode::config::legacy()).unwrap();
95+
assert_eq!(encoded.as_slice(), &[
96+
1, 123, 0, 0, 0 // the Some(..) tag is the leading 1
97+
]);
98+
99+
let data: Option<u32> = None;
100+
let encoded = bincode::encode_to_vec(data, bincode::config::legacy()).unwrap();
101+
assert_eq!(encoded.as_slice(), &[
102+
0 // the None tag is simply 0
103+
]);
104+
```
105+
89106
# Collections
90107

91108
Collections are encoded with their length value first, following by each entry of the collection. The length value is based on your `IntEncoding`.

0 commit comments

Comments
 (0)