by coolreader18 · 5y ago · 0 comments
struct MyEntity {
cache: RenderingCache,
}
impl Drawable for MyEntity {
fn draw(&self, canvas: &mut Canvas) {
self.cache.draw_cached(canvas, |cache_canvas| {
cache_canvas.render(ASSET);
});
}
}
const ASSET: Asset = load_asset("foo.png");Think Rust differs enough that it's a bit hard for me to draw direct comparisons with my primary languages, but with the emphasis on interfaces like that it does seem like a nicer way to implement extension methods.
class MyEntity {
RenderingCache cache;
}
now MyEntity implements Drawable {
void draw(Canvas canvas) {
this.cache.draw_cached(canvas ...);
}
}If you know haskell, Rust's traits are very similar to type classes, except it also has c++-like generics (templates) and is primarily expression-based like ocaml.
traits vs haskell type classes:
trait -> class struct -> data instance -> impl