Skip to content

Commit

Permalink
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 KDAB#882
  • Loading branch information
ahayzen-kdab committed Nov 5, 2024
1 parent 4455c86 commit cc403c4
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.7.0...HEAD)

### Added

- Support for further types: `qreal`, `qint64`, `qintptr`, `qsizetype`, `quint64`, `quintptr`

## [0.7.0](https://github.com/KDAB/cxx-qt/compare/v0.6.1...v0.7.0) - 2024-10-30

### Added
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 @@ -147,6 +147,7 @@ fn main() {
"core/qstringlist",
"core/qt",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/mod",
"core/qvariant/qvariant_bool",
Expand Down Expand Up @@ -271,6 +272,7 @@ fn main() {
"core/qstring",
"core/qstringlist",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/qvariant",
"core/qvector/qvector",
Expand Down
9 changes: 9 additions & 0 deletions crates/cxx-qt-lib/include/core/qtypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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
#pragma once

#include <QtCore/Qt>
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 @@ -83,6 +83,9 @@ pub use qt::{
mod qtime;
pub use qtime::QTime;

mod qtypes;
pub use qtypes::{qint64, qintptr, qreal, qsizetype, quint64, quintptr};

#[cfg(not(target_os = "emscripten"))]
mod qtimezone;
#[cfg(not(target_os = "emscripten"))]
Expand Down
19 changes: 19 additions & 0 deletions crates/cxx-qt-lib/src/core/qtypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 <cstdint>

#include "cxx-qt-lib/assertion_utils.h"

assert_alignment_and_size(qint64, { ::std::int64_t a0; });
assert_alignment_and_size(qintptr, { ::std::intptr_t a0; });
assert_alignment_and_size(quint64, { ::std::uint64_t a0; });
assert_alignment_and_size(quintptr, { ::std::uintptr_t a0; });
assert_alignment_and_size(qsizetype, { ::std::size_t a0; });
// We only support qreal being a double
assert_alignment_and_size(qreal, { double a0; });
173 changes: 173 additions & 0 deletions crates/cxx-qt-lib/src/core/qtypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// SPDX-FileCopyrightText: 2024 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};

#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qtypes.h");
}
}

/// Typedef for long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub struct qint64(i64);

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

impl From<qint64> for i64 {
fn from(value: qint64) -> Self {
value.0
}
}

// 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;
}

/// Integral type for representing pointers in a signed integer (useful for hashing, etc.).
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub struct qintptr(isize);

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

impl From<qintptr> for isize {
fn from(value: qintptr) -> Self {
value.0
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
unsafe impl ExternType for qintptr {
type Id = type_id!("qintptr");
type Kind = cxx::kind::Trivial;
}

/// Typedef for double
///
/// Note that configuring Qt with -qreal float is not supported
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, PartialEq, PartialOrd)]
#[allow(non_camel_case_types)]
pub struct qreal(f64);

impl From<f64> for qreal {
fn from(value: f64) -> Self {
qreal(value)
}
}

impl From<qreal> for f64 {
fn from(value: qreal) -> Self {
value.0
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
unsafe impl ExternType for qreal {
type Id = type_id!("qreal");
type Kind = cxx::kind::Trivial;
}

/// Typedef for unsigned long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt.
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub struct quint64(u64);

impl From<u64> for quint64 {
fn from(value: u64) -> Self {
quint64(value)
}
}

impl From<quint64> for u64 {
fn from(value: quint64) -> Self {
value.0
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
unsafe impl ExternType for quint64 {
type Id = type_id!("quint64");
type Kind = cxx::kind::Trivial;
}

/// Integral type for representing pointers in an unsigned integer (useful for hashing, etc.).
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub struct quintptr(usize);

impl From<usize> for quintptr {
fn from(value: usize) -> Self {
quintptr(value)
}
}

impl From<quintptr> for usize {
fn from(value: quintptr) -> Self {
value.0
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
unsafe impl ExternType for quintptr {
type Id = type_id!("quintptr");
type Kind = cxx::kind::Trivial;
}

/// Integral type providing Posix' ssize_t for all platforms.
///
/// This type is guaranteed to be the same size as a size_t on all platforms supported by Qt.
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
#[allow(non_camel_case_types)]
pub struct qsizetype(isize);

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

impl From<qsizetype> for isize {
fn from(value: qsizetype) -> Self {
value.0
}
}

// 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 cc403c4

Please sign in to comment.