diff --git a/Cargo.lock b/Cargo.lock index a3d7079..3d700db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,6 +158,7 @@ dependencies = [ "ouroboros", "proptest", "proptest-derive", + "serde", "sha2", ] @@ -364,6 +365,26 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "serde" +version = "1.0.198" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.198" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.55", +] + [[package]] name = "sha2" version = "0.10.8" diff --git a/Cargo.toml b/Cargo.toml index 1ab5721..f15edbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ license = "MIT OR Apache-2.0" [features] default = ["std"] std = [] +serde = ["dep:serde"] [profile.test] opt-level = 3 @@ -18,9 +19,10 @@ overflow-checks = true digest = "0.10" bumpalo = "3" ouroboros = "0.18" +serde = { version = "1", default-features = false, features = ["derive", "alloc"], optional = true } [dev-dependencies] sha2 = "0.10" proptest-derive = { version = "0.4" } -proptest = { version = "1"} +proptest = { version = "1" } diff --git a/src/lib.rs b/src/lib.rs index 2d458ec..10bd413 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ pub use transaction::{ Entry, OccupiedEntry, Transaction, VacantEntry, VacantEntryEmptyTrie, }; +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct KeyHash(pub [u32; 8]); @@ -72,6 +73,7 @@ impl PortableHash for KeyHash { } } +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct NodeHash { pub bytes: [u8; 32], diff --git a/src/stored/merkle.rs b/src/stored/merkle.rs index dccf683..f49795b 100644 --- a/src/stored/merkle.rs +++ b/src/stored/merkle.rs @@ -16,6 +16,8 @@ type Result = core::result::Result; /// A snapshot of the merkle trie /// /// Contains visited nodes and unvisited nodes + +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct Snapshot { /// The last branch is the root of the trie if it exists. diff --git a/src/transaction/nodes.rs b/src/transaction/nodes.rs index 670aa30..bc56327 100644 --- a/src/transaction/nodes.rs +++ b/src/transaction/nodes.rs @@ -3,6 +3,7 @@ use core::{fmt, iter, mem}; use crate::{hash::PortableHasher, stored, KeyHash, NodeHash, PortableHash, PortableUpdate}; +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)] pub enum TrieRoot { #[default] @@ -38,6 +39,7 @@ impl From> for Option { } /// A unmodified Node +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub enum Node { Branch(B), @@ -115,6 +117,7 @@ impl<'s, V> StoredLeafRef<'s, V> { } } +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct BranchMask { /// The index of the discriminant bit in the 256 bit hash key. @@ -240,6 +243,7 @@ mod tests { } } +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Branch { pub left: NR, @@ -562,6 +566,7 @@ impl Branch> { } } +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Leaf { pub key_hash: KeyHash,