qtbridge_runtime/qrustproxy.rs
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use qtbridge_type_lib::{QMetaObject, QMetaType};
5use std::rc::Rc;
6use std::cell::RefCell;
7
8pub enum ConstructionMode {
9 Strong,
10 Weak,
11 AtAddress(*mut u8),
12}
13
14/// `QRustProxy` defines the Rust-side bridge object that binds:
15///
16/// - A Rust object stored in `Rc<RefCell<dyn _>>`
17/// - A corresponding C++ QObject-based proxy
18///
19/// Implementations of this trait are the concrete glue layer between
20/// Rust and Qt, usually using Cxx.
21///
22/// # Purpose
23///
24/// A `QRustProxy` implementation:
25///
26/// - Stores a raw pointer to the C++ proxy (`cpp_proxy`)
27/// - Stores access to the Rust object through `RustObjAccess<dyn _>` (`rust_obj`)
28/// - Coordinates destruction, layout and Qt meta-object information
29/// - Forwards all foreign function calls to the C++ proxy
30///
31/// Typical structure:
32///
33/// ```rust, ignore
34/// pub struct QObjectProxyRust {
35/// cpp_proxy: *mut QObjectProxyCpp,
36/// rust_obj: RustObjAccess<dyn QObjectProxyGet>,
37/// on_drop: fn(rust_obj: *const u8),
38/// }
39/// ```
40///
41/// Where:
42///
43/// - `cpp_proxy` points to the actual C++ QObject subclass.
44/// - `rust_obj` wraps access to the users rust object.
45/// - `on_drop` cleaning up memory.
46///
47/// # Associated Types
48///
49/// ## `ProxyCppType`
50///
51/// The concrete C++ proxy type.
52///
53/// ## `RcRefCellType`
54///
55/// The Rust container type holding the actual object, typically:
56///
57/// ```rust, ignore
58/// Rc<RefCell<dyn MarkerTrait>>
59/// ```
60/// The MarkerTrait is implemented on the users rust struct by the qobject macro.
61///
62pub trait QRustProxy {
63 type ProxyCppType;
64 type AdapterType: ?Sized;
65 fn new<OnDropFn: FnOnce() + 'static>(rust_obj: &Rc<RefCell<Self::AdapterType>>, construction: ConstructionMode, on_drop: OnDropFn) -> *mut Self;
66 fn get_static_meta_object() -> &'static QMetaObject;
67 fn get_size_of_cpp_proxy() -> usize;
68 fn get_align_of_cpp_proxy() -> usize;
69 fn get_qmetatype_list_of_cpp_proxy() -> QMetaType;
70 fn get_cpp_proxy(&self) -> *const Self::ProxyCppType;
71 fn get_cpp_proxy_mut(&self) -> *mut Self::ProxyCppType;
72}