Skip to content

Commit 85961d1

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 86d00e5 commit 85961d1

5 files changed

Lines changed: 177 additions & 0 deletions

File tree

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn main() {
136136
"core/qstringlist",
137137
"core/qt",
138138
"core/qtime",
139+
"core/qtypes",
139140
"core/qurl",
140141
"core/qvariant/mod",
141142
"core/qvariant/qvariant_bool",
@@ -254,6 +255,7 @@ fn main() {
254255
"core/qstring",
255256
"core/qstringlist",
256257
"core/qtime",
258+
"core/qtypes",
257259
"core/qurl",
258260
"core/qvariant/qvariant",
259261
"core/qvector/qvector",
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 <info@kdab.com>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
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/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"))]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
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+
constexpr static ::std::array<::std::size_t, 1> arrSizeType{ sizeof(
12+
::std::size_t) };
13+
assert_alignment_and_size(::qsizetype,
14+
alignof(::std::size_t),
15+
arrSizeType,
16+
arrSizeType.size());
17+
18+
constexpr static ::std::array<::std::size_t, 1> arrInt64{ sizeof(
19+
::std::int64_t) };
20+
assert_alignment_and_size(::qint64,
21+
alignof(::std::int64_t),
22+
arrInt64,
23+
arrInt64.size());
24+
25+
// static_assert(!::std::is_trivially_copy_assignable<qsizetype>::value);
26+
// static_assert(!::std::is_trivially_copy_constructible<qsizetype>::value);
27+
28+
// static_assert(!::std::is_trivially_destructible<qsizetype>::value);
29+
30+
// static_assert(QTypeInfo<qsizetype>::isRelocatable);
31+
32+
namespace rust {
33+
namespace cxxqtlib1 {
34+
35+
::qint64
36+
qint64FromI64(::std::int64_t value)
37+
{
38+
return static_cast<::qint64>(value);
39+
}
40+
41+
::std::int64_t
42+
qint64IntoI64(::qint64 value)
43+
{
44+
return static_cast<::std::int64_t>(value);
45+
}
46+
47+
::qsizetype
48+
qsizetypeFromIsize(::rust::isize value)
49+
{
50+
return static_cast<::qsizetype>(value);
51+
}
52+
53+
::rust::isize
54+
qsizetypeIntoIsize(::qsizetype value)
55+
{
56+
return static_cast<::rust::isize>(value);
57+
}
58+
59+
}
60+
}
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 <info@kdab.com>
2+
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
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)