Hi,
Given a full implementation of specialization in the rust compiler, would that work ?
Playground
#[rustc_unsafe_specialization_marker]
trait IsFloat {}
#[rustc_unsafe_specialization_marker]
trait IsInt {}
impl IsFloat for f32 {
}
impl IsFloat for f64 {
}
impl IsInt for u32 {
}
impl IsInt for u8 {
}
... some more for primitive types
impl<T: Display + IsInt> Serialize for T {
fn serialize(&self) {
println!("Is a Int Type {}", self);
}
}
impl<T: Display + IsFloat> Serialize for T {
fn serialize(&self) {
println!("Is a float Type {}", self);
}
}
impl<T: Display + IsInt + IsFloat> Serialize for T {
fn serialize(&self) {
println!("Won't happen {}", self);
}
}
impl<T: Display> Serialize for T {
default fn serialize(&self) {
println!("Is another Type {}", self);
}
}
Now the case T: Display + IsInt + IsFloat
should be a catch all case that should lead to compilation of the code because, even if later someone add the impl of IsInt
for f32 (nobody would), it would still fall in this case .
But now, it complains about conflicting implementations, which they are not.
So is this a current limitation (fine), or is there something else that I don't get? Tried with min_specialization
and specialization
.
Thanks