Skip to content

Commit 824aa44

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 e6ee353 commit 824aa44

File tree

5 files changed

+319
-0
lines changed

5 files changed

+319
-0
lines changed

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ fn main() {
147147
"core/qstringlist",
148148
"core/qt",
149149
"core/qtime",
150+
"core/qtypes",
150151
"core/qurl",
151152
"core/qvariant/mod",
152153
"core/qvariant/qvariant_bool",
@@ -271,6 +272,7 @@ fn main() {
271272
"core/qstring",
272273
"core/qstringlist",
273274
"core/qtime",
275+
"core/qtypes",
274276
"core/qurl",
275277
"core/qvariant/qvariant",
276278
"core/qvector/qvector",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 <cstdint>
10+
11+
#include <QtCore/Qt>
12+
13+
#include "rust/cxx.h"
14+
15+
namespace rust {
16+
namespace cxxqtlib1 {
17+
18+
::qint64
19+
qint64FromI64(::std::int64_t value);
20+
::std::int64_t
21+
qint64IntoI64(::qint64 value);
22+
23+
::qintptr
24+
qintptrFromIsize(::rust::isize value);
25+
::rust::isize
26+
qintptrIntoIsize(qintptr value);
27+
28+
::quint64
29+
quint64FromU64(::std::uint64_t value);
30+
::std::uint64_t
31+
quint64IntoU64(::quint64 value);
32+
33+
::quintptr
34+
quintptrFromUsize(::rust::usize value);
35+
::rust::usize
36+
quintptrIntoUsize(quintptr value);
37+
38+
::qsizetype
39+
qsizetypeFromIsize(::rust::isize value);
40+
::rust::isize
41+
qsizetypeIntoIsize(qsizetype value);
42+
43+
}
44+
}

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, 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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 "cxx-qt-lib/assertion_utils.h"
10+
11+
assert_alignment_and_size(qint64, { ::std::int64_t a0; });
12+
assert_alignment_and_size(qintptr, { ::std::intptr_t a0; });
13+
assert_alignment_and_size(quint64, { ::std::uint64_t a0; });
14+
assert_alignment_and_size(quintptr, { ::std::uintptr_t a0; });
15+
assert_alignment_and_size(qsizetype, { ::std::size_t a0; });
16+
17+
namespace rust {
18+
namespace cxxqtlib1 {
19+
20+
::qint64
21+
qint64FromI64(::std::int64_t value)
22+
{
23+
return static_cast<::qint64>(value);
24+
}
25+
26+
::std::int64_t
27+
qint64IntoI64(::qint64 value)
28+
{
29+
return static_cast<::std::int64_t>(value);
30+
}
31+
32+
::qintptr
33+
qintptrFromIsize(::rust::isize value)
34+
{
35+
return static_cast<::qintptr>(value);
36+
}
37+
38+
::rust::isize
39+
qintptrIntoIsize(::qintptr value)
40+
{
41+
return static_cast<::rust::isize>(value);
42+
}
43+
44+
::quint64
45+
quint64FromU64(::std::uint64_t value)
46+
{
47+
return static_cast<::quint64>(value);
48+
}
49+
50+
::std::uint64_t
51+
quint64IntoU64(::quint64 value)
52+
{
53+
return static_cast<::std::uint64_t>(value);
54+
}
55+
56+
::quintptr
57+
quintptrFromUsize(::rust::usize value)
58+
{
59+
return static_cast<::quintptr>(value);
60+
}
61+
62+
::rust::usize
63+
quintptrIntoUsize(::quintptr value)
64+
{
65+
return static_cast<::rust::usize>(value);
66+
}
67+
68+
::qsizetype
69+
qsizetypeFromIsize(::rust::isize value)
70+
{
71+
return static_cast<::qsizetype>(value);
72+
}
73+
74+
::rust::isize
75+
qsizetypeIntoIsize(::qsizetype value)
76+
{
77+
return static_cast<::rust::isize>(value);
78+
}
79+
80+
}
81+
}

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

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
use std::mem::MaybeUninit;
8+
9+
#[cxx::bridge]
10+
mod ffi {
11+
unsafe extern "C++" {
12+
include!("cxx-qt-lib/qtypes.h");
13+
14+
#[cxx_name = "qint64"]
15+
type QInt64 = super::QInt64;
16+
17+
#[cxx_name = "qintptr"]
18+
type QIntPtr = super::QIntPtr;
19+
20+
#[cxx_name = "quint64"]
21+
type QUInt64 = super::QUInt64;
22+
23+
#[cxx_name = "quintptr"]
24+
type QUIntPtr = super::QUIntPtr;
25+
26+
#[cxx_name = "qsizetype"]
27+
type QSizeType = super::QSizeType;
28+
}
29+
30+
#[namespace = "rust::cxxqtlib1"]
31+
unsafe extern "C++" {
32+
#[rust_name = "qint64_from_i64"]
33+
fn qint64FromI64(value: i64) -> QInt64;
34+
#[rust_name = "qint64_into_i64"]
35+
fn qint64IntoI64(value: QInt64) -> i64;
36+
37+
#[rust_name = "qintptr_from_isize"]
38+
fn qintptrFromIsize(value: isize) -> QIntPtr;
39+
#[rust_name = "qintptr_into_isize"]
40+
fn qintptrIntoIsize(value: QIntPtr) -> isize;
41+
42+
#[rust_name = "quint64_from_u64"]
43+
fn quint64FromU64(value: u64) -> QUInt64;
44+
#[rust_name = "quint64_into_u64"]
45+
fn quint64IntoU64(value: QUInt64) -> u64;
46+
47+
#[rust_name = "quintptr_from_usize"]
48+
fn quintptrFromUsize(value: usize) -> QUIntPtr;
49+
#[rust_name = "quintptr_into_usize"]
50+
fn quintptrIntoUsize(value: QUIntPtr) -> usize;
51+
52+
#[rust_name = "qsizetype_from_isize"]
53+
fn qsizetypeFromIsize(value: isize) -> QSizeType;
54+
#[rust_name = "qsizetype_into_isize"]
55+
fn qsizetypeIntoIsize(value: QSizeType) -> isize;
56+
}
57+
}
58+
59+
/// Typedef for long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
60+
#[repr(C)]
61+
pub struct QInt64 {
62+
_space: MaybeUninit<i64>,
63+
}
64+
65+
impl From<i64> for QInt64 {
66+
fn from(value: i64) -> Self {
67+
ffi::qint64_from_i64(value)
68+
}
69+
}
70+
71+
impl From<QInt64> for i64 {
72+
fn from(value: QInt64) -> Self {
73+
ffi::qint64_into_i64(value)
74+
}
75+
}
76+
77+
// Safety:
78+
//
79+
// Static checks on the C++ side to ensure the size is the same.
80+
unsafe impl ExternType for QInt64 {
81+
type Id = type_id!("qint64");
82+
type Kind = cxx::kind::Trivial;
83+
}
84+
85+
/// Integral type for representing pointers in a signed integer (useful for hashing, etc.).
86+
#[repr(C)]
87+
pub struct QIntPtr {
88+
_space: MaybeUninit<isize>,
89+
}
90+
91+
impl From<isize> for QIntPtr {
92+
fn from(value: isize) -> Self {
93+
ffi::qintptr_from_isize(value)
94+
}
95+
}
96+
97+
impl From<QIntPtr> for isize {
98+
fn from(value: QIntPtr) -> Self {
99+
ffi::qintptr_into_isize(value)
100+
}
101+
}
102+
103+
// Safety:
104+
//
105+
// Static checks on the C++ side to ensure the size is the same.
106+
unsafe impl ExternType for QIntPtr {
107+
type Id = type_id!("qintptr");
108+
type Kind = cxx::kind::Trivial;
109+
}
110+
111+
/// Typedef for unsigned long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
112+
#[repr(C)]
113+
pub struct QUInt64 {
114+
_space: MaybeUninit<u64>,
115+
}
116+
117+
impl From<u64> for QUInt64 {
118+
fn from(value: u64) -> Self {
119+
ffi::quint64_from_u64(value)
120+
}
121+
}
122+
123+
impl From<QUInt64> for u64 {
124+
fn from(value: QUInt64) -> Self {
125+
ffi::quint64_into_u64(value)
126+
}
127+
}
128+
129+
// Safety:
130+
//
131+
// Static checks on the C++ side to ensure the size is the same.
132+
unsafe impl ExternType for QUInt64 {
133+
type Id = type_id!("quint64");
134+
type Kind = cxx::kind::Trivial;
135+
}
136+
137+
/// Integral type for representing pointers in an unsigned integer (useful for hashing, etc.).
138+
#[repr(C)]
139+
pub struct QUIntPtr {
140+
_space: MaybeUninit<usize>,
141+
}
142+
143+
impl From<usize> for QUIntPtr {
144+
fn from(value: usize) -> Self {
145+
ffi::quintptr_from_usize(value)
146+
}
147+
}
148+
149+
impl From<QUIntPtr> for usize {
150+
fn from(value: QUIntPtr) -> Self {
151+
ffi::quintptr_into_usize(value)
152+
}
153+
}
154+
155+
// Safety:
156+
//
157+
// Static checks on the C++ side to ensure the size is the same.
158+
unsafe impl ExternType for QUIntPtr {
159+
type Id = type_id!("quintptr");
160+
type Kind = cxx::kind::Trivial;
161+
}
162+
163+
/// Integral type providing Posix' ssize_t for all platforms.
164+
///
165+
/// This type is guaranteed to be the same size as a size_t on all platforms supported by Qt.
166+
#[repr(C)]
167+
pub struct QSizeType {
168+
_space: MaybeUninit<isize>,
169+
}
170+
171+
impl From<isize> for QSizeType {
172+
fn from(value: isize) -> Self {
173+
ffi::qsizetype_from_isize(value)
174+
}
175+
}
176+
177+
impl From<QSizeType> for isize {
178+
fn from(value: QSizeType) -> Self {
179+
ffi::qsizetype_into_isize(value)
180+
}
181+
}
182+
183+
// Safety:
184+
//
185+
// Static checks on the C++ side to ensure the size is the same.
186+
unsafe impl ExternType for QSizeType {
187+
type Id = type_id!("qsizetype");
188+
type Kind = cxx::kind::Trivial;
189+
}

0 commit comments

Comments
 (0)