From e640595bbfedc5dea335c1bf886dd824a2f367cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s?= Date: Wed, 26 Apr 2023 17:40:06 -0300 Subject: [PATCH 1/4] Add `Program::iter_identifiers` --- src/types/program.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/types/program.rs b/src/types/program.rs index 3683bb3cfe..3955276af2 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -124,6 +124,10 @@ impl Program { pub fn get_identifier(&self, id: &str) -> Option<&Identifier> { self.shared_program_data.identifiers.get(id) } + + pub fn iter_identifiers(&self) -> impl Iterator { + self.shared_program_data.identifiers.iter() + } } impl Default for Program { From 404b7cc0b6fea985c07d0d41071dc88afb430a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s?= Date: Wed, 26 Apr 2023 17:49:43 -0300 Subject: [PATCH 2/4] Add unit test --- src/types/program.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/types/program.rs b/src/types/program.rs index 3955276af2..0ef9e1d559 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -433,6 +433,70 @@ mod tests { ); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn iter_identifiers() { + let reference_manager = ReferenceManager { + references: Vec::new(), + }; + + let builtins: Vec = Vec::new(); + + let data: Vec = vec![ + mayberelocatable!(5189976364521848832), + mayberelocatable!(1000), + mayberelocatable!(5189976364521848832), + mayberelocatable!(2000), + mayberelocatable!(5201798304953696256), + mayberelocatable!(2345108766317314046), + ]; + + let mut identifiers: HashMap = HashMap::new(); + + identifiers.insert( + String::from("__main__.main"), + Identifier { + pc: Some(0), + type_: Some(String::from("function")), + value: None, + full_name: None, + members: None, + cairo_type: None, + }, + ); + + identifiers.insert( + String::from("__main__.main.SIZEOF_LOCALS"), + Identifier { + pc: None, + type_: Some(String::from("const")), + value: Some(Felt252::zero()), + full_name: None, + members: None, + cairo_type: None, + }, + ); + + let program = Program::new( + builtins, + data, + None, + HashMap::new(), + reference_manager, + identifiers.clone(), + Vec::new(), + None, + ) + .unwrap(); + + let collected_identifiers: HashMap<_, _> = program + .iter_identifiers() + .map(|(cairo_type, identifier)| (cairo_type.clone(), identifier.clone())) + .collect(); + + assert_eq!(collected_identifiers, identifiers); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn new_program_with_invalid_identifiers() { From 4208fa333f97c62d381d7c7d800cf00a69b3d300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s?= Date: Wed, 26 Apr 2023 17:53:09 -0300 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e0f90c404..b40e33f158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,8 @@ Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs segments.write_arg(ids.blake2s_ptr_end, padding) %} +* Add `Program::iter_identifiers(&self) -> Iterator` to get an iterator over the program's identifiers [#1079](https://github.com/lambdaclass/cairo-rs/pull/1079) + * Implement hint on `assert_le_felt` for versions 0.6.0 and 0.8.2 [#1047](https://github.com/lambdaclass/cairo-rs/pull/1047): `BuiltinHintProcessor` now supports the following hints: From 7de09600385760499c786808cc37d936ca17d9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s?= Date: Thu, 27 Apr 2023 10:12:36 -0300 Subject: [PATCH 4/4] Change &String -> &str --- CHANGELOG.md | 2 +- src/types/program.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b40e33f158..358b5e099d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,7 +115,7 @@ Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs segments.write_arg(ids.blake2s_ptr_end, padding) %} -* Add `Program::iter_identifiers(&self) -> Iterator` to get an iterator over the program's identifiers [#1079](https://github.com/lambdaclass/cairo-rs/pull/1079) +* Add `Program::iter_identifiers(&self) -> Iterator` to get an iterator over the program's identifiers [#1079](https://github.com/lambdaclass/cairo-rs/pull/1079) * Implement hint on `assert_le_felt` for versions 0.6.0 and 0.8.2 [#1047](https://github.com/lambdaclass/cairo-rs/pull/1047): diff --git a/src/types/program.rs b/src/types/program.rs index 0ef9e1d559..3d29aeaa60 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -125,8 +125,11 @@ impl Program { self.shared_program_data.identifiers.get(id) } - pub fn iter_identifiers(&self) -> impl Iterator { - self.shared_program_data.identifiers.iter() + pub fn iter_identifiers(&self) -> impl Iterator { + self.shared_program_data + .identifiers + .iter() + .map(|(cairo_type, identifier)| (cairo_type.as_str(), identifier)) } } @@ -491,7 +494,7 @@ mod tests { let collected_identifiers: HashMap<_, _> = program .iter_identifiers() - .map(|(cairo_type, identifier)| (cairo_type.clone(), identifier.clone())) + .map(|(cairo_type, identifier)| (cairo_type.to_string(), identifier.clone())) .collect(); assert_eq!(collected_identifiers, identifiers);