Skip to content

Commit

Permalink
cxx-qt-gen: mangle free rust functions names in writer
Browse files Browse the repository at this point in the history
We need to do this to avoid collisions when there are multiple QObjects
  • Loading branch information
ahayzen-kdab committed Sep 15, 2022
1 parent 9a8afba commit 1693f58
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 33 deletions.
30 changes: 24 additions & 6 deletions crates/cxx-qt-gen/src/writer/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::generator::rust::GeneratedRustBlocks;
use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::Ident;

/// Mangle an input name with an object name
///
/// For now we need to do this to avoid free Rust methods from colliding
/// Once static methods are possible in CXX this could be removed
/// https://github.com/dtolnay/cxx/issues/447
fn mangle(name: &str, object: &Ident) -> Ident {
format_ident!(
"{}",
format!("{name}_{object}", name = name, object = object).to_case(Case::Snake)
)
}

/// Return common blocks for CXX bridge which the C++ writer adds as well
fn cxx_common_blocks(
cpp_struct_ident: &Ident,
rust_struct_ident: &Ident,
cxx_qt_thread_ident: &Ident,
namespace_internals: &String,
) -> Vec<TokenStream> {
let new_cpp_obj_str = mangle("new_cpp_object", cpp_struct_ident).to_string();
let create_rs_ident = mangle("create_rs", rust_struct_ident);

vec![
quote! {
unsafe extern "C++" {
Expand All @@ -37,7 +53,7 @@ fn cxx_common_blocks(
fn qt_thread(self: &#cpp_struct_ident) -> UniquePtr<#cxx_qt_thread_ident>;
fn queue(self: &#cxx_qt_thread_ident, func: fn(ctx: Pin<&mut #cpp_struct_ident>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = #new_cpp_obj_str]
#[namespace = #namespace_internals]
fn newCppObject() -> UniquePtr<#cpp_struct_ident>;
}
Expand All @@ -52,7 +68,7 @@ fn cxx_common_blocks(
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = #namespace_internals]
fn create_rs() -> Box<#rust_struct_ident>;
fn #create_rs_ident() -> Box<#rust_struct_ident>;
}
},
]
Expand Down Expand Up @@ -86,6 +102,8 @@ pub fn write_rust(generated: &GeneratedRustBlocks) -> TokenStream {
cxx_mod_items.push(syn::parse2(block).expect("Could not build CXX common block"));
}

let create_rs_ident = mangle("create_rs", rust_struct_ident);

quote! {
#[cxx::bridge(namespace = #namespace)]
#cxx_mod
Expand All @@ -100,7 +118,7 @@ pub fn write_rust(generated: &GeneratedRustBlocks) -> TokenStream {

#(#cxx_qt_mod_contents)*

pub fn create_rs() -> std::boxed::Box<#rust_struct_ident> {
pub fn #create_rs_ident() -> std::boxed::Box<#rust_struct_ident> {
std::default::Default::default()
}
}
Expand Down Expand Up @@ -182,7 +200,7 @@ mod tests {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -195,7 +213,7 @@ mod tests {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand All @@ -216,7 +234,7 @@ mod tests {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/custom_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -47,7 +47,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -75,7 +75,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -54,7 +54,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -105,7 +105,7 @@ pub mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -68,7 +68,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -60,7 +60,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/types_primitive_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -111,7 +111,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -313,7 +313,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/types_qt_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -112,7 +112,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -289,7 +289,7 @@ mod cxx_qt_ffi {
}
}

pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/types_qt_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod ffi {
fn qt_thread(self: &MyObjectQt) -> UniquePtr<MyObjectCxxQtThread>;
fn queue(self: &MyObjectCxxQtThread, func: fn(ctx: Pin<&mut MyObjectQt>)) -> Result<()>;

#[rust_name = "new_cpp_object"]
#[rust_name = "new_cpp_object_my_object_qt"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn newCppObject() -> UniquePtr<MyObjectQt>;
}
Expand All @@ -172,7 +172,7 @@ mod ffi {
extern "Rust" {
#[cxx_name = "createRs"]
#[namespace = "cxx_qt::my_object::cxx_qt_my_object"]
fn create_rs() -> Box<MyObject>;
fn create_rs_my_object() -> Box<MyObject>;
}
}

Expand Down Expand Up @@ -453,7 +453,7 @@ mod cxx_qt_ffi {
self.as_mut().emit_variant_changed();
}
}
pub fn create_rs() -> std::boxed::Box<MyObject> {
pub fn create_rs_my_object() -> std::boxed::Box<MyObject> {
std::default::Default::default()
}
}

0 comments on commit 1693f58

Please sign in to comment.