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

[E0207] if type parameter used only in impl constraints #33374

Closed
KalitaAlexey opened this issue May 3, 2016 · 1 comment
Closed

[E0207] if type parameter used only in impl constraints #33374

KalitaAlexey opened this issue May 3, 2016 · 1 comment

Comments

@KalitaAlexey
Copy link
Contributor

use std::borrow::BorrowMut;

pub trait Value {
    fn get(&self) -> i32;
    fn set(&mut self, value: i32);
}

pub struct UnmanagedValue {
    value: i32,
}

impl Value for UnmanagedValue {
    fn get(&self) -> i32 {
        self.value
    }

    fn set(&mut self, value: i32) {
        self.value = value;
    }
}

pub struct ManagedValue {
    value: i32,
}

impl Value for ManagedValue {
    fn get(&self) -> i32 {
        self.value
    }

    fn set(&mut self, value: i32) {
        if self.value == value {
            return;
        }
        self.value = value;
    }
}

pub struct SetValueAction {
    value: i32,
}

pub trait Action<T>: Sized {
    fn execute(self, target: &mut T);
}

impl<V: Value, T: BorrowMut<V>> Action<T> for SetValueAction {
    fn execute(self, target: &mut T) {
        target.borrow_mut().set(self.value);
    }
}

fn main() {}

fails with error:

<anon>:47:6: 47:7 error: the type parameter `V` is not constrained by the impl trait, self type, or predicates [E0207]
<anon>:47 impl<V: Value, T: BorrowMut<V>> Action<T> for SetValueAction {
               ^
<anon>:47:6: 47:7 help: see the detailed explanation for E0207
error: aborting due to previous error
playpen: application terminated with error code 101

But this function compiles

pub fn execute<V: Value, T: BorrowMut<V>>(action: SetValueAction, target: &mut T) {
    target.borrow_mut().set(action.value);
}
@arielb1
Copy link
Contributor

arielb1 commented May 3, 2016

This is as per rust-lang/rfcs#447. <Action<Whatever> as SetValueAction>::execute must refer to a single method, but there can easily be multiple types that implement Value and are valid for BorrowMut.

@arielb1 arielb1 closed this as completed May 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants