-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for signed integers #2161
Changes from 1 commit
d5e28cb
06c5565
f5f10c6
0ad393c
5947778
0404110
3bb9810
c6ac6fc
40dc003
4d0f5f3
aa2ea32
96d5a03
8ca937e
23e9b55
936ba3c
2adfda3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use core::convert::TryInto; | ||
use starknet::core::types::Felt; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct PrimitiveFromFeltError; | ||
|
||
impl core::fmt::Display for PrimitiveFromFeltError { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
write!(f, "Failed to convert `Felt` into primitive type") | ||
} | ||
} | ||
|
||
const MINUS_TWO_BYTES_REPR: [u8; 32] = [ | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 16, 0, 0, 0, 0, 0, 0, 8, | ||
]; | ||
|
||
pub trait FromFelt: Sized { | ||
fn try_from_felt(value: Felt) -> Result<Self, PrimitiveFromFeltError>; | ||
} | ||
|
||
macro_rules! impl_from_felt { | ||
($into:ty) => { | ||
impl FromFelt for $into { | ||
fn try_from_felt(value: Felt) -> Result<Self, PrimitiveFromFeltError> { | ||
let size_of_type = core::mem::size_of::<$into>(); | ||
let bytes_le = value.to_bytes_le(); | ||
|
||
if bytes_le[size_of_type..].iter().all(|&v| v == 0) | ||
&& bytes_le[size_of_type - 1] <= 0b01111111 | ||
{ | ||
Ok(<$into>::from_le_bytes(bytes_le[..size_of_type].try_into().unwrap())) | ||
} else if bytes_le[size_of_type..] == MINUS_TWO_BYTES_REPR[size_of_type..] | ||
&& bytes_le[size_of_type - 1] >= 0b10000000 | ||
{ | ||
let offsetted_value = | ||
<$into>::from_le_bytes(bytes_le[..size_of_type].try_into().unwrap()); | ||
|
||
offsetted_value.checked_sub(1).ok_or(PrimitiveFromFeltError) | ||
} else if bytes_le[24..] == [17, 0, 0, 0, 0, 0, 0, 8] { | ||
return Ok(-1); | ||
} else { | ||
Err(PrimitiveFromFeltError) | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from_felt!(i8); | ||
impl_from_felt!(i16); | ||
impl_from_felt!(i32); | ||
impl_from_felt!(i64); | ||
impl_from_felt!(i128); | ||
|
||
pub fn try_from_felt<T: FromFelt>(value: Felt) -> Result<T, PrimitiveFromFeltError> { | ||
T::try_from_felt(value) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,11 +61,11 @@ mod records { | |
Record { | ||
record_id, | ||
depth: Depth::Zero, | ||
type_i8: record_idx.into(), | ||
type_i16: record_idx.into(), | ||
type_i32: record_idx.into(), | ||
type_i64: record_idx.into(), | ||
type_i128: record_idx.into(), | ||
type_i8: type_felt.try_into().unwrap(), | ||
type_i16: type_felt.try_into().unwrap(), | ||
type_i32: type_felt.try_into().unwrap(), | ||
type_i64: type_felt.try_into().unwrap(), | ||
type_i128: type_felt.try_into().unwrap(), | ||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider safer error handling when converting types. The current implementation uses - type_i8: type_felt.try_into().unwrap(),
+ type_i8: type_felt.try_into().unwrap_or_default(), Repeat similar changes for
|
||
type_u8: record_idx.into(), | ||
type_u16: record_idx.into(), | ||
type_u32: record_idx.into(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job on that! Would you mind please adding a comment for the reason having this file + a link to the PR, to ensure we can track it easily and remove the code once merged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay