No method "push" found

Please use the glob operator when importing StorageVec:

use std::storage::storage_vec::*;

push() is implemented on StorageKey<StorageVec<T>> so importing StorageVec does not import push(). I will update the docs with this.

contract;

use std::{hash::Hash, storage::storage_vec::*,};

storage {
    nested_map_vec: StorageMap<u64, StorageVec<Identity>> = StorageMap {},
}

abi MyContract {
    #[storage(read, write)]
    fn debug(coupon_id: u64, id: Identity);
}

impl MyContract for Contract {
    #[storage(read, write)]
    fn debug(coupon_id: u64, id: Identity) {
        // Initalize
        storage.nested_map_vec.insert(coupon_id, StorageVec {});

        // Push
        storage.nested_map_vec.get(coupon_id).push(id);
    }
}
3 Likes

Thank you
Which trait was required for push?

No trait, the push() function is implemented on StorageKey<StorageVec>. The implementation can be found here.

/// A persistent vector struct.
pub struct StorageVec<V> {} // This was imported

impl<V> StorageKey<StorageVec<V>> { // <- This is not imported without the glob operator
    #[storage(read, write)]
    pub fn push(self, value: V) {
        let len = read::<u64>(self.field_id(), 0).unwrap_or(0);

        // Storing the value at the current length index (if this is the first item, starts off at 0)
        let key = sha256(self.field_id());
        let offset = offset_calculator::<V>(len);
        write::<V>(key, offset, value);

        // Incrementing the length
        write(self.field_id(), 0, len + 1);
    }
}

The PR to add this to the docs can be found here

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.