Skip to content

Instantly share code, notes, and snippets.

@ghulette
Last active August 8, 2021 16:03
Show Gist options
  • Save ghulette/8398c2525d92d2b2369a0c45886a2839 to your computer and use it in GitHub Desktop.
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
#[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