From da987a99dfe318e77b36502f1aceb73d78842368 Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Sat, 19 Aug 2023 02:07:07 -0400 Subject: [PATCH] Update map iterator to be index based (#1061) ### What Sdk part of https://github.com/stellar/rs-soroban-env/issues/992 ### Why [TODO: Why this change is being made. Include any context required to understand the why.] ### Known limitations [TODO or N/A] --- Cargo.lock | 12 ++++++------ Cargo.toml | 8 ++++---- soroban-sdk/src/map.rs | 37 ++++++++++++++++++------------------- soroban-sdk/src/vec.rs | 2 +- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 111a119e6..ff9c8d51e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,7 +1103,7 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "soroban-env-common" version = "0.0.17" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d92944576e2301c9866215efcdc4bbd24a5f3981#d92944576e2301c9866215efcdc4bbd24a5f3981" +source = "git+https://github.com/stellar/rs-soroban-env?rev=c2e1c21cf8d44db23a159090e3cbaab741860295#c2e1c21cf8d44db23a159090e3cbaab741860295" dependencies = [ "arbitrary", "crate-git-revision", @@ -1120,7 +1120,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.17" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d92944576e2301c9866215efcdc4bbd24a5f3981#d92944576e2301c9866215efcdc4bbd24a5f3981" +source = "git+https://github.com/stellar/rs-soroban-env?rev=c2e1c21cf8d44db23a159090e3cbaab741860295#c2e1c21cf8d44db23a159090e3cbaab741860295" dependencies = [ "soroban-env-common", "static_assertions", @@ -1129,7 +1129,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.17" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d92944576e2301c9866215efcdc4bbd24a5f3981#d92944576e2301c9866215efcdc4bbd24a5f3981" +source = "git+https://github.com/stellar/rs-soroban-env?rev=c2e1c21cf8d44db23a159090e3cbaab741860295#c2e1c21cf8d44db23a159090e3cbaab741860295" dependencies = [ "backtrace", "curve25519-dalek", @@ -1155,7 +1155,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.17" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d92944576e2301c9866215efcdc4bbd24a5f3981#d92944576e2301c9866215efcdc4bbd24a5f3981" +source = "git+https://github.com/stellar/rs-soroban-env?rev=c2e1c21cf8d44db23a159090e3cbaab741860295#c2e1c21cf8d44db23a159090e3cbaab741860295" dependencies = [ "itertools", "proc-macro2", @@ -1181,7 +1181,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.17" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d92944576e2301c9866215efcdc4bbd24a5f3981#d92944576e2301c9866215efcdc4bbd24a5f3981" +source = "git+https://github.com/stellar/rs-soroban-env?rev=c2e1c21cf8d44db23a159090e3cbaab741860295#c2e1c21cf8d44db23a159090e3cbaab741860295" dependencies = [ "itertools", "proc-macro2", @@ -1307,7 +1307,7 @@ dependencies = [ [[package]] name = "stellar-xdr" version = "0.0.17" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=4876e5eb20016caebbd13bcf6401626dc6073b8e#4876e5eb20016caebbd13bcf6401626dc6073b8e" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=2d2526f515a476b1e3df70233a4cf9232287977e#2d2526f515a476b1e3df70233a4cf9232287977e" dependencies = [ "arbitrary", "base64 0.13.1", diff --git a/Cargo.toml b/Cargo.toml index 9279aff14..1bd8a4670 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,17 +40,17 @@ soroban-token-sdk = { version = "0.9.2", path = "soroban-token-sdk" } [workspace.dependencies.soroban-env-common] version = "0.0.17" git = "https://github.com/stellar/rs-soroban-env" -rev = "d92944576e2301c9866215efcdc4bbd24a5f3981" +rev = "c2e1c21cf8d44db23a159090e3cbaab741860295" [workspace.dependencies.soroban-env-guest] version = "0.0.17" git = "https://github.com/stellar/rs-soroban-env" -rev = "d92944576e2301c9866215efcdc4bbd24a5f3981" +rev = "c2e1c21cf8d44db23a159090e3cbaab741860295" [workspace.dependencies.soroban-env-host] version = "0.0.17" git = "https://github.com/stellar/rs-soroban-env" -rev = "d92944576e2301c9866215efcdc4bbd24a5f3981" +rev = "c2e1c21cf8d44db23a159090e3cbaab741860295" [workspace.dependencies.stellar-strkey] version = "0.0.7" @@ -60,7 +60,7 @@ rev = "e6ba45c60c16de28c7522586b80ed0150157df73" [workspace.dependencies.stellar-xdr] version = "0.0.17" git = "https://github.com/stellar/rs-stellar-xdr" -rev = "4876e5eb20016caebbd13bcf6401626dc6073b8e" +rev = "2d2526f515a476b1e3df70233a4cf9232287977e" default-features = false #[patch."https://github.com/stellar/rs-soroban-env"] diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index a745461f8..be6f85071 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -508,18 +508,15 @@ where #[derive(Clone)] pub struct MapTryIter { map: Map, + pos: u32, len: u32, - min_key: Val, - max_key: Val, } impl MapTryIter { fn new(map: Map) -> Self { - let env = map.env(); Self { + pos: 0, len: map.len(), - min_key: env.map_min_key(map.to_object()).unwrap_infallible(), - max_key: env.map_max_key(map.to_object()).unwrap_infallible(), map, } } @@ -534,15 +531,16 @@ where fn next(&mut self) -> Option { let env = self.map.env(); - if self.len == 0 { + if self.pos == self.len { return None; } - let key = self.min_key; - self.min_key = env - .map_next_key(self.map.to_object(), key) + let key = env + .map_key_by_pos(self.map.to_object(), self.pos.into()) .unwrap_infallible(); - self.len -= 1; - let value = env.map_get(self.map.to_object(), key).unwrap_infallible(); + let value = env + .map_val_by_pos(self.map.to_object(), self.pos.into()) + .unwrap_infallible(); + self.pos += 1; Some(Ok(( match K::try_from_val(env, &key) { Ok(k) => k, @@ -556,7 +554,7 @@ where } fn size_hint(&self) -> (usize, Option) { - let len = self.len as usize; + let len = (self.len - self.pos) as usize; (len, Some(len)) } @@ -570,15 +568,16 @@ where { fn next_back(&mut self) -> Option { let env = self.map.env(); - if self.len == 0 { + if self.pos == self.len { return None; } - let key = self.max_key; - self.max_key = env - .map_prev_key(self.map.to_object(), key) - .unwrap_infallible(); self.len -= 1; - let value = env.map_get(self.map.to_object(), key).unwrap_infallible(); + let key = env + .map_key_by_pos(self.map.to_object(), self.len.into()) + .unwrap_infallible(); + let value = env + .map_val_by_pos(self.map.to_object(), self.len.into()) + .unwrap_infallible(); Some(Ok(( match K::try_from_val(env, &key) { Ok(k) => k, @@ -607,7 +606,7 @@ where V: IntoVal + TryFromVal, { fn len(&self) -> usize { - self.len as usize + (self.len - self.pos) as usize } } diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 0c08667c6..3cb44c49a 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -365,7 +365,7 @@ where /// Create an empty Vec. #[inline(always)] pub fn new(env: &Env) -> Vec { - unsafe { Self::unchecked_new(env.clone(), env.vec_new(().into()).unwrap_infallible()) } + unsafe { Self::unchecked_new(env.clone(), env.vec_new().unwrap_infallible()) } } /// Create a Vec from the array of items.