Skip to content

Commit

Permalink
Merge pull request #67 from CosmWasm/start-end-bound
Browse files Browse the repository at this point in the history
Use same bound type for start and end of iteration
  • Loading branch information
webmaster128 authored Sep 23, 2024
2 parents 55f0234 + a267fda commit ded4533
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
49 changes: 47 additions & 2 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,28 +558,73 @@ mod tests {
access.push(&2).unwrap();
access.remove(2).unwrap();

// start and end set
assert_eq!(
access
.bounded_pairs(Some(1), Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(1, 42), (3, 1)]
);

assert_eq!(
access
.bounded_keys(Some(1), Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1, 3]
);

assert_eq!(
access
.bounded_values(Some(1), Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![42, 1]
);

// end unset
assert_eq!(
access
.bounded_pairs(Some(1), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(1, 42), (3, 1), (4, 2)]
);
assert_eq!(
access
.bounded_keys(Some(1), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1, 3, 4]
);
assert_eq!(
access
.bounded_values(Some(1), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![42, 1, 2]
);

// start unset
assert_eq!(
access
.bounded_pairs(None, Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(0, 1337), (1, 42), (3, 1)]
);
assert_eq!(
access
.bounded_keys(None, Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0, 1, 3]
);
assert_eq!(
access
.bounded_values(None, Some(4))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1337, 42, 1]
);
}
}
27 changes: 12 additions & 15 deletions packages/storey/src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ pub trait IterableAccessor: Sized {
/// in turn means the entries found between two string keys may not be the expected ones.
pub trait BoundedIterableAccessor: IterableAccessor {
/// Iterate over key-value pairs in this collection, respecting the given bounds.
fn bounded_pairs<S, E>(
fn bounded_pairs<B>(
&self,
start: Option<S>,
end: Option<E>,
start: Option<B>,
end: Option<B>,
) -> StorableIter<'_, Self::Storable, Self::Storage>
where
S: BoundFor<Self::Storable>,
E: BoundFor<Self::Storable>,
B: BoundFor<Self::Storable>,
{
let start = start.map(|b| b.into_bytes());
let end = end.map(|b| b.into_bytes());
Expand All @@ -144,14 +143,13 @@ pub trait BoundedIterableAccessor: IterableAccessor {
}

/// Iterate over keys in this collection, respecting the given bounds.
fn bounded_keys<S, E>(
fn bounded_keys<B>(
&self,
start: Option<S>,
end: Option<E>,
start: Option<B>,
end: Option<B>,
) -> StorableKeys<'_, Self::Storable, Self::Storage>
where
S: BoundFor<Self::Storable>,
E: BoundFor<Self::Storable>,
B: BoundFor<Self::Storable>,
{
let start = start.map(|b| b.into_bytes());
let end = end.map(|b| b.into_bytes());
Expand All @@ -163,14 +161,13 @@ pub trait BoundedIterableAccessor: IterableAccessor {
}

/// Iterate over values in this collection, respecting the given bounds.
fn bounded_values<S, E>(
fn bounded_values<B>(
&self,
start: Option<S>,
end: Option<E>,
start: Option<B>,
end: Option<B>,
) -> StorableValues<'_, Self::Storable, Self::Storage>
where
S: BoundFor<Self::Storable>,
E: BoundFor<Self::Storable>,
B: BoundFor<Self::Storable>,
{
let start = start.map(|b| b.into_bytes());
let end = end.map(|b| b.into_bytes());
Expand Down

0 comments on commit ded4533

Please sign in to comment.