[rust-dev] Qt5 Rust bindings and general C++ to Rust bindings feedback

Kevin Cantu me at kevincantu.org
Wed Jun 11 16:13:35 PDT 2014


Matthew Monrocq suggests this improvement, which looks even cleaner to use,
although slightly more complicated to implement generation of:


On Wed, Jun 11, 2014 at 11:38 AM, Matthieu Monrocq <
matthieu.monrocq at gmail.com> wrote:

> [snip]
>
> I do like the idea of the trait, however I would rather do away with all
> the `get_aa`: why not directly wrap the parameters ?
>
>     enum AaBbEnum {
>         Aa(int, f64),
>         Bb(f64),
>     }
>     trait AaBb {
>         fn get(&self) -> AaBbEnum;
>
>     }
>
>     impl AaBb for (int, f64) {
>         fn get(&self) -> AaBbEnum { match *self { (i, f) => Aa(i, f), } }
>     }
>
>     impl AaBb for (f64) {
>         fn get(&self) -> AaBbEnum { Bb(*self) }
>
>     }
>
>     fn overloaded<T: AaBb>(x: T) {
>         match x.get() {
>             Aa(i, f) => println!("got Aa: {}", (i, f)),
>             Bb(f) => println!("got Bb: {}", f),
>
>         }
>     }
>
>     #[main]
>     fn main() {
>         overloaded((5i, 7.3243)); // prints: got Aa: (5, 7.3243)
>         overloaded((3.5)); // prints: got Bb: 3.5
>     }
>
> Now, there is no runtime failure => you cannot accidentally match on `Bb`
> and requests `get_aa`!
>



Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140611/04529400/attachment.html>


More information about the Rust-dev mailing list