Skip to content

Feature: Support Complex Generic Types in Contract Storage #289

@0xNeshi

Description

@0xNeshi

This specifically refers to upgrading #[storage] attribute. The following code compiles just fine:

pub struct EnumerableSet<T: SimpleStorageType<'static>>
where
    for<'a> <T as StorageType>::Wraps<'a>: StorageKey + Copy + Clone,
    Self: 'static,
{
    /// Values in the set.
    values: StorageVec<T>,
    /// Value -> Index of the value in the `values` array.
    positions: StorageMap<<T as StorageType>::Wraps<'static>, StorageU256>,
}

But adding the #[storage] attribute introduces compiler errors:

#[storage]
// ^^^^^^
// the trait bound `<T as stylus_sdk::prelude::StorageType>::Wraps<'static>: StorageKey` is not satisfied the trait `StorageKey` is not implemented for `<T as stylus_sdk::prelude::StorageType>::Wraps<'static>`
// map.rs(15, 26): required by a bound in `StorageMap`
pub struct EnumerableSet<T: SimpleStorageType<'static>>
where
    for<'a> <T as StorageType>::Wraps<'a>: StorageKey + Copy + Clone,
    Self: 'static,
{
    /// Values in the set.
    values: StorageVec<T>,
    /// Value -> Index of the value in the `values` array.
    positions: StorageMap<<T as StorageType>::Wraps<'static>, StorageU256>,
    //                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                the trait bound `<T as stylus_sdk::prelude::StorageType>::Wraps<'static>: StorageKey` is not satisfied the trait `StorageKey` is not implemented for `<T as stylus_sdk::prelude::StorageType>::Wraps<'static>` 
    //                map.rs(15, 26): required by a bound in `StorageMap`
    //                file_name.rs(23, 56): consider introducing a `where` clause, but there might be an alternative better way to express this requirement: ` where <T as stylus_sdk::prelude::StorageType>::Wraps<'static>: StorageKey`
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions