Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
use associated iterator types for InspectEnumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Sep 30, 2022
1 parent 61b9a4d commit 580f3f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
15 changes: 11 additions & 4 deletions frame/support/src/traits/tokens/nonfungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ pub trait Inspect<AccountId> {
/// Interface for enumerating items in existence or owned by a given account over a collection
/// of NFTs.
pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {
type ItemsIter: Iterator<Item = Self::ItemId>;
type OwnedIter: Iterator<Item = Self::ItemId>;

/// Returns an iterator of the items within a `collection` in existence.
fn items() -> Box<dyn Iterator<Item = Self::ItemId>>;
fn items() -> Self::ItemsIter;

/// Returns an iterator of the items of all collections owned by `who`.
fn owned(who: &AccountId) -> Box<dyn Iterator<Item = Self::ItemId>>;
fn owned(who: &AccountId) -> Self::OwnedIter;
}

/// Trait for providing an interface for NFT-like items which may be minted, burned and/or have
Expand Down Expand Up @@ -149,10 +152,14 @@ impl<
AccountId,
> InspectEnumerable<AccountId> for ItemOf<F, A, AccountId>
{
fn items() -> Box<dyn Iterator<Item = Self::ItemId>> {
type ItemsIter = <F as nonfungibles::InspectEnumerable<AccountId>>::ItemsIter;
type OwnedIter = <F as nonfungibles::InspectEnumerable<AccountId>>::OwnedInCollectionIter;

fn items() -> Self::ItemsIter {
<F as nonfungibles::InspectEnumerable<AccountId>>::items(&A::get())
}
fn owned(who: &AccountId) -> Box<dyn Iterator<Item = Self::ItemId>> {

fn owned(who: &AccountId) -> Self::OwnedIter {
<F as nonfungibles::InspectEnumerable<AccountId>>::owned_in_collection(&A::get(), who)
}
}
Expand Down
13 changes: 9 additions & 4 deletions frame/support/src/traits/tokens/nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,25 @@ pub trait Inspect<AccountId> {
/// Interface for enumerating items in existence or owned by a given account over many collections
/// of NFTs.
pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {
type CollectionsIter: Iterator<Item = Self::CollectionId>;
type ItemsIter: Iterator<Item = Self::ItemId>;
type OwnedIter: Iterator<Item = (Self::CollectionId, Self::ItemId)>;
type OwnedInCollectionIter: Iterator<Item = Self::ItemId>;

/// Returns an iterator of the collections in existence.
fn collections() -> Box<dyn Iterator<Item = Self::CollectionId>>;
fn collections() -> Self::CollectionsIter;

/// Returns an iterator of the items of a `collection` in existence.
fn items(collection: &Self::CollectionId) -> Box<dyn Iterator<Item = Self::ItemId>>;
fn items(collection: &Self::CollectionId) -> Self::ItemsIter;

/// Returns an iterator of the items of all collections owned by `who`.
fn owned(who: &AccountId) -> Box<dyn Iterator<Item = (Self::CollectionId, Self::ItemId)>>;
fn owned(who: &AccountId) -> Self::OwnedIter;

/// Returns an iterator of the items of `collection` owned by `who`.
fn owned_in_collection(
collection: &Self::CollectionId,
who: &AccountId,
) -> Box<dyn Iterator<Item = Self::ItemId>>;
) -> Self::OwnedInCollectionIter;
}

/// Trait for providing the ability to create collections of nonfungible items.
Expand Down
22 changes: 14 additions & 8 deletions frame/uniques/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use super::*;
use frame_support::{
storage::KeyPrefixIterator,
traits::{tokens::nonfungibles::*, Get},
BoundedSlice,
};
Expand Down Expand Up @@ -155,25 +156,30 @@ impl<T: Config<I>, I: 'static> Transfer<T::AccountId> for Pallet<T, I> {
}

impl<T: Config<I>, I: 'static> InspectEnumerable<T::AccountId> for Pallet<T, I> {
type CollectionsIter = KeyPrefixIterator<<T as Config<I>>::CollectionId>;
type ItemsIter = KeyPrefixIterator<<T as Config<I>>::ItemId>;
type OwnedIter = KeyPrefixIterator<(<T as Config<I>>::CollectionId, <T as Config<I>>::ItemId)>;
type OwnedInCollectionIter = KeyPrefixIterator<<T as Config<I>>::ItemId>;

/// Returns an iterator of the collections in existence.
///
/// NOTE: iterating this list invokes a storage read per item.
fn collections() -> Box<dyn Iterator<Item = Self::CollectionId>> {
Box::new(CollectionMetadataOf::<T, I>::iter_keys())
fn collections() -> Self::CollectionsIter {
CollectionMetadataOf::<T, I>::iter_keys()
}

/// Returns an iterator of the items of a `collection` in existence.
///
/// NOTE: iterating this list invokes a storage read per item.
fn items(collection: &Self::CollectionId) -> Box<dyn Iterator<Item = Self::ItemId>> {
Box::new(ItemMetadataOf::<T, I>::iter_key_prefix(collection))
fn items(collection: &Self::CollectionId) -> Self::ItemsIter {
ItemMetadataOf::<T, I>::iter_key_prefix(collection)
}

/// Returns an iterator of the items of all collections owned by `who`.
///
/// NOTE: iterating this list invokes a storage read per item.
fn owned(who: &T::AccountId) -> Box<dyn Iterator<Item = (Self::CollectionId, Self::ItemId)>> {
Box::new(Account::<T, I>::iter_key_prefix((who,)))
fn owned(who: &T::AccountId) -> Self::OwnedIter {
Account::<T, I>::iter_key_prefix((who,))
}

/// Returns an iterator of the items of `collection` owned by `who`.
Expand All @@ -182,7 +188,7 @@ impl<T: Config<I>, I: 'static> InspectEnumerable<T::AccountId> for Pallet<T, I>
fn owned_in_collection(
collection: &Self::CollectionId,
who: &T::AccountId,
) -> Box<dyn Iterator<Item = Self::ItemId>> {
Box::new(Account::<T, I>::iter_key_prefix((who, collection)))
) -> Self::OwnedInCollectionIter {
Account::<T, I>::iter_key_prefix((who, collection))
}
}

0 comments on commit 580f3f1

Please sign in to comment.