Skip to content

Conversation

@aerooneqq
Copy link
Contributor

@aerooneqq aerooneqq commented Dec 18, 2025

This PR adds support for reusing the whole trait with a one-line reuse syntax and is part of the delegation feature #118212:

trait T {
  fn foo(&self);
}

struct S;
impl T for S { ... }

struct Wrapper(S);
reuse impl T for Wrapper { self.0 }

The core idea is that we already have support for glob reuse, so in this scenario we want to transform one-line reuse into a trait impl block with a glob reuse in the following way:

//Before
reuse impl T for Wrapper { self.0 }

//After
impl T for Wrapper {
  reuse T::* { self.0 }
}

It seems like this task can be solved during parsing stage, when we encountered a one-line trait reuse, we can expand into this impl block right away, and the code which was already written to expand glob delegations will take care about the rest. We will copy trait path into glob reuse path.

The implementation of the transformation reuses already existing methods for impl parsing, however, we do not parse inner impl items, instead we parse "inner items" as delegation body. Thus, we do not have to deal with generics, consts, unsafe and other impl related features.

Other syntax possibility is trying to shorten one-line reuse by replacing impl keyword with reuse keyword:

reuse T for Wrapper { self.0 }

In this case implementation may become more complicated, and the syntax more confusing, as keywords such as const or unsafe will precede reuse, and there are also generics:

unsafe reuse<T1, T2> T for Wrapper { self.0 }

In the first (currently implemented) version reuse is placed in the beginning of the item, and it is clear that we will reuse trait implementation, while in the second, shorter version, the reuse keyword may be lost in generics and keywords that may precede impl.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants