Skip to content

Commit

Permalink
Updated spec.md
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Jan 18, 2022
1 parent b882000 commit d78a2a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ assert_eq!(encoded.as_slice(), &[

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

**note**: fixed array length do not have their `len` encoded. See [Arrays](#arrays)
**note**: fixed array length may not have their `len` encoded. See [Arrays](#arrays)

```rust
use bincode::config::Configuration;
Expand Down Expand Up @@ -133,7 +133,10 @@ assert_eq!(encoded.as_slice(), &[

# Arrays

Arrays are encoded *with* a length by default.
Array length is encoded based on the `.write_fixed_array_length` and `.skip_fixed_array_length()` config. When an array length is written, it will be encoded as a `u64`.

Note that `&[T]` is encoded as a [Collection](#collections).


```rust
use bincode::config::Configuration;
Expand All @@ -144,6 +147,12 @@ assert_eq!(encoded.as_slice(), &[
5, 0, 0, 0, 0, 0, 0, 0, // The length, as a u64
10, 20, 30, 40, 50, // the bytes
]);

let encoded = bincode::encode_to_vec(arr, Configuration::legacy().skip_fixed_array_length()).unwrap();
assert_eq!(encoded.as_slice(), &[
// no length
10, 20, 30, 40, 50, // the bytes
]);
```

This applies to any type `T` that implements `Encode`/`Decode`
Expand All @@ -168,11 +177,18 @@ let arr: [Foo; 2] = [
},
];

let encoded = bincode::encode_to_vec(arr, Configuration::legacy()).unwrap();
let encoded = bincode::encode_to_vec(&arr, Configuration::legacy()).unwrap();
assert_eq!(encoded.as_slice(), &[
2, 0, 0, 0, 0, 0, 0, 0, // Length of the array
10, 20, // First Foo
30, 40, // Second Foo
]);

let encoded = bincode::encode_to_vec(&arr, Configuration::legacy().skip_fixed_array_length()).unwrap();
assert_eq!(encoded.as_slice(), &[
// no length
10, 20, // First Foo
30, 40, // Second Foo
]);
```

2 changes: 1 addition & 1 deletion src/enc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use self::encoder::EncoderImpl;
/// # pub y: f32,
/// # }
/// impl bincode::Encode for Entity {
/// fn encode<E: bincode::Encoder>(
/// fn encode<E: bincode::enc::Encoder>(
/// &self,
/// encoder: &mut E,
/// ) -> core::result::Result<(), bincode::error::EncodeError> {
Expand Down

0 comments on commit d78a2a5

Please sign in to comment.