contextfree / winrt-rust

Use and (eventually) make Windows Runtime APIs with Rust
Apache License 2.0
142 stars 10 forks source link

Make sure that you can match on enum values #15

Closed Boddlnagg closed 7 years ago

Boddlnagg commented 7 years ago

I'm not sure if the current implementation strategy allows using match for enum values. Should work for the variants with and without associated constants. I'm too lazy to test this right now, so just close this if it already works.

Boddlnagg commented 7 years ago

I just verified that everything works okay.

This is the variant with associated constants, which currently requires nightly:

let t = MidiMessageType::NoteOn;
match t {
    MidiMessageType::NoteOff => println!("off"),
    MidiMessageType::NoteOn => println!("on"),
    _ => ()
};

And this variant without associated constants results in warnings, but otherwise works as intended (also on stable):

let t = MidiMessageType_NoteOn;
match t {
    MidiMessageType_NoteOff => println!("off"),
    MidiMessageType_NoteOn => println!("on"),
    _ => ()
};

#[allow(non_upper_case_globals)] should be sufficient to silence these warnings:

warning: constant in pattern `MidiMessageType_NoteOff` should have an upper case name such as `MIDI_MESSAGE_TYPE_NOTE_OFF`, #[warn(non_upper_case_globals)] on by default
  --> examples\test.rs:41:9
   |
41 |         MidiMessageType_NoteOff => println!("off"),
   |         ^^^^^^^^^^^^^^^^^^^^^^^

warning: constant in pattern `MidiMessageType_NoteOn` should have an upper case name such as `MIDI_MESSAGE_TYPE_NOTE_ON`, #[warn(non_upper_case_globals)] on by default
  --> examples\test.rs:42:9
   |
42 |         MidiMessageType_NoteOn => println!("on"),
   |         ^^^^^^^^^^^^^^^^^^^^^^
Boddlnagg commented 7 years ago

The only possible pain point is that you can't use associated constants (yet), i.e.

use wrt::windows::devices::midi::MidiMessageType::NoteOff;

doesn't work (Not a moduleMidiMessageType``).