Lucifers,

I started playing around with Rust and, naturally, I became interested in how Rust bindings for Clownfish could be implemented. Here's a first sketch:

https://gist.github.com/nwellnhof/0c83c64a8f15c253e73e

Some notes:

The approach is somewhat similar to the Go bindings. Clownfish objects are implemented as structs holding a raw C pointer to cfish_Obj.

Methods are implemented as default methods in a trait accompanying each class. This trait inherits from the trait of the superclass. This way, we only have to implement a method once.

The vtable lookup for method dispatch is reimplemented in Rust.

Rust doesn't offer the ability to freely cast between trait objects (like type assertions in Go). So I implemented my own method to cast Clownfish objects that uses cfish_Obj_is_a internally. Right now, this method is not only used for downcasting but also for upcasting which is a bit inconvenient. We should probably add helper methods for upcasting.

Nick

Reply via email to