Skip to content

Commit cf3e876

Browse files
committed
WIP: 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 cf3e876

File tree

5 files changed

+312
-0
lines changed

5 files changed

+312
-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: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
#[repr(C)]
60+
pub struct QInt64 {
61+
_space: MaybeUninit<i64>,
62+
}
63+
64+
impl From<i64> for QInt64 {
65+
fn from(value: i64) -> Self {
66+
ffi::qint64_from_i64(value)
67+
}
68+
}
69+
70+
impl From<QInt64> for i64 {
71+
fn from(value: QInt64) -> Self {
72+
ffi::qint64_into_i64(value)
73+
}
74+
}
75+
76+
// Safety:
77+
//
78+
// Static checks on the C++ side to ensure the size is the same.
79+
unsafe impl ExternType for QInt64 {
80+
type Id = type_id!("qint64");
81+
type Kind = cxx::kind::Trivial;
82+
}
83+
84+
#[repr(C)]
85+
pub struct QIntPtr {
86+
_space: MaybeUninit<isize>,
87+
}
88+
89+
impl From<isize> for QIntPtr {
90+
fn from(value: isize) -> Self {
91+
ffi::qintptr_from_isize(value)
92+
}
93+
}
94+
95+
impl From<QIntPtr> for isize {
96+
fn from(value: QIntPtr) -> Self {
97+
ffi::qintptr_into_isize(value)
98+
}
99+
}
100+
101+
// Safety:
102+
//
103+
// Static checks on the C++ side to ensure the size is the same.
104+
unsafe impl ExternType for QIntPtr {
105+
type Id = type_id!("qintptr");
106+
type Kind = cxx::kind::Trivial;
107+
}
108+
109+
#[repr(C)]
110+
pub struct QUInt64 {
111+
_space: MaybeUninit<u64>,
112+
}
113+
114+
impl From<u64> for QUInt64 {
115+
fn from(value: u64) -> Self {
116+
ffi::quint64_from_u64(value)
117+
}
118+
}
119+
120+
impl From<QUInt64> for u64 {
121+
fn from(value: QUInt64) -> Self {
122+
ffi::quint64_into_u64(value)
123+
}
124+
}
125+
126+
// Safety:
127+
//
128+
// Static checks on the C++ side to ensure the size is the same.
129+
unsafe impl ExternType for QUInt64 {
130+
type Id = type_id!("quint64");
131+
type Kind = cxx::kind::Trivial;
132+
}
133+
134+
#[repr(C)]
135+
pub struct QUIntPtr {
136+
_space: MaybeUninit<usize>,
137+
}
138+
139+
impl From<usize> for QUIntPtr {
140+
fn from(value: usize) -> Self {
141+
ffi::quintptr_from_usize(value)
142+
}
143+
}
144+
145+
impl From<QUIntPtr> for usize {
146+
fn from(value: QUIntPtr) -> Self {
147+
ffi::quintptr_into_usize(value)
148+
}
149+
}
150+
151+
// Safety:
152+
//
153+
// Static checks on the C++ side to ensure the size is the same.
154+
unsafe impl ExternType for QUIntPtr {
155+
type Id = type_id!("quintptr");
156+
type Kind = cxx::kind::Trivial;
157+
}
158+
159+
#[repr(C)]
160+
pub struct QSizeType {
161+
_space: MaybeUninit<isize>,
162+
}
163+
164+
impl From<isize> for QSizeType {
165+
fn from(value: isize) -> Self {
166+
ffi::qsizetype_from_isize(value)
167+
}
168+
}
169+
170+
impl From<QSizeType> for isize {
171+
fn from(value: QSizeType) -> Self {
172+
ffi::qsizetype_into_isize(value)
173+
}
174+
}
175+
176+
// Safety:
177+
//
178+
// Static checks on the C++ side to ensure the size is the same.
179+
unsafe impl ExternType for QSizeType {
180+
type Id = type_id!("qsizetype");
181+
type Kind = cxx::kind::Trivial;
182+
}

0 commit comments

Comments
 (0)