Skip to content

Commit

Permalink
Update map iterator to be index based (#1061)
Browse files Browse the repository at this point in the history
### What

Sdk part of stellar/rs-soroban-env#992

### Why

[TODO: Why this change is being made. Include any context required to
understand the why.]

### Known limitations

[TODO or N/A]
  • Loading branch information
jayz22 authored Aug 19, 2023
1 parent 9506a86 commit da987a9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]
Expand Down
37 changes: 18 additions & 19 deletions soroban-sdk/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,15 @@ where
#[derive(Clone)]
pub struct MapTryIter<K, V> {
map: Map<K, V>,
pos: u32,
len: u32,
min_key: Val,
max_key: Val,
}

impl<K, V> MapTryIter<K, V> {
fn new(map: Map<K, V>) -> 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,
}
}
Expand All @@ -534,15 +531,16 @@ where

fn next(&mut self) -> Option<Self::Item> {
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,
Expand All @@ -556,7 +554,7 @@ where
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len as usize;
let len = (self.len - self.pos) as usize;
(len, Some(len))
}

Expand All @@ -570,15 +568,16 @@ where
{
fn next_back(&mut self) -> Option<Self::Item> {
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,
Expand Down Expand Up @@ -607,7 +606,7 @@ where
V: IntoVal<Env, Val> + TryFromVal<Env, Val>,
{
fn len(&self) -> usize {
self.len as usize
(self.len - self.pos) as usize
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ where
/// Create an empty Vec.
#[inline(always)]
pub fn new(env: &Env) -> Vec<T> {
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.
Expand Down

0 comments on commit da987a9

Please sign in to comment.