rust-lang / rust-lang/rfcs

Improve boilerplate for Trait impl

Open
#2,676 13 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-impls A-syntax T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently impl for a struct and traits look like this:

struct Services {};

impl Services {

}

impl Clone for Services {
    fn clone(&self) -> Services {
        Services {}
    }
}

Now if we add lifetimes, and/or generics, the syntax gets out of hand rather quickly.

struct Services<'a, T> { wrap_drive: &'a WarpDrive<T> };

impl<'a, T> Services<'a, T> {

}

impl<'a, T> Clone for Services<'a, T> {
    fn clone(&self) -> Services<'a, T> {
        Services {
          // ..
         }
    }
}

What I'm proposing is to be able to write this instead:

struct Services<'a, T> { wrap_drive: &'a WarpDrive<T> };

impl<'a, T> Services<'a, T> {
    fn beam_all_services() { }
    
    // ..snip..snip
    
    impl Clone for Self {
        fn clone(&self) -> Services<'a, T> {
            Services { 
               // ... 
            }
        }
    }
}

Advantages:

  • Reduce boiler plate for impl when it's an in-module struct.
  • Encourage in-module blocks to be cleanly encapsulated into the parent impl block, allowing for easier source comprehension and maintenance.
  • While the above is a trivial case, code bases like actix and Hyper which makes heavy use of generics can be made much more readable removing redundant type information from each impl blocks.
  • This is all entirely optional. So, no breaking changes.

Disadvantages:

  • impls are no longer just confined to the top-level, but also be one level nested.
  • Compiler complexity.

Contributor guide

No contributing guide indexed for this repository

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 with the proposed nested impl examples in this issue and review the surrounding discussion for unresolved design questions. No implementation files or tests are named; determine the Rust syntax and compiler areas affected before pursuing it. Done would require an accepted design and corresponding compiler and test changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.