Last active
August 8, 2021 16:03
-
-
Save ghulette/8398c2525d92d2b2369a0c45886a2839 to your computer and use it in GitHub Desktop.
How to fix: this won't type check because the From impl conflicts with the core reflexive instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
pub struct Point<T> { | |
pub x: T, | |
pub y: T, | |
} | |
impl<T> Point<T> { | |
pub fn new(x: T, y: T) -> Self { | |
Point { x, y } | |
} | |
} | |
/* | |
error[E0119]: conflicting implementations of trait `std::convert::From<Point<_>>` for type `Point<_>` | |
--> src/main.rs:13:1 | |
| | |
13 | / impl <T1, T2> From<Point<T1>> for Point<T2> | |
14 | | where | |
15 | | T1: Into<T2>, | |
16 | | { | |
... | | |
22 | | } | |
23 | | } | |
| |_^ | |
| | |
= note: conflicting implementation in crate `core`: | |
- impl<T> From<T> for T; | |
*/ | |
impl <T1, T2> From<Point<T1>> for Point<T2> | |
where | |
T1: Into<T2>, | |
{ | |
fn from(pt: Point<T1>) -> Self { | |
Point { | |
x: pt.x.into(), | |
y: pt.y.into(), | |
} | |
} | |
} | |
fn pt32(pt: &Point<f64>) { | |
println!("{:?}", pt) | |
} | |
fn main() { | |
let pt: Point<f32> = Point::new(5.0, 2.0); | |
pt32(&pt.into()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment