Skip to content

Commit

Permalink
cxx-qt-gen: use original signal definition and don't rename
Browse files Browse the repository at this point in the history
This allows for passing through the original signal definition
to CXX and then removes camel <-> snake case conversion.

Related to #616
  • Loading branch information
ahayzen-kdab committed Aug 24, 2023
1 parent c815756 commit 14dfe02
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 53 deletions.
7 changes: 3 additions & 4 deletions crates/cxx-qt-gen/src/generator/naming/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::generator::naming::CombinedIdent;
use crate::parser::signals::ParsedSignal;
use convert_case::{Case, Casing};
use quote::format_ident;
use syn::Ident;

Expand All @@ -26,16 +25,16 @@ impl From<&ParsedSignal> for QSignalName {
}

fn on_from_signal(ident: &Ident) -> Ident {
format_ident!("on_{}", ident.to_string().to_case(Case::Snake))
format_ident!("on_{}", ident.to_string())
}

impl CombinedIdent {
fn connect_from_signal(ident: &CombinedIdent) -> Self {
Self {
// Use signalConnect instead of onSignal here so that we don't
// create a C++ name that is similar to the QML naming scheme for signals
cpp: format_ident!("{}Connect", ident.cpp.to_string().to_case(Case::Camel)),
rust: format_ident!("connect_{}", ident.rust.to_string().to_case(Case::Snake)),
cpp: format_ident!("{}Connect", ident.cpp.to_string()),
rust: format_ident!("connect_{}", ident.rust.to_string()),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/cxx-qt-gen/src/generator/rust/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ mod tests {
parse_quote! {
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "trivial_property_changed"]
fn trivialPropertyChanged(self: Pin<&mut MyObject>, );
#[cxx_name = "trivialPropertyChanged"]
fn trivial_property_changed(self: Pin<&mut MyObject>);
}
},
);
Expand Down Expand Up @@ -305,8 +305,8 @@ mod tests {
parse_quote! {
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "opaque_property_changed"]
fn opaquePropertyChanged(self: Pin<&mut MyObject>, );
#[cxx_name = "opaquePropertyChanged"]
fn opaque_property_changed(self: Pin<&mut MyObject>);
}
},
);
Expand Down Expand Up @@ -346,8 +346,8 @@ mod tests {
parse_quote! {
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "unsafe_property_changed"]
fn unsafePropertyChanged(self: Pin<&mut MyObject>, );
#[cxx_name = "unsafePropertyChanged"]
fn unsafe_property_changed(self: Pin<&mut MyObject>);
}
},
);
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-gen/src/generator/rust/property/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ pub fn generate(idents: &QPropertyName, qobject_idents: &QObjectName) -> ParsedS
// We build our signal in the generation phase as we need to use the naming
// structs to build the signal name
let cpp_class_rust = &qobject_idents.cpp_class.rust;
let notify_cpp_str = idents.notify.cpp.to_string();
let notify_rust = &idents.notify.rust;
let method: ForeignItemFn = syn::parse_quote! {
#[doc = "Notify for the Q_PROPERTY"]
#[cxx_name = #notify_cpp_str]
fn #notify_rust(self: Pin<&mut #cpp_class_rust>);
};
ParsedSignal::from_property_method(
Expand Down
27 changes: 12 additions & 15 deletions crates/cxx-qt-gen/src/generator/rust/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ pub fn generate_rust_signals(
// Create the methods for the other signals
for signal in signals {
let idents = QSignalName::from(signal);
let signal_name_rust = idents.name.rust;
let signal_name_rust_str = signal_name_rust.to_string();
let signal_name_cpp = idents.name.cpp;
let signal_name_cpp_str = signal_name_cpp.to_string();
let connect_ident_cpp = idents.connect_name.cpp;
let connect_ident_rust = idents.connect_name.rust;
let connect_ident_rust_str = connect_ident_rust.to_string();
let on_ident_rust = idents.on_name;
let original_method = &signal.method;

let parameters_cxx: Vec<FnArg> = signal
.parameters
Expand Down Expand Up @@ -73,15 +72,11 @@ pub fn generate_rust_signals(
std::mem::swap(&mut unsafe_call, &mut unsafe_block);
}

let attrs = &signal.method.attrs;

let fragment = RustFragmentPair {
cxx_bridge: vec![
quote! {
#unsafe_block extern "C++" {
#(#attrs)*
#[rust_name = #signal_name_rust_str]
#unsafe_call fn #signal_name_cpp(self: #self_type_cxx, #(#parameters_cxx),*);
#original_method
}
},
quote! {
Expand Down Expand Up @@ -164,8 +159,7 @@ mod tests {
&generated.cxx_mod_contents[0],
quote! {
unsafe extern "C++" {
#[rust_name = "ready"]
fn ready(self: Pin<&mut MyObject>, );
fn ready(self: Pin<&mut MyObject>);
}
},
);
Expand Down Expand Up @@ -206,6 +200,7 @@ mod tests {
let qsignal = ParsedSignal {
method: parse_quote! {
#[attribute]
#[cxx_name = "dataChanged"]
fn data_changed(self: Pin<&mut MyObject>, trivial: i32, opaque: UniquePtr<QColor>);
},
qobject_ident: format_ident!("MyObject"),
Expand Down Expand Up @@ -244,8 +239,8 @@ mod tests {
quote! {
unsafe extern "C++" {
#[attribute]
#[rust_name = "data_changed"]
fn dataChanged(self: Pin<&mut MyObject>, trivial: i32, opaque: UniquePtr<QColor>);
#[cxx_name = "dataChanged"]
fn data_changed(self: Pin<&mut MyObject>, trivial: i32, opaque: UniquePtr<QColor>);
}
},
);
Expand Down Expand Up @@ -285,6 +280,7 @@ mod tests {
fn test_generate_rust_signal_unsafe() {
let qsignal = ParsedSignal {
method: parse_quote! {
#[cxx_name = "unsafeSignal"]
unsafe fn unsafe_signal(self: Pin<&mut MyObject>, param: *mut T);
},
qobject_ident: format_ident!("MyObject"),
Expand Down Expand Up @@ -316,8 +312,8 @@ mod tests {
&generated.cxx_mod_contents[0],
quote! {
extern "C++" {
#[rust_name = "unsafe_signal"]
unsafe fn unsafeSignal(self: Pin<&mut MyObject>, param: *mut T);
#[cxx_name = "unsafeSignal"]
unsafe fn unsafe_signal(self: Pin<&mut MyObject>, param: *mut T);
}
},
);
Expand Down Expand Up @@ -358,6 +354,7 @@ mod tests {
let qsignal = ParsedSignal {
method: parse_quote! {
#[inherit]
#[cxx_name = "baseName"]
fn existing_signal(self: Pin<&mut MyObject>, );
},
qobject_ident: format_ident!("MyObject"),
Expand Down Expand Up @@ -387,8 +384,8 @@ mod tests {
quote! {
unsafe extern "C++" {
#[inherit]
#[rust_name = "existing_signal"]
fn baseName(self: Pin<&mut MyObject>, );
#[cxx_name = "baseName"]
fn existing_signal(self: Pin<&mut MyObject>, );
}
},
);
Expand Down
5 changes: 3 additions & 2 deletions crates/cxx-qt-gen/test_inputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ mod ffi {
type MyObject = super::MyObjectRust;

#[qsignal]
fn ready(self: Pin<&mut MyObject>);
fn dataReady(self: Pin<&mut MyObject>);

#[qsignal]
fn data_changed(
#[rust_name = "data_changed"]
fn dataChanged(
self: Pin<&mut MyObject>,
first: i32,
second: UniquePtr<Opaque>,
Expand Down
10 changes: 4 additions & 6 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub mod ffi {
}
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "property_name_changed"]
fn propertyNameChanged(self: Pin<&mut MyObject>);
#[cxx_name = "propertyNameChanged"]
fn property_name_changed(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[doc = "Connect the given function pointer to the signal "]
Expand All @@ -105,7 +105,6 @@ pub mod ffi {
fn invokable_name(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[rust_name = "ready"]
fn ready(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
Expand Down Expand Up @@ -158,8 +157,8 @@ pub mod ffi {
}
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "property_name_changed"]
fn propertyNameChanged(self: Pin<&mut SecondObject>);
#[cxx_name = "propertyNameChanged"]
fn property_name_changed(self: Pin<&mut SecondObject>);
}
unsafe extern "C++" {
#[doc = "Connect the given function pointer to the signal "]
Expand All @@ -180,7 +179,6 @@ pub mod ffi {
}
unsafe extern "C++" {
#[my_attribute]
#[rust_name = "ready"]
fn ready(self: Pin<&mut SecondObject>);
}
unsafe extern "C++" {
Expand Down
8 changes: 4 additions & 4 deletions crates/cxx-qt-gen/test_outputs/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ mod ffi {
}
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "primitive_changed"]
fn primitiveChanged(self: Pin<&mut MyObject>);
#[cxx_name = "primitiveChanged"]
fn primitive_changed(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[doc = "Connect the given function pointer to the signal "]
Expand All @@ -69,8 +69,8 @@ mod ffi {
}
unsafe extern "C++" {
#[doc = "Notify for the Q_PROPERTY"]
#[rust_name = "trivial_changed"]
fn trivialChanged(self: Pin<&mut MyObject>);
#[cxx_name = "trivialChanged"]
fn trivial_changed(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[doc = "Connect the given function pointer to the signal "]
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ MyObject::invokable()
}

::QMetaObject::Connection
MyObject::readyConnect(::rust::Fn<void(MyObject&)> func,
::Qt::ConnectionType type)
MyObject::dataReadyConnect(::rust::Fn<void(MyObject&)> func,
::Qt::ConnectionType type)
{
return ::QObject::connect(
this,
&MyObject::ready,
&MyObject::dataReady,
this,
[&, func = ::std::move(func)]() {
const ::rust::cxxqtlib1::MaybeLockGuard<MyObject> guard(*this);
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class MyObject

public:
Q_INVOKABLE void invokable();
Q_SIGNAL void ready();
::QMetaObject::Connection readyConnect(::rust::Fn<void(MyObject&)> func,
::Qt::ConnectionType type);
Q_SIGNAL void dataReady();
::QMetaObject::Connection dataReadyConnect(::rust::Fn<void(MyObject&)> func,
::Qt::ConnectionType type);
Q_SIGNAL void dataChanged(::std::int32_t first,
::std::unique_ptr<Opaque> second,
QPoint third,
Expand Down
18 changes: 8 additions & 10 deletions crates/cxx-qt-gen/test_outputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ mod ffi {
fn invokable(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[rust_name = "ready"]
fn ready(self: Pin<&mut MyObject>);
fn dataReady(self: Pin<&mut MyObject>);
}
unsafe extern "C++" {
#[doc = "Connect the given function pointer to the signal "]
#[doc = "ready"]
#[doc = "dataReady"]
#[doc = ", so that when the signal is emitted the function pointer is executed."]
#[must_use]
#[rust_name = "connect_ready"]
fn readyConnect(
#[rust_name = "connect_dataReady"]
fn dataReadyConnect(
self: Pin<&mut MyObject>,
func: fn(Pin<&mut MyObject>),
conn_type: CxxQtConnectionType,
Expand Down Expand Up @@ -85,8 +84,7 @@ mod ffi {
}
unsafe extern "C++" {
#[cxx_name = "newData"]
#[rust_name = "base_class_new_data"]
fn newData(
fn base_class_new_data(
self: Pin<&mut MyObject>,
first: i32,
second: UniquePtr<Opaque>,
Expand Down Expand Up @@ -130,16 +128,16 @@ mod ffi {
}
impl ffi::MyObject {
#[doc = "Connect the given function pointer to the signal "]
#[doc = "ready"]
#[doc = "dataReady"]
#[doc = ", so that when the signal is emitted the function pointer is executed."]
#[doc = "\n"]
#[doc = "Note that this method uses a AutoConnection connection type."]
#[must_use]
pub fn on_ready(
pub fn on_dataReady(
self: core::pin::Pin<&mut ffi::MyObject>,
func: fn(core::pin::Pin<&mut ffi::MyObject>),
) -> cxx_qt_lib::QMetaObjectConnection {
self.connect_ready(func, cxx_qt_lib::ConnectionType::AutoConnection)
self.connect_dataReady(func, cxx_qt_lib::ConnectionType::AutoConnection)
}
}
impl ffi::MyObject {
Expand Down
3 changes: 3 additions & 0 deletions examples/demo_threading/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ pub mod qobject {
unsafe extern "RustQt" {
/// A new sensor has been detected
#[qsignal]
#[cxx_name = "sensorAdded"]
fn sensor_added(self: Pin<&mut EnergyUsage>, uuid: QString);
/// A value on an existing sensor has changed
#[qsignal]
#[cxx_name = "sensorChanged"]
fn sensor_changed(self: Pin<&mut EnergyUsage>, uuid: QString);
/// An existing sensor has been removed
#[qsignal]
#[cxx_name = "sensorRemoved"]
fn sensor_removed(self: Pin<&mut EnergyUsage>, uuid: QString);
}

Expand Down
1 change: 1 addition & 0 deletions examples/qml_features/rust/src/custom_base_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub mod qobject {
/// Inherit the DataChanged signal from the QAbstractListModel base class
#[inherit]
#[qsignal]
#[cxx_name = "dataChanged"]
fn data_changed(
self: Pin<&mut CustomBaseClass>,
top_left: &QModelIndex,
Expand Down

0 comments on commit 14dfe02

Please sign in to comment.