Skip to content

Commit b12929f

Browse files
committed
qtypes: add support for Qt alias types that don't match
Some times don't match the Rust types so add these missing types. Closes #882
1 parent 5229e31 commit b12929f

File tree

6 files changed

+207
-0
lines changed

6 files changed

+207
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- `QDateTime::from_string` to parse `QDateTime` from a `QString`.
2323
- Support for further types: `QUuid`
2424
- New example: Basic greeter app
25+
- Support for further types: `qreal`, `qint64`, `qintptr`, `qsizetype`, `quint64`, `quintptr`
2526

2627
### Fixed
2728

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ fn main() {
149149
"core/qstringlist",
150150
"core/qt",
151151
"core/qtime",
152+
"core/qtypes",
152153
"core/qurl",
153154
"core/quuid",
154155
"core/qvariant/mod",
@@ -276,6 +277,7 @@ fn main() {
276277
"core/qstring",
277278
"core/qstringlist",
278279
"core/qtime",
280+
"core/qtypes",
279281
"core/qurl",
280282
"core/quuid",
281283
"core/qvariant/qvariant",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#pragma once
8+
9+
#include <QtCore/Qt>

crates/cxx-qt-lib/src/core/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub use qt::{
8383
mod qtime;
8484
pub use qtime::QTime;
8585

86+
mod qtypes;
87+
pub use qtypes::{qint64, qintptr, qreal, qsizetype, quint64, quintptr};
88+
8689
#[cfg(not(target_os = "emscripten"))]
8790
mod qtimezone;
8891
#[cfg(not(target_os = "emscripten"))]

crates/cxx-qt-lib/src/core/qtypes.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#include "cxx-qt-lib/qtypes.h"
8+
9+
#include <cstdint>
10+
11+
#include "cxx-qt-lib/assertion_utils.h"
12+
13+
assert_alignment_and_size(qint64, { ::std::int64_t a0; });
14+
assert_alignment_and_size(qintptr, { ::std::intptr_t a0; });
15+
assert_alignment_and_size(quint64, { ::std::uint64_t a0; });
16+
assert_alignment_and_size(quintptr, { ::std::uintptr_t a0; });
17+
assert_alignment_and_size(qsizetype, { ::std::size_t a0; });
18+
// We only support qreal being a double
19+
assert_alignment_and_size(qreal, { double a0; });

crates/cxx-qt-lib/src/core/qtypes.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+
use cxx::{type_id, ExternType};
7+
8+
#[cxx::bridge]
9+
mod ffi {
10+
unsafe extern "C++" {
11+
include!("cxx-qt-lib/qtypes.h");
12+
}
13+
}
14+
15+
/// Typedef for long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
16+
#[repr(transparent)]
17+
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
18+
#[allow(non_camel_case_types)]
19+
pub struct qint64(i64);
20+
21+
impl From<i64> for qint64 {
22+
fn from(value: i64) -> Self {
23+
Self(value)
24+
}
25+
}
26+
27+
impl From<qint64> for i64 {
28+
fn from(value: qint64) -> Self {
29+
value.0
30+
}
31+
}
32+
33+
// Safety:
34+
//
35+
// Static checks on the C++ side to ensure the size is the same.
36+
unsafe impl ExternType for qint64 {
37+
type Id = type_id!("qint64");
38+
type Kind = cxx::kind::Trivial;
39+
}
40+
41+
/// Integral type for representing pointers in a signed integer (useful for hashing, etc.).
42+
#[repr(transparent)]
43+
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
44+
#[allow(non_camel_case_types)]
45+
pub struct qintptr(isize);
46+
47+
impl From<isize> for qintptr {
48+
fn from(value: isize) -> Self {
49+
qintptr(value)
50+
}
51+
}
52+
53+
impl From<qintptr> for isize {
54+
fn from(value: qintptr) -> Self {
55+
value.0
56+
}
57+
}
58+
59+
// Safety:
60+
//
61+
// Static checks on the C++ side to ensure the size is the same.
62+
unsafe impl ExternType for qintptr {
63+
type Id = type_id!("qintptr");
64+
type Kind = cxx::kind::Trivial;
65+
}
66+
67+
/// Typedef for double
68+
///
69+
/// Note that configuring Qt with -qreal float is not supported
70+
#[repr(transparent)]
71+
#[derive(Clone, Copy, Default, Debug, PartialEq, PartialOrd)]
72+
#[allow(non_camel_case_types)]
73+
pub struct qreal(f64);
74+
75+
impl From<f64> for qreal {
76+
fn from(value: f64) -> Self {
77+
qreal(value)
78+
}
79+
}
80+
81+
impl From<qreal> for f64 {
82+
fn from(value: qreal) -> Self {
83+
value.0
84+
}
85+
}
86+
87+
// Safety:
88+
//
89+
// Static checks on the C++ side to ensure the size is the same.
90+
unsafe impl ExternType for qreal {
91+
type Id = type_id!("qreal");
92+
type Kind = cxx::kind::Trivial;
93+
}
94+
95+
/// Typedef for unsigned long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
96+
#[repr(transparent)]
97+
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
98+
#[allow(non_camel_case_types)]
99+
pub struct quint64(u64);
100+
101+
impl From<u64> for quint64 {
102+
fn from(value: u64) -> Self {
103+
quint64(value)
104+
}
105+
}
106+
107+
impl From<quint64> for u64 {
108+
fn from(value: quint64) -> Self {
109+
value.0
110+
}
111+
}
112+
113+
// Safety:
114+
//
115+
// Static checks on the C++ side to ensure the size is the same.
116+
unsafe impl ExternType for quint64 {
117+
type Id = type_id!("quint64");
118+
type Kind = cxx::kind::Trivial;
119+
}
120+
121+
/// Integral type for representing pointers in an unsigned integer (useful for hashing, etc.).
122+
#[repr(transparent)]
123+
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
124+
#[allow(non_camel_case_types)]
125+
pub struct quintptr(usize);
126+
127+
impl From<usize> for quintptr {
128+
fn from(value: usize) -> Self {
129+
quintptr(value)
130+
}
131+
}
132+
133+
impl From<quintptr> for usize {
134+
fn from(value: quintptr) -> Self {
135+
value.0
136+
}
137+
}
138+
139+
// Safety:
140+
//
141+
// Static checks on the C++ side to ensure the size is the same.
142+
unsafe impl ExternType for quintptr {
143+
type Id = type_id!("quintptr");
144+
type Kind = cxx::kind::Trivial;
145+
}
146+
147+
/// Integral type providing Posix' ssize_t for all platforms.
148+
///
149+
/// This type is guaranteed to be the same size as a size_t on all platforms supported by Qt.
150+
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
151+
#[repr(transparent)]
152+
#[allow(non_camel_case_types)]
153+
pub struct qsizetype(isize);
154+
155+
impl From<isize> for qsizetype {
156+
fn from(value: isize) -> Self {
157+
qsizetype(value)
158+
}
159+
}
160+
161+
impl From<qsizetype> for isize {
162+
fn from(value: qsizetype) -> Self {
163+
value.0
164+
}
165+
}
166+
167+
// Safety:
168+
//
169+
// Static checks on the C++ side to ensure the size is the same.
170+
unsafe impl ExternType for qsizetype {
171+
type Id = type_id!("qsizetype");
172+
type Kind = cxx::kind::Trivial;
173+
}

0 commit comments

Comments
 (0)