Skip to content

Commit 4619a1b

Browse files
Add book entry
1 parent 17a4112 commit 4619a1b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

book/src/bridge/attributes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ For [`#[qproperty]`](./extern_rustqt.md#properties), a CXX or Rust name can be p
4343
The `#[auto_cxx_name]` and `#[auto_rust_name]` attributes can be used to automatically rename cxx and rust names.
4444
These are placed at a block level on `extern "RustQt"` or `extern "C++Qt"` blocks, and will automatically case convert the items inside, unless they specify either a `rust_name` or `cxx_name`.
4545
By default `#[auto_cxx_name]` will generate a camelCase conversion for`cxx_name` and `#[auto_rust_name]` will generate a snake_case conversion for `rust_name`.
46+
47+
### Automatic wrapping
48+
49+
A fairly common operation is calling a method on the inner rust type, via the `.rust()` accessor. This can be simplified
50+
with the `#[auto_wrap]` attribute. This will generate a wrapper for your function which accesses the rust method of that name.

crates/cxx-qt-gen/src/generator/rust/method.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ pub fn generate_rust_methods(
5656
Some(quote! { unsafe })
5757
};
5858

59+
let method_safety = if invokable.safe {
60+
None
61+
} else {
62+
Some(quote! { unsafe })
63+
};
64+
5965
let wrapper_fn = if invokable.wrap {
6066
vec![parse_quote_spanned! {
6167
invokable.method.span() =>
62-
#unsafe_call fn #invokable_ident_rust(#parameter_signatures) #return_type {
68+
#method_safety fn #invokable_ident_rust(#parameter_signatures) #return_type {
6369
self.rust().#invokable_ident_rust(#call_parameters)
6470
}
6571
}]

crates/cxx-qt-gen/test_outputs/passthrough_and_naming.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ cxx_qt::static_assertions::assert_eq_size!(
780780
cxx_qt::signalhandler::CxxQtSignalHandler<SecondObjectCxxQtSignalClosurepropertyNameChanged>,
781781
[usize; 2]
782782
);
783-
unsafe fn my_function(self: &SecondObject, param: i32) {
783+
fn my_function(self: &SecondObject, param: i32) {
784784
self.rust().my_function(param)
785785
}
786786
impl ffi::SecondObject {

0 commit comments

Comments
 (0)