Skip to content

Allow SovState use Borrow to behave similar to std::collections::HashMap #427

@citizen-stig

Description

@citizen-stig

Consider following rust code, which compares usage of HashMap and StateMap:

use std::collections::HashMap;
use sov_state::{DefaultStorageSpec, Prefix, ProverStorage, StateMap, WorkingSet};

pub fn hashmap() {
    let mut map: HashMap<Vec<u8>, u64> = HashMap::new();

    map.insert(vec![1, 2, 3], 1);
    map.insert(vec![4, 5, 6], 2);
    map.insert(vec![7, 8, 9], 3);

    println!("HASH MAP: {:?}", map);

    let check_this: [u8; 3] = [1, 2, 3];

    let v = map.get(&check_this[..]);
    assert!(v.is_some());
    assert_eq!(v, Some(&1));
}

pub fn sovstatemap() {
    let tmpdir = tempfile::tempdir().unwrap();
    let mut working_set = WorkingSet::new(ProverStorage::<DefaultStorageSpec>::with_path(tmpdir.path()).unwrap());


    let map: StateMap<Vec<u8>, u64> = StateMap::new(Prefix::new(vec![1, 2, 3]));

    let a = vec![1, 2, 3];
    let b = vec![4, 5, 6];
    let c = vec![7, 8, 9];
    map.set(&a, &1, &mut working_set);
    map.set(&b, &2, &mut working_set);
    map.set(&c, &3, &mut working_set);

    // Uncomment this to get compiler error
    // let check_this: [u8; 3] = [1, 2, 3];
    // let v = map.get(&check_this[..]);
    // assert!(v.is_some());

}

It would be nice to be able to use slice the same as with standard HashMap, it will remove some unnecessary clonings in default STF.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions