rust-lang / rust-lang/rust-analyzer

New assist: Generate `IndexMut` implementation from `Index` implementation

Open
#15,581 8 comments 0 reactions 1 assignee View on GitHub

@Young-Flash is already working on this.

Since Nov 2, 2023.

A-assists C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Given an existing core::ops::Index implementation, this assist would generate a copy of it where

  • Index becomes IndexMut
  • The method signature is updated
  • & in the return value position in the function body (and perhaps other obviously-relevant positions) is replaced with &mut

For an example from the code I'm working on right now,

pub enum Axis { X = 0, Y = 1, Z = 2 }

impl<T> ops::Index<Axis> for [T; 3] {
    type Output = T;

    fn index(&self, index: Axis) -> &Self::Output {
        &self[index as usize]
    }
}

could be used to generate

impl<T> ops::IndexMut<Axis> for [T; 3] {
    fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
        &mut self[index as usize]
    }
}

with no manual changes instead of 6. There might be other ref/mut trait pairs that could be assisted similarly, too.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.