Why are these to impls "conflicting", and how do I get around this? What I'm trying to say seems logical: I have this trait called ClickListener
and things that are Fn()
or Fn(Event)
implement it. Is it complaining because it's possible to have a type that implements both those traits, so then it wouldn't know which impl
to use between 1
and 2
below? Maybe there is a way to tell it which one takes priority in that case?
#![allow(dead_code)]
#[derive(Debug)]
struct Event {
foo: i32,
}
trait ClickListener {
fn on_click(&self, event: Event);
}
// impl #1
impl<F: Fn(Event)> ClickListener for F {
fn on_click(&self, event: Event) {
(self)(event)
}
}
// impl #2
impl<F: Fn()> ClickListener for F {
fn on_click(&self, _event: Event) {
(self)()
}
}
struct ButtonBuilder {
text: String,
on_click: Option<Box<dyn ClickListener>>,
}
impl ButtonBuilder {
fn text<S: Into<String>>(mut self, s: S) -> Self {
self.text = s.into();
self
}
fn on_click<F: ClickListener + 'static>(mut self, f: F) -> Self {
self.on_click = Some(Box::new(f));
self
}
fn new() -> Self {
ButtonBuilder {
text: "".to_string(),
on_click: None,
}
}
}
fn main() {
let _b = ButtonBuilder::new()
.on_click(|e: Event| println!("click! e = {:?}", e));
let _b = ButtonBuilder::new()
.on_click(|| println!("click!"));
}
Error:
Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `ClickListener`
--> src/main.rs:18:1
|
12 | impl<F: Fn(Event)> ClickListener for F {
| -------------------------------------- first implementation here
...
18 | impl<F: Fn()> ClickListener for F {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation