Is there a way other than traits to add methods to a type I don't own?

前端 未结 2 1830
半阙折子戏
半阙折子戏 2020-11-29 10:54

I\'m trying to extend the Grid struct from the piston-2dgraphics library. There\'s no method for getting the location on the window of a particular cell, so I implemented a

2条回答
  •  抹茶落季
    2020-11-29 11:38

    No. Currently, the only way to write new methods for a type that has been defined in another crate is through traits. However, this seems too cumbersome as you have to write the both the trait definition and the implementation.

    In my opinion, the way to go is to use free functions instead of methods. This would at least avoid the duplication caused by traits.

提交回复
热议问题