Skip to content

Commit

Permalink
Add storage path update trait.
Browse files Browse the repository at this point in the history
commit-id:01d5fd64
  • Loading branch information
gilbens-starkware committed Jul 7, 2024
1 parent 1af57c9 commit 24c52a7
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions corelib/src/starknet/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,31 @@ impl StoragePathImpl<T> of StoragePathTrait<T> {
}
}

/// Trait for updating the hash state of a storage path with a value. Notice that when updating the
/// hash state, usually the generic type of the storage path should be changed, however, this is not
/// done here, and `into` should be called to convert the storage path to the new type.
trait StoragePathUpdateTrait<T, Key> {
fn update(self: StoragePath<T>, value: Key) -> StoragePath<T>;
}

impl StoragePathUpdateImpl<
T, Key, +core::hash::Hash<Key, StoragePathHashState>
> of StoragePathUpdateTrait<T, Key> {
fn update(self: StoragePath<T>, value: Key) -> StoragePath<T> {
StoragePath {
hash_state: core::hash::Hash::<
Key, StoragePathHashState
>::update_state(self.hash_state, value)
}
}
}

impl StoragePathSIntoT<S, T> of core::traits::Into<StoragePath<S>, StoragePath<T>> {
fn into(self: StoragePath<S>) -> StoragePath<T> {
StoragePath::<T> { hash_state: self.hash_state }
}
}


/// Trait for creating a new `StoragePath` from a storage member.
pub trait StorageAsPath<TMemberState> {
Expand Down Expand Up @@ -227,13 +252,7 @@ impl EntryInfoStoragePathEntry<
type Key = EntryInfo::<T>::Key;
type Value = EntryInfo::<T>::Value;
fn entry(self: StoragePath<T>, key: EntryInfo::<T>::Key) -> StoragePath<EntryInfo::<T>::Value> {
StoragePath::<
EntryInfo::<T>::Value
> {
hash_state: core::hash::Hash::<
EntryInfo::<T>::Key, StoragePathHashState
>::update_state(self.hash_state, key)
}
self.update(key).into()
}
}

Expand All @@ -248,13 +267,7 @@ impl MutableEntryStoragePathEntry<
type Key = EntryImpl::Key;
type Value = Mutable<EntryImpl::Value>;
fn entry(self: StoragePath<T>, key: EntryImpl::Key) -> StoragePath<Mutable<EntryImpl::Value>> {
StoragePath::<
Mutable<EntryImpl::Value>
> {
hash_state: core::hash::Hash::<
EntryImpl::Key, StoragePathHashState
>::update_state(self.hash_state, key)
}
self.update(key).into()
}
}

Expand Down

0 comments on commit 24c52a7

Please sign in to comment.