Skip to content

Commit

Permalink
move IntoIterRange to starknet::storage
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 9, 2025
1 parent 99c69e8 commit c216934
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
18 changes: 15 additions & 3 deletions corelib/src/starknet/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
//! The generic type of the storage object can also be wrapped with a `Mutable` type, which
//! indicates that the storage object is mutable, i.e., it was created from a `ref` contract state,
//! and thus the object can be written to.

use core::hash::HashStateTrait;
#[allow(unused_imports)]
use core::pedersen::HashState;
Expand All @@ -78,8 +77,11 @@ use starknet::SyscallResult;
use starknet::storage_access::{StorageBaseAddress, storage_base_address_from_felt252};

mod vec;
pub use vec::{IntoIterRange, MutableVecTrait, Vec, VecTrait};
use vec::{MutableVecIndexView, VecIndexView};
use vec::{
MutableVecIndexView, MutableVecIntoIterRange, PathableMutableVecIntoIterRange,
PathableVecIntoIterRange, VecIndexView, VecIntoIterRange,
};
pub use vec::{MutableVecTrait, Vec, VecTrait};

mod storage_node;
pub use storage_node::{StorageNode, StorageNodeMut};
Expand Down Expand Up @@ -438,3 +440,13 @@ trait MutableTrait<T> {
impl MutableImpl<T> of MutableTrait<Mutable<T>> {
type InnerType = T;
}

/// Trait for turning collection of values into an iterator over a specific range.
pub trait IntoIterRange<T> {
type IntoIter;
impl Iterator: Iterator<Self::IntoIter>;
/// Creates an iterator over a range from a collection.
fn into_iter_range(self: T, range: core::ops::Range<u64>) -> Self::IntoIter;
/// Creates an iterator over the full range of a collection.
fn into_iter_full_range(self: T) -> Self::IntoIter;
}
22 changes: 6 additions & 16 deletions corelib/src/starknet/storage/vec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
//! arr
//! }
//! ```
use core::iter::{IntoIterator, Iterator};
use core::ops::Range;
use super::{
Mutable, StorageAsPath, StorageAsPointer, StoragePath, StoragePathTrait, StoragePathUpdateTrait,
StoragePointer0Offset, StoragePointerReadAccess, StoragePointerWriteAccess,
IntoIterRange, Mutable, StorageAsPath, StorageAsPointer, StoragePath, StoragePathTrait,
StoragePathUpdateTrait, StoragePointer0Offset, StoragePointerReadAccess,
StoragePointerWriteAccess,
};

/// Represents a dynamic array in contract storage.
Expand Down Expand Up @@ -397,16 +397,6 @@ pub impl MutableVecIndexView<
}
}

/// Turn a collection of values into an iterator over a specific range.
pub trait IntoIterRange<T> {
type IntoIter;
impl Iterator: Iterator<Self::IntoIter>;
/// Creates an iterator over a range from a collection.
fn into_iter_range(self: T, range: Range<u64>) -> Self::IntoIter;
/// Creates an iterator over the full range of a collection.
fn into_iter_full_range(self: T) -> Self::IntoIter;
}

/// An iterator struct over a `Vec` in storage.
#[derive(Drop)]
pub struct VecIter<T, impl VecTraitImpl: VecTrait<T>> {
Expand All @@ -422,7 +412,7 @@ impl VecIterator<T, impl VecTraitImpl: VecTrait<T>, +Drop<T>, +Copy<T>> of Itera
}

// Implement `IntoIterRange` for `StoragePath<Vec<T>>`
impl VecIntoIterRange<
pub impl VecIntoIterRange<
T, impl VecTraitImpl: VecTrait<StoragePath<Vec<T>>>,
> of IntoIterRange<StoragePath<Vec<T>>> {
type IntoIter = VecIter<StoragePath<Vec<T>>, VecTraitImpl>;
Expand Down Expand Up @@ -473,7 +463,7 @@ impl MutableVecIterator<
}

// Implement `IntoIterRange` for `StoragePath<Mutable<Vec<T>>>`
impl MutableVecIntoIterRange<
pub impl MutableVecIntoIterRange<
T, impl MutVecTraitImpl: MutableVecTrait<StoragePath<Mutable<Vec<T>>>>,
> of IntoIterRange<StoragePath<Mutable<Vec<T>>>> {
type IntoIter = MutableVecIter<StoragePath<Mutable<Vec<T>>>, MutVecTraitImpl>;
Expand All @@ -489,7 +479,7 @@ impl MutableVecIntoIterRange<

/// Implement `IntoIterRange` for any type that implements StorageAsPath into a storage path
/// that implements MutableVecTrait.
impl PathableMutableVecIntoIterRange<
pub impl PathableMutableVecIntoIterRange<
T,
+Destruct<T>,
impl PathImpl: StorageAsPath<T>,
Expand Down

0 comments on commit c216934

Please sign in to comment.