Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add borsh binary serializer to i256 and u256 #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ members = [
[features]
llvm-intrinsics = ["ethnum-intrinsics"]
macros = [] # deprecated
borsh = ["dep:borsh"]

[dependencies]
ethnum-intrinsics = { version = "=1.2.0", path = "intrinsics", optional = true }
serde = { version = "1", default-features = false, optional = true }
borsh = { version = "1.2.1", default-features = false, optional = true }
58 changes: 58 additions & 0 deletions src/borsh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::{I256, U256};

impl borsh::BorshSerialize for I256 {
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
self.0.serialize(writer)
}
}

impl borsh::BorshSerialize for U256 {
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
self.0.serialize(writer)
}
}

impl borsh::BorshDeserialize for I256 {
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let value: [i128; 2] = borsh::BorshDeserialize::deserialize_reader(reader)?;
Ok(Self(value))
}
}

impl borsh::BorshDeserialize for U256 {
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let value: [u128; 2] = borsh::BorshDeserialize::deserialize_reader(reader)?;
Ok(Self(value))
}
}

#[cfg(test)]
mod borsh_tests {
use crate::{I256, U256};

#[test]
fn test_borsh_i256() {
let i = I256::MIN;
let buffer = borsh::to_vec(&i).expect("failed to serialize value");
let deser_i: I256 = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_i, i);

let i = I256::MAX;
let buffer = borsh::to_vec(&i).expect("failed to serialize value");
let deser_i: I256 = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_i, i);
}

#[test]
fn test_borsh_u256() {
let u = U256::MIN;
let buffer = borsh::to_vec(&u).expect("failed to serialize value");
let deser_u: U256 = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_u, u);

let u = U256::MAX;
let buffer = borsh::to_vec(&u).expect("failed to serialize value");
let deser_u: U256 = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_u, u);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod macros {
pub mod parse;
}

#[cfg(feature = "borsh")]
pub mod borsh;
mod error;
mod fmt;
mod int;
Expand Down