Skip to content

Commit

Permalink
Add serialization for empty tuples (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush authored Dec 29, 2020
1 parent 3856133 commit 6a67c35
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## Pending

The main features of this release are:
Expand Down Expand Up @@ -59,6 +58,7 @@ The main features of this release are:
- #144 (ark-poly) Add serialization for polynomials and evaluations
- #149 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `String`.
- #153 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `Rc<T>`.
- #158 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `()`.

### Bug fixes
- #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`.
Expand Down
43 changes: 23 additions & 20 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ pub const fn buffer_byte_size(modulus_bits: usize) -> usize {

// Implement Serialization for tuples
macro_rules! impl_tuple {
($( $ty: ident : $no: tt, )+) => {
impl<$($ty, )+> CanonicalSerialize for ($($ty,)+) where
$($ty: CanonicalSerialize,)+
($( $ty: ident : $no: tt, )*) => {
impl<$($ty, )*> CanonicalSerialize for ($($ty,)*) where
$($ty: CanonicalSerialize,)*
{
#[inline]
fn serialize<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize(&mut writer)?;)*
fn serialize<W: Write>(&self, mut _writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize(&mut _writer)?;)*
Ok(())
}

Expand All @@ -370,14 +370,14 @@ macro_rules! impl_tuple {
}

#[inline]
fn serialize_uncompressed<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize_uncompressed(&mut writer)?;)*
fn serialize_uncompressed<W: Write>(&self, mut _writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize_uncompressed(&mut _writer)?;)*
Ok(())
}

#[inline]
fn serialize_unchecked<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize_unchecked(&mut writer)?;)*
fn serialize_unchecked<W: Write>(&self, mut _writer: W) -> Result<(), SerializationError> {
$(self.$no.serialize_unchecked(&mut _writer)?;)*
Ok(())
}

Expand All @@ -389,33 +389,34 @@ macro_rules! impl_tuple {
}
}

impl<$($ty, )+> CanonicalDeserialize for ($($ty,)+) where
$($ty: CanonicalDeserialize,)+
impl<$($ty, )*> CanonicalDeserialize for ($($ty,)*) where
$($ty: CanonicalDeserialize,)*
{
#[inline]
fn deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
fn deserialize<R: Read>(mut _reader: R) -> Result<Self, SerializationError> {
Ok(($(
$ty::deserialize(&mut reader)?,
)+))
$ty::deserialize(&mut _reader)?,
)*))
}

#[inline]
fn deserialize_uncompressed<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
fn deserialize_uncompressed<R: Read>(mut _reader: R) -> Result<Self, SerializationError> {
Ok(($(
$ty::deserialize_uncompressed(&mut reader)?,
)+))
$ty::deserialize_uncompressed(&mut _reader)?,
)*))
}

#[inline]
fn deserialize_unchecked<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
fn deserialize_unchecked<R: Read>(mut _reader: R) -> Result<Self, SerializationError> {
Ok(($(
$ty::deserialize_unchecked(&mut reader)?,
)+))
$ty::deserialize_unchecked(&mut _reader)?,
)*))
}
}
}
}

impl_tuple!();
impl_tuple!(A:0, B:1,);
impl_tuple!(A:0, B:1, C:2,);
impl_tuple!(A:0, B:1, C:2, D:3,);
Expand Down Expand Up @@ -967,6 +968,8 @@ mod test {

#[test]
fn test_tuple() {
test_serialize(());
test_serialize((123u64, Dummy));
test_serialize((123u64, 234u32, Dummy));
}

Expand Down

0 comments on commit 6a67c35

Please sign in to comment.