rust-lang / rust-lang/rfcs

Proposal: Trait Derivation

Open
#3,194 14 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Short story: rust should enable a specific flavor of supertrait-subtrait relationship, that is, when a trait B is implemented, trait A would also be derived as a supertrait.

A bit longer story: sometimes, it is only reasonable for certain methods of a supertrait A to be related to that of a subtrait B in certain ways. For a plausible example,

pub mod Graph {
    pub trait Graph {
        type Node; 
        fn adj(&self, node: Self::Node) -> Vec<Box<Self::Node>>; 
    }
}

pub mod tree { 
    // import Graph 

    pub trait Tree { 
        fn parent(&self) -> Option<Box<Self>>; 
        fn children(&self) -> Vec<Box<Self>>;
    } 

    // Now, it would be somewhat natural to do the following: 
    // imaginary syntax 
    derive Graph for Tree { 
        type Node = Self; 
        fn adj(&self, node: Self::Node) -> Vec<Box<Self::Node>> { 
            let mut result: Vec<Box<Self::Node>> = node.children(); 
            match node.parent() {
                Some(p) -> { result.push(p); }
                None -> {}
            }
            result
        } 
    } 
} 

Note that a blanket implementation is not what I'm suggesting, as it has necessary restrictions imposed by the orphan rule.

The reason for this, in a narrower sense, is that it is often perfectly natural to want to automatically derive some traits from one without manual implementation for each concrete type; and perhaps in a general and broader sense, is to extend the variety of the relationships between traits that is so far limited to plain supertrait-subtrait by injecting some dependence of the supertrait on the subtrait, while usually the dependence is that of the subtrait on the supertrait.

And btw I am a newbie in rust and a very inexperienced programmer, and I am open to any suggestions or corrections.

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

No repository files, tests, or implementation entry points are identified. Start by reviewing the proposed trait-derivation examples and the Rust RFC process, then determine the language-design and implementation scope. Done would require an agreed design and an accepted path for the proposed relationship.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.