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

Noam Yorav-Raphael noamraph at gmail.com
Wed Jun 11 23:25:16 PDT 2014


Cool. I was afraid that it will be harder for the compiler to optimize away
the enum, but it seems to be doing fine.
(If it does turn out that it's harder for the compiler, I don't see a real
problem with the approach I suggested, as a runtime failure can only be
caused by a bug in the code generator, not by user code)


On Thu, Jun 12, 2014 at 2:13 AM, Kevin Cantu <me at kevincantu.org> wrote:

> 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/20140612/fbb49448/attachment.html>


More information about the Rust-dev mailing list