KDAB / cxx-qt

Safe interop between Rust and Qt
https://kdab.github.io/cxx-qt/book/
973 stars 67 forks source link

Implement as_ref for overloads in cxx-qt-lib #901

Open Montel opened 3 months ago

Montel commented 3 months ago

Closed: #810

Montel commented 3 months ago
error[E0308]: mismatched types --> crates/cxx-qt-lib/src/gui/qpen.rs:186:9 184 fn as_ref(&self) -> &ffi::QPen { ---------- expected &qpen::QPen because of return type 185 let pen = ffi::qpen_init_from_qcolor(&self); 186 pen ^^^ expected &QPen, found QPen

help: consider borrowing here | 186 | &pen | +

For more information about this error, try rustc --explain E0308. The following warnings were emitted during compilation:

but if I use &pen if doesn't compile too.

Do you have an idea ? thanks

Montel commented 3 months ago

warning: cxx-qt-lib@0.6.0: Compiler version doesn't include clang or GCC: "c++" "--version" error[E0515]: cannot return reference to local variable pen --> crates/cxx-qt-lib/src/gui/qpen.rs:186:9 | 186 | &pen | ^^^^ returns a reference to data owned by the current function

For more information about this error, try rustc --explain E0515. The following warnings were emitted during compilation:

ahayzen-kdab commented 3 months ago

Fun :-) so because it wants a reference the lifetime is the fun part here. Either we would need to link up the lifetimes somehow (not sure if possible?) or maybe we'd need to go the Into route instead as that could be a value not a reference, eg accept Into<QPen> we also need to see what is the common way of doing this in the Rust ecosystem, so looking for examples in std would be good.

ahayzen-kdab commented 3 months ago

Also note that From<T> is usually preferred over Into<T>

Montel commented 3 months ago

@LeonMatthesKDAB do you have an idea how to fix it ? :)