Skip to content

Commit 440f45b

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 4e8c0ff commit 440f45b

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2023 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+
::qsizetype
24+
qsizetypeFromIsize(::rust::isize value);
25+
::rust::isize
26+
qsizetypeIntoIsize(qsizetype value);
27+
28+
}
29+
}

crates/cxx-qt-lib-headers/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts {
6161
),
6262
(include_str!("../include/core/qt.h"), "qt.h"),
6363
(include_str!("../include/core/qtime.h"), "qtime.h"),
64+
(include_str!("../include/core/qtypes.h"), "qtypes.h"),
6465
(include_str!("../include/core/qtimezone.h"), "qtimezone.h"),
6566
(include_str!("../include/core/qurl.h"), "qurl.h"),
6667
(include_str!("../include/core/qvariant.h"), "qvariant.h"),

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn main() {
7979
"core/qstringlist",
8080
"core/qt",
8181
"core/qtime",
82+
"core/qtypes",
8283
"core/qurl",
8384
"core/qvariant/mod",
8485
"core/qvariant/qvariant_bool",
@@ -192,6 +193,7 @@ fn main() {
192193
"core/qstring",
193194
"core/qstringlist",
194195
"core/qtime",
196+
"core/qtypes",
195197
"core/qurl",
196198
"core/qvariant/qvariant",
197199
"core/qvector/qvector",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ pub use qt::{
7878
mod qtime;
7979
pub use qtime::QTime;
8080

81+
mod qtypes;
82+
pub use qtypes::{QInt64, QSizeType};
83+
8184
#[cfg(not(target_os = "emscripten"))]
8285
mod qtimezone;
8386
#[cfg(not(target_os = "emscripten"))]

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 "../assertion_utils.h"
10+
11+
assert_alignment_and_size(::qsizetype,
12+
alignof(::std::size_t),
13+
sizeof(::std::size_t));
14+
15+
assert_alignment_and_size(::qint64,
16+
alignof(::std::int64_t),
17+
sizeof(::std::int64_t));
18+
19+
// static_assert(!::std::is_trivially_copy_assignable<qsizetype>::value);
20+
// static_assert(!::std::is_trivially_copy_constructible<qsizetype>::value);
21+
22+
// static_assert(!::std::is_trivially_destructible<qsizetype>::value);
23+
24+
// static_assert(QTypeInfo<qsizetype>::isRelocatable);
25+
26+
namespace rust {
27+
namespace cxxqtlib1 {
28+
29+
::qint64
30+
qint64FromI64(::std::int64_t value)
31+
{
32+
return static_cast<::qint64>(value);
33+
}
34+
35+
::std::int64_t
36+
qint64IntoI64(::qint64 value)
37+
{
38+
return static_cast<::std::int64_t>(value);
39+
}
40+
41+
::qsizetype
42+
qsizetypeFromIsize(::rust::isize value)
43+
{
44+
return static_cast<::qsizetype>(value);
45+
}
46+
47+
::rust::isize
48+
qsizetypeIntoIsize(::qsizetype value)
49+
{
50+
return static_cast<::rust::isize>(value);
51+
}
52+
53+
}
54+
}

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// SPDX-FileCopyrightText: 2023 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 = "qsizetype"]
18+
type QSizeType = super::QSizeType;
19+
}
20+
21+
#[namespace = "rust::cxxqtlib1"]
22+
unsafe extern "C++" {
23+
#[rust_name = "qint64_from_i64"]
24+
fn qint64FromI64(value: i64) -> QInt64;
25+
#[rust_name = "qint64_into_i64"]
26+
fn qint64IntoI64(value: QInt64) -> i64;
27+
28+
#[rust_name = "qsizetype_from_isize"]
29+
fn qsizetypeFromIsize(value: isize) -> QSizeType;
30+
#[rust_name = "qsizetype_into_isize"]
31+
fn qsizetypeIntoIsize(value: QSizeType) -> isize;
32+
}
33+
}
34+
35+
#[repr(C)]
36+
pub struct QInt64 {
37+
_space: MaybeUninit<i64>,
38+
}
39+
40+
impl From<i64> for QInt64 {
41+
fn from(value: i64) -> Self {
42+
ffi::qint64_from_i64(value)
43+
}
44+
}
45+
46+
impl From<QInt64> for i64 {
47+
fn from(value: QInt64) -> Self {
48+
ffi::qint64_into_i64(value)
49+
}
50+
}
51+
52+
// Safety:
53+
//
54+
// Static checks on the C++ side to ensure the size is the same.
55+
unsafe impl ExternType for QInt64 {
56+
type Id = type_id!("qint64");
57+
type Kind = cxx::kind::Trivial;
58+
}
59+
60+
#[repr(C)]
61+
pub struct QSizeType {
62+
_space: MaybeUninit<isize>,
63+
}
64+
65+
impl From<isize> for QSizeType {
66+
fn from(value: isize) -> Self {
67+
ffi::qsizetype_from_isize(value)
68+
}
69+
}
70+
71+
impl From<QSizeType> for isize {
72+
fn from(value: QSizeType) -> Self {
73+
ffi::qsizetype_into_isize(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 QSizeType {
81+
type Id = type_id!("qsizetype");
82+
type Kind = cxx::kind::Trivial;
83+
}

0 commit comments

Comments
 (0)