forked from romanz/electrs
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use bincode's new API, add utility functions for safer use
This resolves the deprecation warnings for the old `bincode::config()`, and adds wrapper utility functions that explicitly spells out whether serialization should use little or big endianness. Switching from `bincode::config()` to `bincode::options()` changed the default settings, which requires explicitly enabling `with_fixint_encoding` and `allow_trailing_bytes` to restore the old behaviour. Thanks @junderw for the investigative work and writing the code this is based on (mempool/electrs#34).
- Loading branch information
Showing
6 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! This module creates two sets of serialize and deserialize for bincode. | ||
//! They explicitly spell out the bincode settings so that switching to | ||
//! new versions in the future is less error prone. | ||
//! | ||
//! This is a list of all the row types and their settings for bincode. | ||
//! +--------------+--------+------------+----------------+------------+ | ||
//! | | Endian | Int Length | Allow Trailing | Byte Limit | | ||
//! +--------------+--------+------------+----------------+------------+ | ||
//! | TxHistoryRow | big | fixed | allow | unlimited | | ||
//! | All others | little | fixed | allow | unlimited | | ||
//! +--------------+--------+------------+----------------+------------+ | ||
//! | ||
//! Based on @junderw's https://github.com/mempool/electrs/pull/34. Thanks! | ||
|
||
use bincode::Options; | ||
|
||
pub fn serialize_big<T>(value: &T) -> Result<Vec<u8>, bincode::Error> | ||
where | ||
T: ?Sized + serde::Serialize, | ||
{ | ||
big_endian().serialize(value) | ||
} | ||
|
||
pub fn deserialize_big<'a, T>(bytes: &'a [u8]) -> Result<T, bincode::Error> | ||
where | ||
T: serde::Deserialize<'a>, | ||
{ | ||
big_endian().deserialize(bytes) | ||
} | ||
|
||
pub fn serialize_little<T>(value: &T) -> Result<Vec<u8>, bincode::Error> | ||
where | ||
T: ?Sized + serde::Serialize, | ||
{ | ||
little_endian().serialize(value) | ||
} | ||
|
||
pub fn deserialize_little<'a, T>(bytes: &'a [u8]) -> Result<T, bincode::Error> | ||
where | ||
T: serde::Deserialize<'a>, | ||
{ | ||
little_endian().deserialize(bytes) | ||
} | ||
|
||
/// This is the default settings for Options, | ||
/// but all explicitly spelled out, except for endianness. | ||
/// The following functions will add endianness. | ||
#[inline] | ||
fn options() -> impl Options { | ||
bincode::options() | ||
.with_fixint_encoding() | ||
.with_no_limit() | ||
.allow_trailing_bytes() | ||
} | ||
|
||
/// Adding the endian flag for big endian | ||
#[inline] | ||
fn big_endian() -> impl Options { | ||
options().with_big_endian() | ||
} | ||
|
||
/// Adding the endian flag for little endian | ||
#[inline] | ||
fn little_endian() -> impl Options { | ||
options().with_little_endian() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ mod block; | |
mod script; | ||
mod transaction; | ||
|
||
pub mod bincode; | ||
pub mod electrum_merkle; | ||
pub mod fees; | ||
|
||
|