Skip to main content

cxx_qt_lib/core/
qobjectmutptr.rs

1// SPDX-FileCopyrightText: 2026 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Yuri Knigavko <yuri.knigavko@qt.io>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use cxx_qt::QObject;
7
8/// A thin wrapper around `*mut QObject`.
9/// Using the 'newtype' idiom lets us implement `ExternType`
10/// for it, which wouldn't be possible for pure `*mut QObject` itself.
11#[repr(transparent)]
12#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13pub struct QObjectMutPtr(*mut QObject);
14
15impl QObjectMutPtr {
16    /// Create a new instance from a raw pointer.
17    ///
18    /// # Safety
19    ///
20    /// The pointer is passed to C++ as a `QObject*` and may be dereferenced
21    /// there. This wrapper tracks neither ownership nor lifetime,
22    /// so the object must outlive every use of the underlying QObject pointer
23    /// (including any copy stored in a `QVariant` or `QList`).
24    pub unsafe fn from_raw(raw: *mut QObject) -> Self {
25        Self(raw)
26    }
27
28    /// Return a wrapped raw const pointer.
29    pub fn as_ptr(&self) -> *const QObject {
30        self.0
31    }
32
33    /// Return a wrapped raw mut pointer.
34    pub fn as_mut_ptr(&self) -> *mut QObject {
35        self.0
36    }
37
38    /// Consume an object a return a wrapped raw mut pointer.
39    pub fn into_raw(self) -> *mut QObject {
40        self.0
41    }
42}
43
44unsafe impl cxx::ExternType for QObjectMutPtr {
45    type Id = cxx::type_id!("QObjectMutPtr");
46    type Kind = cxx::kind::Trivial;
47}