Skip to content

Commit

Permalink
WIP: qtypes: add support for Qt alias types that don't match
Browse files Browse the repository at this point in the history
Some times don't match the Rust types so add these missing types.

Closes #882
  • Loading branch information
ahayzen-kdab committed Mar 28, 2024
1 parent 4e8c0ff commit 440f45b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/cxx-qt-lib-headers/include/core/qtypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <cstdint>

#include <QtCore/Qt>

#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);

}
}
1 change: 1 addition & 0 deletions crates/cxx-qt-lib-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts {
),
(include_str!("../include/core/qt.h"), "qt.h"),
(include_str!("../include/core/qtime.h"), "qtime.h"),
(include_str!("../include/core/qtypes.h"), "qtypes.h"),
(include_str!("../include/core/qtimezone.h"), "qtimezone.h"),
(include_str!("../include/core/qurl.h"), "qurl.h"),
(include_str!("../include/core/qvariant.h"), "qvariant.h"),
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn main() {
"core/qstringlist",
"core/qt",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/mod",
"core/qvariant/qvariant_bool",
Expand Down Expand Up @@ -192,6 +193,7 @@ fn main() {
"core/qstring",
"core/qstringlist",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/qvariant",
"core/qvector/qvector",
Expand Down
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
54 changes: 54 additions & 0 deletions crates/cxx-qt-lib/src/core/qtypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// clang-format off
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#include "cxx-qt-lib/qtypes.h"

#include "../assertion_utils.h"

assert_alignment_and_size(::qsizetype,
alignof(::std::size_t),
sizeof(::std::size_t));

assert_alignment_and_size(::qint64,
alignof(::std::int64_t),
sizeof(::std::int64_t));

// static_assert(!::std::is_trivially_copy_assignable<qsizetype>::value);
// static_assert(!::std::is_trivially_copy_constructible<qsizetype>::value);

// static_assert(!::std::is_trivially_destructible<qsizetype>::value);

// static_assert(QTypeInfo<qsizetype>::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);
}

}
}
83 changes: 83 additions & 0 deletions crates/cxx-qt-lib/src/core/qtypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// 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<i64>,
}

impl From<i64> for QInt64 {
fn from(value: i64) -> Self {
ffi::qint64_from_i64(value)
}
}

impl From<QInt64> 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<isize>,
}

impl From<isize> for QSizeType {
fn from(value: isize) -> Self {
ffi::qsizetype_from_isize(value)
}
}

impl From<QSizeType> 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;
}

0 comments on commit 440f45b

Please sign in to comment.