Skip to content

Commit

Permalink
wip. impl digest_newtype!() macro
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-da committed Oct 1, 2024
1 parent 7c01ae2 commit 758616d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 107 deletions.
109 changes: 2 additions & 107 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,113 +96,8 @@ const MINER_CHANNEL_CAPACITY: usize = 3;
const RPC_CHANNEL_CAPACITY: usize = 1000;
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(
Copy,
Debug,
Clone,
Default,
Hash,
GetSize,
PartialEq,
Eq,
Serialize,
Deserialize,
BFieldCodec,
Arbitrary,
)]
pub struct SenderRandomness(Digest);
impl std::ops::Deref for SenderRandomness {
type Target = Digest;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::fmt::Display for SenderRandomness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl From<Digest> for SenderRandomness {
fn from(d: Digest) -> Self {
Self(d)
}
}
impl From<SenderRandomness> for Digest {
fn from(sr: SenderRandomness) -> Self {
*sr
}
}
impl Distribution<SenderRandomness> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> SenderRandomness {
SenderRandomness(rng.gen())
}
}

impl FromStr for SenderRandomness {
type Err = TryFromDigestError;

fn from_str(string: &str) -> Result<Self, Self::Err> {
Ok(Digest::from_str(string)?.into())
}
}

impl TryFrom<&[BFieldElement]> for SenderRandomness {
type Error = TryFromDigestError;

fn try_from(value: &[BFieldElement]) -> Result<Self, Self::Error> {
Ok(Digest::try_from(value)?.into())
}
}

impl TryFrom<Vec<BFieldElement>> for SenderRandomness {
type Error = TryFromDigestError;

fn try_from(value: Vec<BFieldElement>) -> Result<Self, Self::Error> {
Ok(Digest::try_from(value)?.into())
}
}

impl From<SenderRandomness> for Vec<BFieldElement> {
fn from(val: SenderRandomness) -> Self {
val.0.into()
}
}

impl From<SenderRandomness> for [u8; Digest::BYTES] {
fn from(item: SenderRandomness) -> Self {
item.0.into()
}
}

impl TryFrom<[u8; Digest::BYTES]> for SenderRandomness {
type Error = TryFromDigestError;

fn try_from(item: [u8; Digest::BYTES]) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(item)?))
}
}

impl TryFrom<&[u8]> for SenderRandomness {
type Error = TryFromDigestError;

fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(slice)?))
}
}

impl TryFrom<BigUint> for SenderRandomness {
type Error = TryFromDigestError;

fn try_from(value: BigUint) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(value)?))
}
}

impl From<SenderRandomness> for BigUint {
fn from(digest: SenderRandomness) -> Self {
digest.0.into()
}
}
// a Digest new-type to represent sender-randomness, for type safety.
crate::macros::digest_newtype!(SenderRandomness);

pub async fn initialize(cli_args: cli_args::Args) -> Result<()> {
// Get data directory (wallet, block database), create one if none exists
Expand Down
114 changes: 114 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,125 @@ macro_rules! duration_async_debug {
};
}

macro_rules! digest_newtype {
($target: ident) => {
#[derive(
Copy,
Debug,
Clone,
Default,
Hash,
GetSize,
PartialEq,
Eq,
Serialize,
Deserialize,
BFieldCodec,
Arbitrary,
)]
pub struct $target(Digest);
impl std::ops::Deref for $target {
type Target = Digest;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::fmt::Display for $target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl From<Digest> for $target {
fn from(d: Digest) -> Self {
Self(d)
}
}
impl From<$target> for Digest {
fn from(sr: $target) -> Self {
*sr
}
}
impl Distribution<$target> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $target {
$target(rng.gen())
}
}

impl FromStr for $target {
type Err = TryFromDigestError;

fn from_str(string: &str) -> Result<Self, Self::Err> {
Ok(Digest::from_str(string)?.into())
}
}

impl TryFrom<&[BFieldElement]> for $target {
type Error = TryFromDigestError;

fn try_from(value: &[BFieldElement]) -> Result<Self, Self::Error> {
Ok(Digest::try_from(value)?.into())
}
}

impl TryFrom<Vec<BFieldElement>> for $target {
type Error = TryFromDigestError;

fn try_from(value: Vec<BFieldElement>) -> Result<Self, Self::Error> {
Ok(Digest::try_from(value)?.into())
}
}

impl From<$target> for Vec<BFieldElement> {
fn from(val: $target) -> Self {
val.0.into()
}
}

impl From<$target> for [u8; Digest::BYTES] {
fn from(item: $target) -> Self {
item.0.into()
}
}

impl TryFrom<[u8; Digest::BYTES]> for $target {
type Error = TryFromDigestError;

fn try_from(item: [u8; Digest::BYTES]) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(item)?))
}
}

impl TryFrom<&[u8]> for $target {
type Error = TryFromDigestError;

fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(slice)?))
}
}

impl TryFrom<BigUint> for $target {
type Error = TryFromDigestError;

fn try_from(value: BigUint) -> Result<Self, Self::Error> {
Ok(Self(Digest::try_from(value)?))
}
}

impl From<$target> for BigUint {
fn from(digest: $target) -> Self {
digest.0.into()
}
}
};
}

// These allow the macros to be used as
// use crate::macros::xxxxx;
//
// see: https://stackoverflow.com/a/67140319/10087197

#[allow(unused_imports)]
pub(crate) use digest_newtype;
#[allow(unused_imports)]
pub(crate) use duration;
#[allow(unused_imports)]
Expand Down

0 comments on commit 758616d

Please sign in to comment.