From 85961d162b7afa17ccacb76af6815495ad345c84 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Thu, 28 Mar 2024 16:34:55 +0000 Subject: [PATCH] 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 --- crates/cxx-qt-lib/build.rs | 2 + crates/cxx-qt-lib/include/core/qtypes.h | 29 +++++++++ crates/cxx-qt-lib/src/core/mod.rs | 3 + crates/cxx-qt-lib/src/core/qtypes.cpp | 60 ++++++++++++++++++ crates/cxx-qt-lib/src/core/qtypes.rs | 83 +++++++++++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 crates/cxx-qt-lib/include/core/qtypes.h create mode 100644 crates/cxx-qt-lib/src/core/qtypes.cpp create mode 100644 crates/cxx-qt-lib/src/core/qtypes.rs diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index 93d3fbbb9..16733e153 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -136,6 +136,7 @@ fn main() { "core/qstringlist", "core/qt", "core/qtime", + "core/qtypes", "core/qurl", "core/qvariant/mod", "core/qvariant/qvariant_bool", @@ -254,6 +255,7 @@ fn main() { "core/qstring", "core/qstringlist", "core/qtime", + "core/qtypes", "core/qurl", "core/qvariant/qvariant", "core/qvector/qvector", diff --git a/crates/cxx-qt-lib/include/core/qtypes.h b/crates/cxx-qt-lib/include/core/qtypes.h new file mode 100644 index 000000000..f29d7c887 --- /dev/null +++ b/crates/cxx-qt-lib/include/core/qtypes.h @@ -0,0 +1,29 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include + +#include + +#include "rust/cxx.h" + +namespace rust { +namespace cxxqtlib1 { + +::qint64 +qint64FromI64(::std::int64_t value); +::std::int64_t +qint64IntoI64(::qint64 value); + +::qsizetype +qsizetypeFromIsize(::rust::isize value); +::rust::isize +qsizetypeIntoIsize(qsizetype value); + +} +} diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index 7f8d82a7e..cc8e328b6 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -78,6 +78,9 @@ pub use qt::{ mod qtime; pub use qtime::QTime; +mod qtypes; +pub use qtypes::{QInt64, QSizeType}; + #[cfg(not(target_os = "emscripten"))] mod qtimezone; #[cfg(not(target_os = "emscripten"))] diff --git a/crates/cxx-qt-lib/src/core/qtypes.cpp b/crates/cxx-qt-lib/src/core/qtypes.cpp new file mode 100644 index 000000000..5ebfa2c93 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.cpp @@ -0,0 +1,60 @@ +// clang-format off +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#include "cxx-qt-lib/qtypes.h" + +#include "../assertion_utils.h" + +constexpr static ::std::array<::std::size_t, 1> arrSizeType{ sizeof( + ::std::size_t) }; +assert_alignment_and_size(::qsizetype, + alignof(::std::size_t), + arrSizeType, + arrSizeType.size()); + +constexpr static ::std::array<::std::size_t, 1> arrInt64{ sizeof( + ::std::int64_t) }; +assert_alignment_and_size(::qint64, + alignof(::std::int64_t), + arrInt64, + arrInt64.size()); + +// static_assert(!::std::is_trivially_copy_assignable::value); +// static_assert(!::std::is_trivially_copy_constructible::value); + +// static_assert(!::std::is_trivially_destructible::value); + +// static_assert(QTypeInfo::isRelocatable); + +namespace rust { +namespace cxxqtlib1 { + +::qint64 +qint64FromI64(::std::int64_t value) +{ + return static_cast<::qint64>(value); +} + +::std::int64_t +qint64IntoI64(::qint64 value) +{ + return static_cast<::std::int64_t>(value); +} + +::qsizetype +qsizetypeFromIsize(::rust::isize value) +{ + return static_cast<::qsizetype>(value); +} + +::rust::isize +qsizetypeIntoIsize(::qsizetype value) +{ + return static_cast<::rust::isize>(value); +} + +} +} diff --git a/crates/cxx-qt-lib/src/core/qtypes.rs b/crates/cxx-qt-lib/src/core/qtypes.rs new file mode 100644 index 000000000..1ae66ae4d --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.rs @@ -0,0 +1,83 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx::{type_id, ExternType}; +use std::mem::MaybeUninit; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qtypes.h"); + + #[cxx_name = "qint64"] + type QInt64 = super::QInt64; + + #[cxx_name = "qsizetype"] + type QSizeType = super::QSizeType; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + #[rust_name = "qint64_from_i64"] + fn qint64FromI64(value: i64) -> QInt64; + #[rust_name = "qint64_into_i64"] + fn qint64IntoI64(value: QInt64) -> i64; + + #[rust_name = "qsizetype_from_isize"] + fn qsizetypeFromIsize(value: isize) -> QSizeType; + #[rust_name = "qsizetype_into_isize"] + fn qsizetypeIntoIsize(value: QSizeType) -> isize; + } +} + +#[repr(C)] +pub struct QInt64 { + _space: MaybeUninit, +} + +impl From for QInt64 { + fn from(value: i64) -> Self { + ffi::qint64_from_i64(value) + } +} + +impl From for i64 { + fn from(value: QInt64) -> Self { + ffi::qint64_into_i64(value) + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for QInt64 { + type Id = type_id!("qint64"); + type Kind = cxx::kind::Trivial; +} + +#[repr(C)] +pub struct QSizeType { + _space: MaybeUninit, +} + +impl From for QSizeType { + fn from(value: isize) -> Self { + ffi::qsizetype_from_isize(value) + } +} + +impl From for isize { + fn from(value: QSizeType) -> Self { + ffi::qsizetype_into_isize(value) + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for QSizeType { + type Id = type_id!("qsizetype"); + type Kind = cxx::kind::Trivial; +}