Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare Signals that emit references #311

Closed
LeonMatthesKDAB opened this issue Oct 6, 2022 · 0 comments · Fixed by #312
Closed

Declare Signals that emit references #311

LeonMatthesKDAB opened this issue Oct 6, 2022 · 0 comments · Fixed by #312
Assignees
Milestone

Comments

@LeonMatthesKDAB
Copy link
Collaborator

As of #281, Signals will be emitted immediately, which theoretically makes dealing with references much easier.

One can easily imagine an API like this:

#[cxx_qt::signals(MyObject)]
pub enum Signals<'x> {
    TestSignal { argument: &'x QVariant }
}

This however, doesn't work, even in #281.

error[E0261]: use of undeclared lifetime name `'x`
  --> examples/qml_features/rust/src/signals.rs:23:31
   |
7  | #[cxx_qt::bridge(cxx_file_stem = "signals")]
   |                                             -
   |                                             |
   |                                             lifetime `'x` is missing in item created through this procedural macro
   |                                             help: consider introducing lifetime `'x` here: `<'x>`
...
23 |         TestSignal { argument: &'x QVariant },
   |

This is caused by this generated code:

impl SignalsQt {
            pub fn emit_test_signal(
                self: ::std::pin::Pin<&mut Self>,
                argument: &'x QVariant,
            ) {
                extern "C" {
                    #[link_name = "cxxbridge1$Signals$emit_test_signal"]
                    fn __emit_test_signal(
                        _: ::std::pin::Pin<&mut SignalsQt>,
                        argument: *const ::cxx::core::ffi::c_void,
                    );
                }
                unsafe {
                    __emit_test_signal(
                        self,
                        argument as *const QVariant as *const ::cxx::core::ffi::c_void,
                    )
                }
            }
        }
}

The fix would be either to declare the 'x lifetime in emit_test_signal function in the CXX bridge.
Alternatively we could try removing the lifetime from the CXX Bridge, as that should be able to be deferred.

Furthermore, the emit function may cause problems, as it's defined as:

pub fn emit(self: Pin<&mut Self>, signal: Signal) {
                match signal {
                    Signal::TestSignal { argument } => self.emit_test_signal(emit),
                }
            }

But it may have to be defined as:

pub fn emit<'x>(self: Pin<&mut Self>, signal: Signal<'x>) {
                ...
            }
@ahayzen-kdab ahayzen-kdab self-assigned this Oct 6, 2022
@ahayzen-kdab ahayzen-kdab added this to the 0.4 milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants