Skip to content

Commit

Permalink
Add sum macro
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen committed Aug 12, 2024
1 parent ed119a9 commit 767463f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use cgp_core::error::HasErrorType;
use crate::impls::encode_mut::variant::SumEncoders;
use crate::impls::encode_mut::with_context::EncodeWithContext;
use crate::traits::encode_mut::{HasEncodeBufferType, MutEncoder};
use crate::types::either::{Either, Void};
use crate::types::either::Either;
use crate::types::nat::Z;
use crate::HList;
use crate::{HList, Sum};

pub struct EncodeOption;

Expand All @@ -14,7 +14,7 @@ pub type OptionEncoder = SumEncoders<Z, HList![EncodeWithContext, EncodeWithCont
impl<Encoding, Strategy, Value> MutEncoder<Encoding, Strategy, Option<Value>> for EncodeOption
where
Encoding: HasEncodeBufferType + HasErrorType,
OptionEncoder: for<'a> MutEncoder<Encoding, Strategy, Either<&'a Value, Either<(), Void>>>,
OptionEncoder: for<'a> MutEncoder<Encoding, Strategy, Sum![&'a Value, ()]>,
{
fn encode_mut(
encoding: &Encoding,
Expand Down
19 changes: 19 additions & 0 deletions relayer/crates/cairo-encoding-components/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ macro_rules! HList {
( $e, $crate::HList!( $($tail)* ) )
};
}

#[macro_export]
macro_rules! Sum {
( $(,)? ) => {
crate::types::either::Void
};
( $e:ty ) => {
crate::types::either::Either<
$e,
crate::types::either::Void
>
};
( $e:ty, $($tail:tt)* ) => {
crate::types::either::Either<
$e,
$crate::Sum!( $($tail)* )
>
};
}

0 comments on commit 767463f

Please sign in to comment.