These are chat archives for rust-lang/rust

25th
Dec 2017
TatriX
@TatriX
Dec 25 2017 17:53
If there is a way to define this without copy-paste/macros? Text and Image are types from the library both having the same method fn width(&self) -> u32
pub trait Center {
    fn center(&self) -> Point2;
}

impl Center for Text {
    fn center(&self) -> Point2 {
        center(self.width() as f32, self.height() as f32)
    }
}

impl Center for Image {
    fn center(&self) -> Point2 {
        center(self.width() as f32, self.height() as f32)
    }
}
Aleksey Kladov
@matklad
Dec 25 2017 17:55
You can do this, I think trait Width { fn width(&self) -> u32 }; impl Width for Text { .. } impl Width for Image { .. } impl<T: Width> Center for T {}
TatriX
@TatriX
Dec 25 2017 17:57
But Text already has method width() defined in the library itself.
https://github.com/ggez/ggez/blob/master/src/graphics/mod.rs#L1311
https://github.com/ggez/ggez/blob/master/src/graphics/text.rs#L431
I'd like to be able to center these when drawing, so my thought was to reuse width() and height() somehow.
TatriX
@TatriX
Dec 25 2017 18:04
I think I'll just use a simple macro for generating implemenations. Thanks ;)
Aleksey Kladov
@matklad
Dec 25 2017 18:06

Yeah, if width from Image and Text are not related in any way, there are only two ways forward:

1) a macro
2) a pull request witch adds trait Dimetions { fn width(); fn height(); } to upstream