Skip to content

Commit 0ebc50b

Browse files
authored
Implement size_of for Felt (#123)
1 parent 67468ff commit 0ebc50b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/starknet-types-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ lambdaworks-math = { version = "0.10.0", default-features = false }
1515
num-traits = { version = "0.2", default-features = false }
1616
num-bigint = { version = "0.4", default-features = false }
1717
num-integer = { version = "0.1", default-features = false }
18+
size-of = { version = "0.1.5", default-features = false }
1819

1920
# Optional
2021
arbitrary = { version = "1.3", optional = true }

crates/starknet-types-core/src/felt/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use core::str::FromStr;
88
use num_bigint::{BigInt, BigUint, Sign};
99
use num_integer::Integer;
1010
use num_traits::{One, Zero};
11+
use size_of::SizeOf;
1112
#[cfg(any(feature = "prime-bigint", test))]
1213
use {lazy_static::lazy_static, num_traits::Num};
1314

@@ -44,6 +45,10 @@ use arbitrary::{self, Arbitrary, Unstructured};
4445
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4546
pub struct Felt(pub(crate) FieldElement<Stark252PrimeField>);
4647

48+
impl SizeOf for Felt {
49+
fn size_of_children(&self, _context: &mut size_of::Context) {}
50+
}
51+
4752
/// A non-zero [Felt].
4853
#[repr(transparent)]
4954
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -1072,6 +1077,7 @@ mod test {
10721077
use core::ops::Shl;
10731078
use proptest::prelude::*;
10741079
use regex::Regex;
1080+
use size_of::TotalSize;
10751081

10761082
#[test]
10771083
fn test_debug_format() {
@@ -1856,4 +1862,15 @@ mod test {
18561862
felt.zeroize();
18571863
assert_eq!(felt, Felt::ZERO);
18581864
}
1865+
1866+
#[test]
1867+
fn felt_size_of() {
1868+
assert_eq!(Felt::ZERO.size_of(), TotalSize::total(32));
1869+
assert_eq!(Felt::ONE.size_of(), TotalSize::total(32));
1870+
assert_eq!(
1871+
Felt(FieldElement::from(1600000000)).size_of(),
1872+
TotalSize::total(32)
1873+
);
1874+
assert_eq!(Felt::MAX.size_of(), TotalSize::total(32));
1875+
}
18591876
}

0 commit comments

Comments
 (0)