Skip to content

Improve boilerplate for Trait impl #2676

Open

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-implsImplementations related proposals & ideasImplementations related proposals & ideasA-syntaxSyntax related proposals & ideasSyntax related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions