White-Oak / qml-rust

QML (Qt Quick) bindings for Rust language
MIT License
205 stars 18 forks source link

add support for Q_LISTMODEL with only one member #16

Closed fneddy closed 7 years ago

fneddy commented 7 years ago

currently it is not possible to create a Q_LISTMODEL! with only one member:

Q_LISTMODEL!{
    pub QTestModel {
        name: String,
    }
}
error: expected one of `,` or `@`, found `)`
  --> <qml macros>:22:26
   |
22 | | ( $ ( $ rolename ) , * ) | {
   |                          ^

error: expected pattern, found `{`
  --> <qml macros>:22:30
   |
22 | | ( $ ( $ rolename ) , * ) | {
   |                              ^

error: expected expression, found statement (`let`)
  --> <qml macros>:23:1
   |
23 | let mut vec = Vec :: new (  ) ; $ ( vec . push ( $ rolename . into (  ) ) ; )
   | ^^^
   |
   = note: variable declaration using `let` is a statement

error: expected one of `,`, `->`, `.`, `<`, `?`, `break`, `continue`, `for`, `if`, `loop`, `match`, `move`, `return`, `unsafe`, `while`, or an operator, found `let`
  --> <qml macros>:23:1
   |
23 | let mut vec = Vec :: new (  ) ; $ ( vec . push ( $ rolename . into (  ) ) ; )
   | ^^^

error: expected one of `->`, `.`, `;`, `<`, `?`, `break`, `continue`, `for`, `if`, `loop`, `match`, `move`, `return`, `unsafe`, `while`, `}`, or an operator, found `let`
  --> <qml macros>:23:1
   |
23 | let mut vec = Vec :: new (  ) ; $ ( vec . push ( $ rolename . into (  ) ) ; )
   | ^^^

error: expected one of `const`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `)`
  --> <qml macros>:24:9
   |
24 | * vec } ) . collect :: < Vec < Vec < QVariant >> > (  ) ) }
   |         ^

error: Could not compile `z-machine`.

To learn more, run the command again with --verbose.
fneddy commented 7 years ago

this happens here. The macro parser somehow does not like the expand in the tuple with only one element $($rolename),* but strangely this $($rolename,)* expands without a problem, through it generates invalid rust code.

White-Oak commented 7 years ago

Yes, I stumbled upon the same, it's a strange issue, I found the somehow simmilar point here in docs if you Ctrl-F for impl<A> Clone for (A,) where A: Clone you'll see that the tuple with one element requires (A,) a comma. I'll ask around

White-Oak commented 7 years ago

Ah, that's because Rust can't unserstand if (expr) is a braced expression or a tuple.