I'm trying to use specialization like this
#![feature(specialization)]
use std::marker::PhantomData;
struct Printer<T:Displayable>{
phantom: PhantomData<T>,
}
struct Speaker();
struct NormalPrintable ();
struct SpecialPrintable {
y : usize
}
trait Displayable{
// elided methods for displaying a Displayable
}
impl Displayable for NormalPrintable{}
impl Displayable for SpecialPrintable{}
trait DisplayProducer<T:Displayable>{
fn display(&self, t: T);
}
impl<T:Displayable> DisplayProducer<T> for Printer<T> {
default fn display(&self, _t: T) { println!("Printing something printable"); }
}
impl DisplayProducer<SpecialPrintable> for Printer<SpecialPrintable> {
fn display(&self, t: SpecialPrintable) { println!("SpecialPrintable is special: {}", t.y); }
}
impl<T: Displayable> DisplayProducer<T> for Speaker{
fn display(&self, _t: T){
// somehow display the Displayable acustically
println!("What's that noise?");
}
}
fn main() {
Speaker().display(NormalPrintable{});
Speaker().display(SpecialPrintable{y:3});
Printer{phantom: PhantomData}.display(SpecialPrintable{y:3});
// Uncommenting the next line leads creates an error
// Printer{phantom: PhantomData}.display(NormalPrintable{});
}
However, when I uncomment the commented line I get the following compile error
error[E0308]: mismatched types
--> src/main.rs:50:44
|
50 | Printer{phantom: PhantomData}.display(NormalPrintable{});
| ^^^^^^^^^^^^^^^^^ expected struct `SpecialPrintable`, found struct `NormalPrintable`
With the min_specialization feature it's no different.
What can I do?