Created
April 12, 2018 02:27
-
-
Save Sushisource/7e1441c56d56706386efcc48fc9ea0d5 to your computer and use it in GitHub Desktop.
Rust from/into generic impl
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
error[E0119]: conflicting implementations of trait `std::convert::Into<ggez::graphics::Rect>` for type `&_`: | |
--> src\dungeongen\rooms.rs:143:1 | |
| | |
143 | impl<'a, T: CenterOriginRect> Into<Rect> for &'a T { | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| | |
= note: conflicting implementation in crate `core`: | |
- impl<T, U> std::convert::Into<U> for T | |
where U: std::convert::From<T>; | |
= note: downstream crates may implement trait `std::convert::From<&_>` for type `ggez::graphics::Rect` | |
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter | |
--> src\dungeongen\rooms.rs:143:1 | |
| | |
143 | / impl<'a, T: CenterOriginRect> Into<Rect> for &'a T { | |
144 | | fn into(self) -> Rect { | |
145 | | Rect { | |
146 | | // GGEZ Rects have a top-left origin | |
... | | |
152 | | } | |
153 | | } | |
| |_^ | |
error: aborting due to 2 previous errors |
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
trait CenterOriginRect { | |
fn center(&self) -> Point; | |
fn width(&self) -> Meters; | |
fn height(&self) -> Meters; | |
} | |
impl CenterOriginRect for Room { | |
fn center(&self) -> Point { self.center } | |
fn width(&self) -> f32 { self.width } | |
fn height(&self) -> f32 { self.height } | |
} | |
impl<'a, T: CenterOriginRect> Into<Rect> for &'a T { | |
fn into(self) -> Rect { | |
Rect { | |
x: self.center().x - self.width() / 2.0, | |
y: self.center().y - self.height() / 2.0, | |
w: self.width(), | |
h: self.height(), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment