-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement partial Ord for EthAddress * add dummy non zero * remove warning * generate rust make task * add-generated * u256 tests * to_bytes * don't generate custom u256 * from felt pair outline * trait From, from_bytes_le/be and tests * code cleanup * change the From implementation to TryFrom and adjust encoding * Implement common traits * add NonZero constraints * remove comment * update readme * add const sizes * update ordering * add inner_mut * simplify generics * Revert "simplify generics" This reverts commit 2a75052. * fix: fix tests * fix: fmt and clippy --------- Co-authored-by: Mateusz <[email protected]> Co-authored-by: glihm <[email protected]>
- Loading branch information
1 parent
bed53f7
commit 60da94a
Showing
16 changed files
with
430 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
/abi | ||
/generated |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ mod abicov { | |
} | ||
|
||
mod simple_get_set; | ||
mod basic; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//! CairoSerde implementation for NonZero. | ||
//! | ||
//! NonZero serializes with zero ( hehe :) ) overhead as the inner value | ||
//! | ||
//! https://github.com/starkware-libs/cairo/blob/main/corelib/src/zeroable.cairo#L38 | ||
use crate::{CairoSerde, ContractAddress, Result, U256}; | ||
use starknet::core::types::FieldElement; | ||
|
||
#[derive(Debug, PartialEq, PartialOrd, Clone)] | ||
pub struct NonZero<T: Zeroable>(T); | ||
|
||
impl<T: Zeroable> NonZero<T> { | ||
pub fn new(value: T) -> Option<Self> { | ||
if value.is_zero() { | ||
None | ||
} else { | ||
Some(NonZero(value)) | ||
} | ||
} | ||
|
||
pub fn inner(&self) -> &T { | ||
&self.0 | ||
} | ||
|
||
pub fn inner_mut(&mut self) -> &mut T { | ||
&mut self.0 | ||
} | ||
|
||
pub fn into_inner(self) -> T { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<T, RT> CairoSerde for NonZero<T> | ||
where | ||
T: CairoSerde<RustType = RT>, | ||
T: Zeroable, | ||
RT: Zeroable, | ||
{ | ||
type RustType = NonZero<RT>; | ||
|
||
const SERIALIZED_SIZE: Option<usize> = T::SERIALIZED_SIZE; | ||
const DYNAMIC: bool = T::DYNAMIC; | ||
|
||
#[inline] | ||
fn cairo_serialized_size(rust: &Self::RustType) -> usize { | ||
T::cairo_serialized_size(&rust.0) | ||
} | ||
|
||
fn cairo_serialize(rust: &Self::RustType) -> Vec<FieldElement> { | ||
T::cairo_serialize(&rust.0) | ||
} | ||
|
||
fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result<Self::RustType> { | ||
NonZero::new(T::cairo_deserialize(felts, offset)?).ok_or(crate::Error::ZeroedNonZero) | ||
} | ||
} | ||
|
||
pub trait Zeroable { | ||
fn is_zero(&self) -> bool; | ||
} | ||
|
||
macro_rules! implement_nonzeroable_for_integer { | ||
($type:ty) => { | ||
impl Zeroable for $type { | ||
fn is_zero(&self) -> bool { | ||
*self == 0 as $type | ||
} | ||
} | ||
}; | ||
} | ||
|
||
implement_nonzeroable_for_integer!(u8); | ||
implement_nonzeroable_for_integer!(u16); | ||
implement_nonzeroable_for_integer!(u32); | ||
implement_nonzeroable_for_integer!(u64); | ||
implement_nonzeroable_for_integer!(u128); | ||
implement_nonzeroable_for_integer!(usize); | ||
implement_nonzeroable_for_integer!(i8); | ||
implement_nonzeroable_for_integer!(i16); | ||
implement_nonzeroable_for_integer!(i32); | ||
implement_nonzeroable_for_integer!(i64); | ||
implement_nonzeroable_for_integer!(i128); | ||
implement_nonzeroable_for_integer!(isize); | ||
|
||
impl Zeroable for U256 { | ||
fn is_zero(&self) -> bool { | ||
self.low.is_zero() && self.high.is_zero() | ||
} | ||
} | ||
|
||
impl Zeroable for FieldElement { | ||
fn is_zero(&self) -> bool { | ||
*self == FieldElement::ZERO | ||
} | ||
} | ||
|
||
impl Zeroable for ContractAddress { | ||
fn is_zero(&self) -> bool { | ||
self.0 == FieldElement::ZERO | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::Error; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_non_zero_cairo_serialize() { | ||
let non_zero = NonZero(1_u32); | ||
let felts = NonZero::<u32>::cairo_serialize(&non_zero); | ||
assert_eq!(felts.len(), 1); | ||
assert_eq!(felts[0], FieldElement::from(1_u32)); | ||
} | ||
|
||
#[test] | ||
fn test_non_zero_cairo_deserialize() { | ||
let felts = vec![FieldElement::from(1_u32)]; | ||
let non_zero = NonZero::<u32>::cairo_deserialize(&felts, 0).unwrap(); | ||
assert_eq!(non_zero, NonZero(1_u32)) | ||
} | ||
|
||
#[test] | ||
fn test_non_zero_cairo_deserialize_zero() { | ||
let felts = vec![FieldElement::ZERO, FieldElement::ZERO]; | ||
let non_zero = NonZero::<U256>::cairo_deserialize(&felts, 0); | ||
match non_zero { | ||
Err(Error::ZeroedNonZero) => (), | ||
_ => panic!("Expected ZeroedNonZero error"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_non_zero_const_size() { | ||
assert_eq!(NonZero::<u32>::SERIALIZED_SIZE, Some(1)); | ||
assert_eq!(NonZero::<U256>::SERIALIZED_SIZE, Some(2)); | ||
|
||
let is_dynamic = NonZero::<i8>::DYNAMIC; | ||
assert!(!is_dynamic); | ||
} | ||
} |
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
Oops, something went wrong.