Skip to content

Commit

Permalink
Use serdect
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 11, 2022
1 parent 50960c0 commit 1d94952
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
16 changes: 11 additions & 5 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ der = { version = "=0.6.0-pre.3", optional = true, default-features = false }
generic-array = { version = "0.14", optional = true }
rand_core = { version = "0.6", optional = true }
rlp = { version = "0.5", optional = true, default-features = false }
serde_ = { version = "1", package = "serde", optional = true, default-features = false }
serde-big-array = { version = "0.4", optional = true }
serdect = { version = "0.1", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[dev-dependencies]
Expand All @@ -41,7 +40,7 @@ rand_chacha = "0.3"
default = ["rand"]
alloc = []
rand = ["rand_core/std"]
serde = ["serde_", "serde-big-array"]
serde = ["serdect"]

[package.metadata.docs.rs]
all-features = true
Expand Down
9 changes: 3 additions & 6 deletions src/checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Provides intentionally-checked arithmetic on `T`.
///
Expand Down Expand Up @@ -68,11 +68,8 @@ impl<'de, T: Default + Deserialize<'de>> Deserialize<'de> for Checked<T> {
D: Deserializer<'de>,
{
let value = Option::<T>::deserialize(deserializer)?;

Ok(Self(match value {
Some(value) => CtOption::new(value, Choice::from(1)),
None => CtOption::new(T::default(), Choice::from(0)),
}))
let choice = Choice::from(value.is_some() as u8);
Ok(Self(CtOption::new(value.unwrap_or_default(), choice)))
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@
#[cfg(all(feature = "alloc", test))]
extern crate alloc;

#[cfg(feature = "serde")]
extern crate serde_ as serde;

#[macro_use]
mod nlimbs;

Expand Down
2 changes: 1 addition & 1 deletion src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use core::fmt;
use subtle::{Choice, ConditionallySelectable};

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("this crate builds on 32-bit and 64-bit platforms only");
Expand Down
2 changes: 1 addition & 1 deletion src/non_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
};

#[cfg(feature = "serde")]
use serde::{
use serdect::serde::{
de::{Error, Unexpected},
Deserialize, Deserializer, Serialize, Serializer,
};
Expand Down
24 changes: 14 additions & 10 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ use core::fmt;
use subtle::{Choice, ConditionallySelectable};

#[cfg(feature = "serde")]
use ::{
serde::{Deserialize, Deserializer, Serialize, Serializer},
serde_big_array::BigArray,
};
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "zeroize")]
use zeroize::DefaultIsZeroes;
Expand Down Expand Up @@ -174,25 +171,32 @@ impl<const LIMBS: usize> fmt::UpperHex for UInt<LIMBS> {

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de, const LIMBS: usize> Deserialize<'de> for UInt<LIMBS> {
impl<'de, const LIMBS: usize> Deserialize<'de> for UInt<LIMBS>
where
UInt<LIMBS>: Encoding,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Self {
limbs: BigArray::deserialize(deserializer)?,
})
let mut buffer = Self::ZERO.to_le_bytes();
serdect::array::deserialize_hex_or_bin(buffer.as_mut(), deserializer)?;

Ok(Self::from_le_bytes(buffer))
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de, const LIMBS: usize> Serialize for UInt<LIMBS> {
impl<'de, const LIMBS: usize> Serialize for UInt<LIMBS>
where
UInt<LIMBS>: Encoding,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
BigArray::serialize(&self.limbs, serializer)
serdect::array::serialize_hex_lower_or_bin(&Encoding::to_le_bytes(self), serializer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/uint/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! impl_uint_aliases {
}

fn from_le_bytes(bytes: Self::Repr) -> Self {
Self::from_be_slice(&bytes)
Self::from_le_slice(&bytes)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Provides intentionally-wrapped arithmetic on `T`.
///
Expand Down

0 comments on commit 1d94952

Please sign in to comment.