|
| 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.0.0-beta/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 | +- `.with_limit(n)` has been changed to `.with_limit::<n>()`. |
| 31 | +- `.with_native_endian()` has been removed. Use `.with_big_endian()` or `with_little_endian()` instead. |
| 32 | +- `.with_varint_encoding()` has been renamed to `.with_variable_int_encoding()`. |
| 33 | +- `.with_fixint_encoding()` has been renamed to `.with_fixed_int_encoding()`. |
| 34 | +- `.reject_trailing_bytes()` has been removed. |
| 35 | +- `.allow_trailing_bytes()` has been removed. |
| 36 | +- You can no longer (de)serialize from the `Options` trait directly. Use one of the `encode_` or `decode_` methods. |
| 37 | + |
| 38 | +Because of confusion with `Options` defaults in bincode 1, we have made `Configuration` mandatory in all calls in bincode 2. |
| 39 | + |
| 40 | +## Migrating with `serde` |
| 41 | + |
| 42 | +Make sure to include bincode 2 with the `serde` feature enabled. |
| 43 | + |
| 44 | +```toml |
| 45 | +[dependencies] |
| 46 | +bincode = { version = "2.0.0-beta", features = ["serde"] } |
| 47 | + |
| 48 | +# Optionally you can disable the `derive` feature: |
| 49 | +# bincode = { version = "2.0.0-beta", default-features = false, features = ["std", "serde"] } |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +Then replace the following functions: (`Configuration` is `bincode::config::legacy()` by default) |
| 54 | + |
| 55 | +|Bincode 1|Bincode 2| |
| 56 | +|--|--| |
| 57 | +|`bincode::deserialize(&[u8])`|`bincode::serde::decode_from_slice(&[u8], Configuration)`<br />`bincode::serde::decode_borrowed_from_slice(&[u8], Configuration)`| |
| 58 | +|`bincode::deserialize_from(std::io::Read)`|`bincode::serde::decode_from_std_read(std::io::Read, Configuration)`| |
| 59 | +|`bincode::deserialize_from_custom(BincodeRead)`|`bincode::serde::decode_from_reader(Reader, Configuration)`| |
| 60 | +||| |
| 61 | +|`bincode::serialize(T)`|`bincode::serde::encode_to_vec(T, Configuration)`<br />`bincode::serde::encode_into_slice(T, &mut [u8], Configuration)`| |
| 62 | +|`bincode::serialize_into(std::io::Write, T)`|`bincode::serde::encode_into_std_write(T, std::io::Write, Configuration)`| |
| 63 | +|`bincode::serialized_size(T)`|Currently not implemented| |
| 64 | + |
| 65 | +## Migrating to `bincode-derive` |
| 66 | + |
| 67 | +`bincode-derive` is enabled by default. If you're using `default-features = false`, make sure to add `features = ["derive"]` to your `Cargo.toml`. |
| 68 | + |
| 69 | +```toml,ignore |
| 70 | +[dependencies] |
| 71 | +bincode = "2.0.0-beta" |
| 72 | +
|
| 73 | +# If you need `no_std` with `alloc`: |
| 74 | +# bincode = { version = "2.0.0-beta", default-features = false, features = ["derive", "alloc"] } |
| 75 | +
|
| 76 | +# If you need `no_std` and no `alloc`: |
| 77 | +# bincode = { version = "2.0.0-beta", default-features = false, features = ["derive"] } |
| 78 | +``` |
| 79 | + |
| 80 | +Replace or add the following attributes. You are able to use both `serde-derive` and `bincode-derive` side-by-side. |
| 81 | + |
| 82 | +|serde-derive|bincode-derive| |
| 83 | +|---|---| |
| 84 | +|`#[derive(serde::Serialize)]`|`#[derive(bincode::Encode)]`| |
| 85 | +|`#[derive(serde::Deserialize)]`|`#[derive(bincode::Decode)]`| |
| 86 | + |
| 87 | +**note:** To implement these traits manually, see the documentation of [Encode](https://docs.rs/bincode/2.0.0-beta/bincode/enc/trait.Encode.html) and [Decode](https://docs.rs/bincode/2.0.0-beta/bincode/de/trait.Decode.html). |
| 88 | + |
| 89 | +**note:** For more information on using `bincode-derive` with external libraries, see [below](#bincode-derive-and-libraries). |
| 90 | + |
| 91 | +Then replace the following functions: (`Configuration` is `bincode::config::legacy()` by default) |
| 92 | + |
| 93 | +|Bincode 1|Bincode 2| |
| 94 | +|--|--| |
| 95 | +|`bincode::deserialize(&[u8])`|`bincode::decode_from_slice(&bytes, Configuration)`<br />`bincode::decode_borrowed_from_slice(&[u8], Configuration)`| |
| 96 | +|`bincode::deserialize_from(std::io::Read)`|`bincode::decode_from_std_read(std::io::Read, Configuration)`| |
| 97 | +|`bincode::deserialize_from_custom(BincodeRead)`|`bincode::decode_from_reader(Reader, Configuration)`| |
| 98 | +||| |
| 99 | +|`bincode::serialize(T)`|`bincode::encode_to_vec(T, Configuration)`<br />`bincode::encode_into_slice(t: T, &mut [u8], Configuration)`| |
| 100 | +|`bincode::serialize_into(std::io::Write, T)`|`bincode::encode_into_std_write(T, std::io::Write, Configuration)`| |
| 101 | +|`bincode::serialized_size(T)`|Currently not implemented| |
| 102 | + |
| 103 | + |
| 104 | +### Bincode derive and libraries |
| 105 | + |
| 106 | +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)]`: |
| 107 | +- Enable the `serde` feature and add a `#[bincode(with_serde)]` above each field that implements `serde::Serialize/Deserialize` but not `Encode/Decode` |
| 108 | +- Enable the `serde` feature and wrap your field in [bincode::serde::Compat](https://docs.rs/bincode/2.0.0-beta/bincode/serde/struct.Compat.html) or [bincode::serde::BorrowCompat](https://docs.rs/bincode/2.0.0-beta/bincode/serde/struct.BorrowCompat.html) |
| 109 | +- Make a pull request to the library: |
| 110 | + - Make sure to be respectful, most of the developers are doing this in their free time. |
| 111 | + - Add a dependency `bincode = { version = "2.0.0-beta", default-features = false, optional = true }` to the `Cargo.toml` |
| 112 | + - Implement [Encode](https://docs.rs/bincode/2.0.0-beta/bincode/enc/trait.Encode.html) |
| 113 | + - Implement [Decode](https://docs.rs/bincode/2.0.0-beta/bincode/de/trait.Decode.html) |
| 114 | + - Make sure both of these implementations have a `#[cfg(feature = "bincode")]` attribute. |
0 commit comments