-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Cairo encoding for sum types (#48)
* Refactor CombineEncoders to accept type level list of encoders * Add HList! macro to simplify encoder list * Add variant encoder and deref encoder * Implement Option encoder * Reorganize sum types * Add sum macro * Fix lint
- Loading branch information
1 parent
2260a5e
commit 16ff7ea
Showing
17 changed files
with
203 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
relayer/crates/cairo-encoding-components/src/impls/encode_mut/option.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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; | ||
use crate::types::nat::Z; | ||
use crate::{HList, Sum}; | ||
|
||
pub struct EncodeOption; | ||
|
||
pub type OptionEncoder = SumEncoders<Z, HList![EncodeWithContext, EncodeWithContext]>; | ||
|
||
impl<Encoding, Strategy, Value> MutEncoder<Encoding, Strategy, Option<Value>> for EncodeOption | ||
where | ||
Encoding: HasEncodeBufferType + HasErrorType, | ||
OptionEncoder: for<'a> MutEncoder<Encoding, Strategy, Sum![&'a Value, ()]>, | ||
{ | ||
fn encode_mut( | ||
encoding: &Encoding, | ||
value: &Option<Value>, | ||
buffer: &mut Encoding::EncodeBuffer, | ||
) -> Result<(), Encoding::Error> { | ||
let sum = match value { | ||
Some(value) => Either::Left(value), | ||
None => Either::Right(Either::Left(())), | ||
}; | ||
|
||
OptionEncoder::encode_mut(encoding, &sum, buffer)?; | ||
|
||
Ok(()) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
relayer/crates/cairo-encoding-components/src/impls/encode_mut/reference.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::ops::Deref; | ||
|
||
use crate::traits::encode_mut::{CanEncodeMut, MutEncoder}; | ||
|
||
pub struct EncodeDeref; | ||
|
||
impl<Encoding, Strategy, Value> MutEncoder<Encoding, Strategy, Value> for EncodeDeref | ||
where | ||
Encoding: CanEncodeMut<Strategy, Value::Target>, | ||
Value: Deref, | ||
Value::Target: Sized, | ||
{ | ||
fn encode_mut( | ||
encoding: &Encoding, | ||
value: &Value, | ||
buffer: &mut Encoding::EncodeBuffer, | ||
) -> Result<(), Encoding::Error> { | ||
encoding.encode_mut(value.deref(), buffer) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
relayer/crates/cairo-encoding-components/src/impls/encode_mut/variant.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::marker::PhantomData; | ||
|
||
use cgp_core::error::HasErrorType; | ||
|
||
use crate::traits::encode_mut::{HasEncodeBufferType, MutEncoder}; | ||
use crate::types::either::{Either, Void}; | ||
use crate::types::nat::{Nat, S}; | ||
|
||
pub struct SumEncoders<N, Encoders>(pub PhantomData<(N, Encoders)>); | ||
|
||
impl<Encoding, Strategy, ValueA, ValueB, N, Encoder, InEncoders> | ||
MutEncoder<Encoding, Strategy, Either<ValueA, ValueB>> for SumEncoders<N, (Encoder, InEncoders)> | ||
where | ||
Encoding: HasEncodeBufferType + HasErrorType, | ||
Encoder: MutEncoder<Encoding, Strategy, ValueA>, | ||
N: Nat, | ||
SumEncoders<S<N>, InEncoders>: MutEncoder<Encoding, Strategy, ValueB>, | ||
{ | ||
fn encode_mut( | ||
encoding: &Encoding, | ||
value: &Either<ValueA, ValueB>, | ||
buffer: &mut <Encoding as HasEncodeBufferType>::EncodeBuffer, | ||
) -> Result<(), <Encoding as HasErrorType>::Error> { | ||
match value { | ||
Either::Left(value) => Encoder::encode_mut(encoding, value, buffer), | ||
Either::Right(value) => { | ||
<SumEncoders<S<N>, InEncoders>>::encode_mut(encoding, value, buffer) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<Encoding, Strategy, N> MutEncoder<Encoding, Strategy, Void> for SumEncoders<N, ()> | ||
where | ||
Encoding: HasEncodeBufferType + HasErrorType, | ||
{ | ||
fn encode_mut( | ||
_encoding: &Encoding, | ||
_value: &Void, | ||
_buffer: &mut Encoding::EncodeBuffer, | ||
) -> Result<(), Encoding::Error> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,7 @@ | |
|
||
pub mod components; | ||
pub mod impls; | ||
pub mod macros; | ||
pub mod strategy; | ||
pub mod traits; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#[macro_export] | ||
macro_rules! HList { | ||
( $(,)? ) => { | ||
() | ||
}; | ||
( $e:ty ) => { | ||
( $e, () ) | ||
}; | ||
( $e:ty, $($tail:tt)* ) => { | ||
( $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)* ) | ||
> | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub enum Either<A, B> { | ||
Left(A), | ||
Right(B), | ||
} | ||
|
||
pub enum Void {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod either; | ||
pub mod nat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use core::marker::PhantomData; | ||
|
||
pub struct Z; | ||
|
||
pub struct S<N>(pub PhantomData<N>); | ||
|
||
pub trait Nat { | ||
const N: usize; | ||
} | ||
|
||
impl Nat for Z { | ||
const N: usize = 0; | ||
} | ||
|
||
impl<N: Nat> Nat for S<N> { | ||
const N: usize = N::N + 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters