rust-lang / rust-lang/rfcs

Suggestion to avoid wrappers with additional impl Trait construction

Open
#1,870 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Just as Deref allows one conversion to a type that may implement many traits, there could be a new impl Trait syntax that allows many conversions, each to one trait. I'm thinking something like this:

impl Trait for Type byconversion {
  fn from_self(&self) -> &TypeIsTrait { ... } // if a `Trait` method has arguments with `Self` type
  fn to_self(TypeIsTrait) -> Self { ... } // if a `Trait` method has a return value with `Self` type
}

I have no opinion about the form of the syntax. The above looks like a new trait, but it would work better as an alternate form of impl (I think)

The result would be an implicit conversion every time a method of Trait is called on Type, similar to Deref, but with traits instead of types. The search for which conversion to do would be handled by the existing search over traits.

The use case is currently handled by wrapper code. For example, most of impl PartialOrd for Rc consists of lines of the form: **self op **other. These could all be replaced by a single directive to implement PartialOrd by conversion (the conversion here being a simple deref).

There might be different forms of conversion for move vs borrow vs etc. I'm not sure how much complexity that would add. I think the two above (new type from reference, new self by move) would be the most useful, and most similar to the functionality provided by Deref.

Maybe this could even be a macro, but I don't yet have experience with macros, so I don't know.

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 issue proposal and the repository's RFC process; no implementation files or tests are identified. Determine whether implicit trait conversions are desired, settle the move and borrow cases and syntax, and document a complete design before implementation can be considered done.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.