rust - Is there a way other than traits to extend structs? -
i'm trying extend grid
struct piston-2dgraphics library. there's no method getting location on window of particular cell, implemented trait calculate me. then, wanted method calculate neighbours of particular cell on grid, implemented trait.
something ugly , feels unnecessary seeing how i'll never use these traits other specific grid structure. so, there way in rust extend struct without having implement traits each time?
as of rust 1.3, no, there no other way.
it's not possible define inherent methods on type defined in crate. however, know, can define own trait methods need, implement trait external type. pattern known extension traits. name of extension traits, convention, ends ext
, indicate trait not meant used generic bound or trait object. there few examples in standard library.
other libraries can export extension traits (example: byteorder). however, other trait, need bring trait's methods in scope use somethingext;
.
Comments
Post a Comment