| 1 |
# Migrating from bincode 1 to 2 |
| 2 |
|
| 3 |
Bincode 2 now has an optional dependency on `serde`. You can either use `serde`, or use bincode's own `derive` feature and macros. |
| 4 |
|
| 5 |
## From `Options` to `Configuration` |
| 6 |
|
| 7 |
Bincode 1 had the [Options](https://docs.rs/bincode/1/bincode/config/trait.Options.html) trait. This has been replaced with the [Configuration](https://docs.rs/bincode/2/bincode/config/struct.Configuration.html) struct. |
| 8 |
|
| 9 |
If you're using `Options`, you can change it like this: |
| 10 |
|
| 11 |
```rust,ignore |
| 12 |
# old |
| 13 |
bincode_1::DefaultOptions::new().with_varint_encoding() |
| 14 |
|
| 15 |
# new |
| 16 |
bincode_2::config::legacy().with_variable_int_encoding() |
| 17 |
``` |
| 18 |
|
| 19 |
If you want to be compatible with bincode 1, use the following table: |
| 20 |
|
| 21 |
| Bincode 1 | Bincode 2 | |
| 22 |
| ---------------------------------------------------------------------- | ----------------------------------------------- | |
| 23 |
| version 1.0 - 1.2 with `bincode_1::DefaultOptions::new().serialize(T)` | `config::legacy()` | |
| 24 |
| version 1.3+ with `bincode_1::DefaultOptions::new().serialize(T)` | `config::legacy().with_variable_int_encoding()` | |
| 25 |
| No explicit `Options`, e.g. `bincode::serialize(T)` | `config::legacy()` | |
| 26 |
|
| 27 |
If you do not care about compatibility with bincode 1, we recommend using `config::standard()` |
| 28 |
|
| 29 |
The following changes have been made: |
| 30 |
|
| 31 |
- `.with_limit(n)` has been changed to `.with_limit::<n>()`. |
| 32 |
- `.with_native_endian()` has been removed. Use `.with_big_endian()` or `with_little_endian()` instead. |
| 33 |
- `.with_varint_encoding()` has been renamed to `.with_variable_int_encoding()`. |
| 34 |
- `.with_fixint_encoding()` has been renamed to `.with_fixed_int_encoding()`. |
| 35 |
- `.reject_trailing_bytes()` has been removed. |
| 36 |
- `.allow_trailing_bytes()` has been removed. |
| 37 |
- You can no longer (de)serialize from the `Options` trait directly. Use one of the `encode_` or `decode_` methods. |
| 38 |
|
| 39 |
Because of confusion with `Options` defaults in bincode 1, we have made `Configuration` mandatory in all calls in bincode 2. |
| 40 |
|
| 41 |
## Migrating with `serde` |
| 42 |
|
| 43 |
You may wish to stick with `serde` when migrating to bincode 2, for example if you are using serde-exclusive derive features such as `#[serde(deserialize_with)]`. |
| 44 |
|
| 45 |
If so, make sure to include bincode 2 with the `serde` feature enabled, and use the `bincode::serde::*` functions instead of `bincode::*` as described below: |
| 46 |
|
| 47 |
```toml |
| 48 |
[dependencies] |
| 49 |
bincode = { version = "2.0", features = ["serde"] } |
| 50 |
|
| 51 |
# Optionally you can disable the `derive` feature: |
| 52 |
# bincode = { version = "2.0", default-features = false, features = ["std", "serde"] } |
| 53 |
``` |
| 54 |
|
| 55 |
Then replace the following functions: (`Configuration` is `bincode::config::legacy()` by default) |
| 56 |
|
| 57 |
| Bincode 1 | Bincode 2 | |
| 58 |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | |
| 59 |
| `bincode::deserialize(&[u8])` | `bincode::serde::decode_from_slice(&[u8], Configuration)`<br />`bincode::serde::borrow_decode_from_slice(&[u8], Configuration)` | |
| 60 |
| `bincode::deserialize_from(std::io::Read)` | `bincode::serde::decode_from_std_read(std::io::Read, Configuration)` | |
| 61 |
| `bincode::deserialize_from_custom(BincodeRead)` | `bincode::serde::decode_from_reader(Reader, Configuration)` | |
| 62 |
| | | |
| 63 |
| `bincode::serialize(T)` | `bincode::serde::encode_to_vec(T, Configuration)`<br />`bincode::serde::encode_into_slice(T, &mut [u8], Configuration)` | |
| 64 |
| `bincode::serialize_into(std::io::Write, T)` | `bincode::serde::encode_into_std_write(T, std::io::Write, Configuration)` | |
| 65 |
| `bincode::serialized_size(T)` | Currently not implemented | |
| 66 |
|
| 67 |
## Migrating from `serde` to `bincode-derive` |
| 68 |
|
| 69 |
`bincode-derive` is enabled by default. If you're using `default-features = false`, make sure to add `features = ["derive"]` to your `Cargo.toml`. |
| 70 |
|
| 71 |
```toml,ignore |
| 72 |
[dependencies] |
| 73 |
bincode = "2.0" |
| 74 |
|
| 75 |
# If you need `no_std` with `alloc`: |
| 76 |
# bincode = { version = "2.0", default-features = false, features = ["derive", "alloc"] } |
| 77 |
|
| 78 |
# If you need `no_std` and no `alloc`: |
| 79 |
# bincode = { version = "2.0", default-features = false, features = ["derive"] } |
| 80 |
``` |
| 81 |
|
| 82 |
Replace or add the following attributes. You are able to use both `serde-derive` and `bincode-derive` side-by-side. |
| 83 |
|
| 84 |
| serde-derive | bincode-derive | |
| 85 |
| ------------------------------- | ---------------------------- | |
| 86 |
| `#[derive(serde::Serialize)]` | `#[derive(bincode::Encode)]` | |
| 87 |
| `#[derive(serde::Deserialize)]` | `#[derive(bincode::Decode)]` | |
| 88 |
|
| 89 |
**note:** To implement these traits manually, see the documentation of [Encode](https://docs.rs/bincode/2/bincode/enc/trait.Encode.html) and [Decode](https://docs.rs/bincode/2/bincode/de/trait.Decode.html). |
| 90 |
|
| 91 |
**note:** For more information on using `bincode-derive` with external libraries, see [below](#bincode-derive-and-libraries). |
| 92 |
|
| 93 |
Then replace the following functions: (`Configuration` is `bincode::config::legacy()` by default) |
| 94 |
|
| 95 |
| Bincode 1 | Bincode 2 | |
| 96 |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | |
| 97 |
| `bincode::deserialize(&[u8])` | `bincode::decode_from_slice(&bytes, Configuration)`<br />`bincode::borrow_decode_from_slice(&[u8], Configuration)` | |
| 98 |
| `bincode::deserialize_from(std::io::Read)` | `bincode::decode_from_std_read(std::io::Read, Configuration)` | |
| 99 |
| `bincode::deserialize_from_custom(BincodeRead)` | `bincode::decode_from_reader(Reader, Configuration)` | |
| 100 |
| | | |
| 101 |
| `bincode::serialize(T)` | `bincode::encode_to_vec(T, Configuration)`<br />`bincode::encode_into_slice(t: T, &mut [u8], Configuration)` | |
| 102 |
| `bincode::serialize_into(std::io::Write, T)` | `bincode::encode_into_std_write(T, std::io::Write, Configuration)` | |
| 103 |
| `bincode::serialized_size(T)` | Currently not implemented | |
| 104 |
|
| 105 |
### Bincode derive and libraries |
| 106 |
|
| 107 |
Currently not many libraries support the traits `Encode` and `Decode`. There are a couple of options if you want to use `#[derive(bincode::Encode, bincode::Decode)]`: |
| 108 |
|
| 109 |
- Enable the `serde` feature and add a `#[bincode(with_serde)]` above each field that implements `serde::Serialize/Deserialize` but not `Encode/Decode` |
| 110 |
- Enable the `serde` feature and wrap your field in [bincode::serde::Compat](https://docs.rs/bincode/2/bincode/serde/struct.Compat.html) or [bincode::serde::BorrowCompat](https://docs.rs/bincode/2/bincode/serde/struct.BorrowCompat.html) |
| 111 |
- Make a pull request to the library: |
| 112 |
- Make sure to be respectful, most of the developers are doing this in their free time. |
| 113 |
- Add a dependency `bincode = { version = "2.0", default-features = false, optional = true }` to the `Cargo.toml` |
| 114 |
- Implement [Encode](https://docs.rs/bincode/2/bincode/enc/trait.Encode.html) |
| 115 |
- Implement [Decode](https://docs.rs/bincode/2/bincode/de/trait.Decode.html) |
| 116 |
- Make sure both of these implementations have a `#[cfg(feature = "bincode")]` attribute. |