rust-lang / rust-lang/rust-analyzer

New assist: Generate `From` implementations from `from()` or `into()` calls.

Open
#15,926 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Given a B::from(a) or a.into() call which is currently erroring with an unsatisfied From<A> or Into<B> bound, this assist would generate the skeleton of the appropriate impl:

impl From<A> for B {
    fn from(value: A) -> Self {
        todo!()
    }
}

A further elaboration might be to sketch the body:

  • If A is an enum, automatically insert a match value { <arms> }.
  • If A is a struct with public fields, automatically insert let A { <fields> } = value;.
  • If B is a struct with public fields, automatically insert Self { <fields> }.

This assist would greatly speed up the process of writing type conversion boilerplate, and would make writing Froms at least as easy as writing plain functions with the “Generate function” assist.

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.

Research direction

Start by examining the existing “Generate function” assist referenced in the issue and how rust-analyzer assists are structured. Implement generation of the From<A> for B skeleton for failing from() or into() calls, then consider whether the enum and struct body elaborations belong in the same scope; done means the assist produces the requested implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.