Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic dereferencing doesn't occur on std::ops::* traits #34405

Closed
ghost opened this issue Jun 22, 2016 · 3 comments
Closed

Automatic dereferencing doesn't occur on std::ops::* traits #34405

ghost opened this issue Jun 22, 2016 · 3 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Jun 22, 2016

use std::ops::Add;

#[derive(Copy, Clone)]
struct Wrapper(i8);

// Only defining one implementation of Add for Wrapper,
// with the assumption that automatic dereferencing will handle
// (&a, b), (a, &b), and (&a, &b)
impl Add for Wrapper {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        Wrapper(self.0 + other.0)
    }
}

// This will fail, however.
// <anon>:29:31: 29:35 error: binary operation `+` cannot be applied to type `&Wrapper` [E0369]
// <anon>:29     println!("&a + b: {}\n", (refa + b).0);
/*
fn std_add_wrappers() {
    println!("Wrapper's std::ops::add");
    let a: Wrapper = Wrapper(1);
    let b: Wrapper = Wrapper(2);

    println!("a + b: {}", (a + b).0);

    let refa = &a;

    println!("&a + b: {}\n", (refa + b).0);
}
*/

// This succeeds, since std::ops::Add's parameters for i8 are explicitly 
// implemented for (a, b), (&a, b), (a, &b), and (&a, &b), 
// according to stdlib docs
// https://doc.rust-lang.org/core/ops/trait.Add.html
fn std_add_integers() {
    println!("i8's std::ops::add");
    let a: i8 = 1;
    let b: i8 = 2;

    println!("a + b: {}", a + b);

    let refa = &a;

    println!("&a + b: {}\n", refa + b);
}


// So lets define our own add functionality, 
// as the good lord who created asm intended
trait DIYAdd<RHS=Self> {
    type Output;
    fn diy_add(self, other: RHS) -> Self::Output;
}

// Only one definition is required, and automatic
// dereferencing handles the rest
impl DIYAdd for Wrapper {
    type Output = Self;

    fn diy_add(self, other: Self) -> Self::Output {
        Wrapper(self.0 + other.0)
    }
}

fn diy_add_wrappers() {
    println!("Wrapper's diy_add");
    let a: Wrapper = Wrapper(1);
    let b: Wrapper = Wrapper(2);

    println!("a.diy_add(b): {}", (a.diy_add(b)).0);

    let refa = &a;

    println!("&a.diy_add(b): {}\n", (refa.diy_add(b)).0);
}

fn main() {
    std_add_integers();
    //std_add_wrappers();
    diy_add_wrappers()
}

Playground Link

@durka
Copy link
Contributor

durka commented Jun 22, 2016

I think it's because the real desugaring is Add::add(a, b), not a.add(b).

@Mark-Simulacrum Mark-Simulacrum added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 25, 2017
@leoyvens
Copy link
Contributor

leoyvens commented Mar 2, 2018

Triage: Superseded by #44762

@steveklabnik
Copy link
Member

Yes, it seems that #447672 is tracking this now, so I'm gonna give this a close. If this is in error please re-open!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants