Structures API should support deref coercion

Open
#400 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust

Research direction

Start by locating the BTreeMap API implementation, especially its get method and existing key lookup behavior. Confirm how standard Rust collections handle borrowed or deref-compatible keys; done means btree.get("foo") works without constructing a temporary String while the existing lookup still works.

Written by the indexing model from the issue text.

Description

The current API implementation does not support deref coercion like the standard Rust collections. In some cases, this forces a redundant instantiation only for retrieving data. E.g.:

    #[test]
    fn should_allow_retrieving_with_deref_types() {
        let mem = make_memory();
        let mut btree: BTreeMap<String, u64, _> = BTreeMap::new(mem.clone());
        btree.insert("foo".to_string(), 42);

        // This one works but it forces a useless instantiation of a new String
        assert_eq!(btree.get(&"foo".to_string()), Some(42)); // OK

        // This does not compile because the API strictly expects a `&String` type
        assert_eq!(btree.get("foo"), Some(42)); 
    }
Dominant language
Rust
Stars
111
Forks
39
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dfinity/stable-structures

All issues in dfinity/stable-structures

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.