Skip to content

Commit

Permalink
Implement trait for arrays and tuples (#20)
Browse files Browse the repository at this point in the history
* Implement trait for arrays and tuples

* Nit
  • Loading branch information
maxzaver authored Sep 22, 2019
1 parent 7939504 commit 59c912f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 48 deletions.
12 changes: 6 additions & 6 deletions borsh-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion borsh-rs/borsh-derive-internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive-internal"
version = "0.2.4"
version = "0.2.5"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions borsh-rs/borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive"
version = "0.2.4"
version = "0.2.5"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -16,6 +16,6 @@ Binary Object Representation Serializer for Hashing
proc-macro = true

[dependencies]
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.2.4"}
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.2.5"}
syn = {version = "0.15.34", features = ["full", "fold"] }

4 changes: 2 additions & 2 deletions borsh-rs/borsh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh"
version = "0.2.4"
version = "0.2.5"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -13,7 +13,7 @@ Binary Object Representation Serializer for Hashing
"""

[dependencies]
borsh-derive = { path = "../borsh-derive", version = "0.2.4" }
borsh-derive = { path = "../borsh-derive", version = "0.2.5" }

[features]
default = ["std"]
Expand Down
87 changes: 64 additions & 23 deletions borsh-rs/borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub trait BorshDeserialize: Sized {
let mut c = Cursor::new(v);
let result = Self::deserialize(&mut c)?;
if c.position() != v.len() as u64 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ));
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
ERROR_NOT_ALL_BYTES_READ,
));
}
Ok(result)
}
Expand Down Expand Up @@ -63,7 +66,10 @@ macro_rules! impl_for_float {
reader.read_exact(&mut data)?;
let res = $type::from_bits($int_type::from_le_bytes(data));
if res.is_nan() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "For portability reasons we do not allow to deserialize NaNs."))
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"For portability reasons we do not allow to deserialize NaNs.",
));
}
Ok(res)
}
Expand Down Expand Up @@ -184,10 +190,8 @@ impl BorshDeserialize for std::net::SocketAddr {
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
let kind = u8::deserialize(reader)?;
match kind {
0 => std::net::SocketAddrV4::deserialize(reader)
.map(std::net::SocketAddr::V4),
1 => std::net::SocketAddrV6::deserialize(reader)
.map(std::net::SocketAddr::V6),
0 => std::net::SocketAddrV4::deserialize(reader).map(std::net::SocketAddr::V4),
1 => std::net::SocketAddrV6::deserialize(reader).map(std::net::SocketAddr::V6),
value => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("Invalid SocketAddr variant: {}", value),
Expand Down Expand Up @@ -236,23 +240,6 @@ impl BorshDeserialize for std::net::Ipv6Addr {
}
}

macro_rules! impl_for_fixed_len_array {
($len: expr) => {
impl BorshDeserialize for [u8; $len] {
#[inline]
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
let mut data = [0u8; $len];
reader.read(&mut data)?;
Ok(data)
}
}
};
}

impl_for_fixed_len_array!(32);
impl_for_fixed_len_array!(64);
impl_for_fixed_len_array!(65);

impl BorshDeserialize for Box<[u8]> {
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
let len = u32::deserialize(reader)?;
Expand All @@ -261,3 +248,57 @@ impl BorshDeserialize for Box<[u8]> {
Ok(res.into_boxed_slice())
}
}

macro_rules! impl_arrays {
($($len:expr)+) => {
$(
impl<T> BorshDeserialize for [T; $len]
where T: BorshDeserialize + Default + Copy
{
#[inline]
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
let mut result = [T::default(); $len];
for i in 0..$len {
result[i] = T::deserialize(reader)?;
}
Ok(result)
}
}
)+
};
}

impl_arrays!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 32 64 65);

macro_rules! impl_tuple {
($($name:ident)+) => {
impl<$($name),+> BorshDeserialize for ($($name),+)
where $($name: BorshDeserialize,)+
{
#[inline]
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
Ok(($($name::deserialize(reader)?,)+))
}
}
};
}

impl_tuple!(T0 T1);
impl_tuple!(T0 T1 T2);
impl_tuple!(T0 T1 T2 T3);
impl_tuple!(T0 T1 T2 T3 T4);
impl_tuple!(T0 T1 T2 T3 T4 T5);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18);
impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19);
67 changes: 53 additions & 14 deletions borsh-rs/borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,64 @@ impl BorshSerialize for std::net::Ipv6Addr {
}
}

macro_rules! impl_for_fixed_len_array {
($len: expr) => {
impl BorshSerialize for [u8; $len] {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
writer.write(self).map(|_| ())
impl BorshSerialize for Box<[u8]> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
(self.len() as u32).serialize(writer)?;
writer.write(self).map(|_| ())
}
}

macro_rules! impl_arrays {
($($len:expr)+) => {
$(
impl<T> BorshSerialize for [T; $len]
where T: BorshSerialize
{
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
for el in self.iter() {
el.serialize(writer)?;
}
Ok(())
}
}
)+
};
}

impl_for_fixed_len_array!(32);
impl_for_fixed_len_array!(64);
impl_for_fixed_len_array!(65);
impl_arrays!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 32 64 65);

impl BorshSerialize for Box<[u8]> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
(self.len() as u32).serialize(writer)?;
writer.write(self).map(|_| ())
}
macro_rules! impl_tuple {
($($idx:tt $name:ident)+) => {
impl<$($name),+> BorshSerialize for ($($name),+)
where $($name: BorshSerialize,)+
{
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
$(self.$idx.serialize(writer)?;)+
Ok(())
}
}
};
}

impl_tuple!(0 T0 1 T1);
impl_tuple!(0 T0 1 T1 2 T2);
impl_tuple!(0 T0 1 T1 2 T2 3 T3);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18);
impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19);

3 changes: 3 additions & 0 deletions borsh-rs/borsh/tests/test_simple_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct A {
b: B,
y: f32,
z: String,
t: (String, u64),
m: HashMap<String, String>,
s: HashSet<u64>,
v: Vec<String>,
Expand Down Expand Up @@ -59,6 +60,7 @@ fn test_simple_struct() {
b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) },
y: 4.0,
z: "123".to_string(),
t: ("Hello".to_string(), 10),
m: map.clone(),
s: set.clone(),
v: vec!["qwe".to_string(), "zxc".to_string()],
Expand All @@ -74,6 +76,7 @@ fn test_simple_struct() {
b: B { x: 2, y: 3, c: C::C5(D { x: 1 }) },
y: 4.0,
z: a.z,
t: ("Hello".to_string(), 10),
m: map.clone(),
s: set.clone(),
v: a.v,
Expand Down

0 comments on commit 59c912f

Please sign in to comment.