From 05550ddd108d88cc1aebfa0191258a99306aa881 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Thu, 28 Mar 2024 16:34:55 +0000 Subject: [PATCH 1/3] 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 --- CHANGELOG.md | 4 + crates/cxx-qt-lib/build.rs | 2 + crates/cxx-qt-lib/include/core/qtypes.h | 9 ++ crates/cxx-qt-lib/src/core/mod.rs | 3 + crates/cxx-qt-lib/src/core/qtypes.cpp | 19 +++ crates/cxx-qt-lib/src/core/qtypes.rs | 173 ++++++++++++++++++++++++ 6 files changed, 210 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/CHANGELOG.md b/CHANGELOG.md index 3bc1058a8..01b25520d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index bf4132961..9f1e76ab2 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -147,6 +147,7 @@ fn main() { "core/qstringlist", "core/qt", "core/qtime", + "core/qtypes", "core/qurl", "core/qvariant/mod", "core/qvariant/qvariant_bool", @@ -271,6 +272,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..16239f4e6 --- /dev/null +++ b/crates/cxx-qt-lib/include/core/qtypes.h @@ -0,0 +1,9 @@ +// 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 +#pragma once + +#include diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index a2e9bfa33..123b9bbda 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -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"))] 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..883a0150b --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.cpp @@ -0,0 +1,19 @@ +// 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 + +#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; }); 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..3da130aba --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.rs @@ -0,0 +1,173 @@ +// SPDX-FileCopyrightText: 2024 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}; + +#[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 for qint64 { + fn from(value: i64) -> Self { + Self(value) + } +} + +impl From 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 for qintptr { + fn from(value: isize) -> Self { + qintptr(value) + } +} + +impl From 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 for qreal { + fn from(value: f64) -> Self { + qreal(value) + } +} + +impl From 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 for quint64 { + fn from(value: u64) -> Self { + quint64(value) + } +} + +impl From 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 for quintptr { + fn from(value: usize) -> Self { + quintptr(value) + } +} + +impl From 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 for qsizetype { + fn from(value: isize) -> Self { + qsizetype(value) + } +} + +impl From 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; +} From 1190bded63e1449affe3588b9a7de36925152980 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Thu, 28 Mar 2024 17:17:04 +0000 Subject: [PATCH 2/3] cxx-qt-lib: use qint64 type for QDate and QDateTime --- crates/cxx-qt-lib/include/core/qdate.h | 4 +- crates/cxx-qt-lib/include/core/qdatetime.h | 28 +--- crates/cxx-qt-lib/src/core/qdate.cpp | 10 +- crates/cxx-qt-lib/src/core/qdate.rs | 32 ++--- crates/cxx-qt-lib/src/core/qdatetime.cpp | 74 +--------- crates/cxx-qt-lib/src/core/qdatetime.rs | 153 ++++++++------------- 6 files changed, 88 insertions(+), 213 deletions(-) diff --git a/crates/cxx-qt-lib/include/core/qdate.h b/crates/cxx-qt-lib/include/core/qdate.h index ad26d9e1f..75b849258 100644 --- a/crates/cxx-qt-lib/include/core/qdate.h +++ b/crates/cxx-qt-lib/include/core/qdate.h @@ -15,8 +15,6 @@ namespace rust { namespace cxxqtlib1 { -QDate -qdateAddDays(const QDate& date, ::std::int64_t ndays); QDate qdateCurrentDate(); QDate @@ -24,7 +22,7 @@ qdateFromString(const QString& string, const QString& format); QDate qdateFromString(const QString& string, Qt::DateFormat format); // In Qt 5 d is const-ref, in Qt 6 it is value -::std::int64_t +qint64 qdateDaysTo(const QDate& date, QDate d); bool qdateIsLeapYear(::std::int32_t year); diff --git a/crates/cxx-qt-lib/include/core/qdatetime.h b/crates/cxx-qt-lib/include/core/qdatetime.h index 9c0530962..fc4048996 100644 --- a/crates/cxx-qt-lib/include/core/qdatetime.h +++ b/crates/cxx-qt-lib/include/core/qdatetime.h @@ -28,44 +28,24 @@ struct IsRelocatable : ::std::true_type namespace rust { namespace cxxqtlib1 { -QDateTime -qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays); -QDateTime -qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs); -QDateTime -qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs); QDateTime qdatetimeCurrentDateTime(); QDateTime qdatetimeCurrentDateTimeUtc(); -::std::int64_t +qint64 qdatetimeCurrentMSecsSinceEpoch(); -::std::int64_t +qint64 qdatetimeCurrentSecsSinceEpoch(); -::std::int64_t -qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other); QDateTime -qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone); +qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone); QDateTime -qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone); -::std::int64_t -qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other); -::std::int64_t -qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other); +qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone); void qdatetimeSetDate(QDateTime& datetime, QDate date); void -qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs); -void -qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs); -void qdatetimeSetTime(QDateTime& datetime, QTime time); ::std::unique_ptr qdatetimeTimeZone(const QDateTime& datetime); -::std::int64_t -qdatetimeToMSecsSinceEpoch(const QDateTime& datetime); -::std::int64_t -qdatetimeToSecsSinceEpoch(const QDateTime& datetime); void qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone); } diff --git a/crates/cxx-qt-lib/src/core/qdate.cpp b/crates/cxx-qt-lib/src/core/qdate.cpp index c5658f404..fc05a6387 100644 --- a/crates/cxx-qt-lib/src/core/qdate.cpp +++ b/crates/cxx-qt-lib/src/core/qdate.cpp @@ -21,23 +21,17 @@ static_assert(::std::is_trivially_copyable::value, namespace rust { namespace cxxqtlib1 { -QDate -qdateAddDays(const QDate& date, ::std::int64_t ndays) -{ - return date.addDays(static_cast(ndays)); -} - QDate qdateCurrentDate() { return QDate::currentDate(); } -::std::int64_t +qint64 qdateDaysTo(const QDate& date, QDate d) { // In Qt 5 d is const-ref, in Qt 6 it is value - return static_cast<::std::int64_t>(date.daysTo(d)); + return date.daysTo(d); } QDate diff --git a/crates/cxx-qt-lib/src/core/qdate.rs b/crates/cxx-qt-lib/src/core/qdate.rs index a07e80dbb..89127c03f 100644 --- a/crates/cxx-qt-lib/src/core/qdate.rs +++ b/crates/cxx-qt-lib/src/core/qdate.rs @@ -14,6 +14,11 @@ mod ffi { type DateFormat = crate::DateFormat; } + unsafe extern "C++" { + include!("cxx-qt-lib/qtypes.h"); + type qint64 = crate::qint64; + } + unsafe extern "C++" { include!("cxx-qt-lib/qstring.h"); type QString = crate::QString; @@ -21,6 +26,12 @@ mod ffi { include!("cxx-qt-lib/qdate.h"); type QDate = super::QDate; + /// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative). + /// + /// Returns a null date if the current date is invalid or the new date is out of range. + #[rust_name = "add_days"] + fn addDays(self: &QDate, ndays: qint64) -> QDate; + /// Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative). #[rust_name = "add_months"] fn addMonths(self: &QDate, nmonths: i32) -> QDate; @@ -76,17 +87,13 @@ mod ffi { #[namespace = "rust::cxxqtlib1"] unsafe extern "C++" { - #[doc(hidden)] - #[rust_name = "qdate_add_days"] - fn qdateAddDays(date: &QDate, ndays: i64) -> QDate; - #[doc(hidden)] #[rust_name = "qdate_current_date"] fn qdateCurrentDate() -> QDate; #[doc(hidden)] #[rust_name = "qdate_days_to"] - fn qdateDaysTo(date: &QDate, d: QDate) -> i64; + fn qdateDaysTo(date: &QDate, d: QDate) -> qint64; #[doc(hidden)] #[rust_name = "qdate_from_string"] @@ -147,13 +154,6 @@ impl fmt::Debug for QDate { } impl QDate { - /// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative). - /// - /// Returns a null date if the current date is invalid or the new date is out of range. - pub fn add_days(&self, ndays: i64) -> Self { - ffi::qdate_add_days(self, ndays) - } - // Returns the current date, as reported by the system clock. pub fn current_date() -> Self { ffi::qdate_current_date() @@ -162,7 +162,7 @@ impl QDate { /// Returns the number of days from this date to d (which is negative if d is earlier than this date). /// /// Returns 0 if either date is invalid. - pub fn days_to(&self, date: Self) -> i64 { + pub fn days_to(&self, date: Self) -> ffi::qint64 { ffi::qdate_days_to(self, date) } @@ -271,15 +271,15 @@ mod test { #[test] fn qdate_current_date() { let date_a = QDate::current_date(); - let date_b = date_a.add_days(100); - assert_eq!(date_a.days_to(date_b), 100); + let date_b = date_a.add_days(100.into()); + assert_eq!(i64::from(date_a.days_to(date_b)), 100); } #[test] fn qdate_julian_day() { let date_a = QDate::from_julian_day(1000); let date_b = QDate::from_julian_day(1010); - assert_eq!(date_a.days_to(date_b), 10); + assert_eq!(i64::from(date_a.days_to(date_b)), 10); } #[cfg(feature = "chrono")] diff --git a/crates/cxx-qt-lib/src/core/qdatetime.cpp b/crates/cxx-qt-lib/src/core/qdatetime.cpp index 74ac9c0c7..007ad5223 100644 --- a/crates/cxx-qt-lib/src/core/qdatetime.cpp +++ b/crates/cxx-qt-lib/src/core/qdatetime.cpp @@ -27,24 +27,6 @@ static_assert(QTypeInfo::isRelocatable); namespace rust { namespace cxxqtlib1 { -QDateTime -qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays) -{ - return datetime.addDays(static_cast(ndays)); -} - -QDateTime -qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs) -{ - return datetime.addMSecs(static_cast(msecs)); -} - -QDateTime -qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs) -{ - return datetime.addSecs(static_cast(secs)); -} - QDateTime qdatetimeCurrentDateTime() { @@ -57,48 +39,28 @@ qdatetimeCurrentDateTimeUtc() return QDateTime::currentDateTimeUtc(); } -::std::int64_t +qint64 qdatetimeCurrentMSecsSinceEpoch() { return QDateTime::currentMSecsSinceEpoch(); } -::std::int64_t +qint64 qdatetimeCurrentSecsSinceEpoch() { return QDateTime::currentSecsSinceEpoch(); } -::std::int64_t -qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other) -{ - return static_cast<::std::int64_t>(datetime.daysTo(other)); -} - QDateTime -qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone) +qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone) { - return QDateTime::fromMSecsSinceEpoch(static_cast(msecs), timeZone); + return QDateTime::fromMSecsSinceEpoch(msecs, timeZone); } QDateTime -qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone) +qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone) { - return QDateTime::fromSecsSinceEpoch(static_cast(secs), timeZone); -} - -::std::int64_t -qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other) -{ - - return static_cast<::std::int64_t>(datetime.msecsTo(other)); -} - -::std::int64_t -qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other) -{ - - return static_cast<::std::int64_t>(datetime.secsTo(other)); + return QDateTime::fromSecsSinceEpoch(secs, timeZone); } void @@ -107,18 +69,6 @@ qdatetimeSetDate(QDateTime& datetime, QDate date) datetime.setDate(date); } -void -qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs) -{ - datetime.setMSecsSinceEpoch(static_cast(msecs)); -} - -void -qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs) -{ - datetime.setSecsSinceEpoch(static_cast(secs)); -} - void qdatetimeSetTime(QDateTime& datetime, QTime time) { @@ -131,18 +81,6 @@ qdatetimeTimeZone(const QDateTime& datetime) return ::std::make_unique(datetime.timeZone()); } -::std::int64_t -qdatetimeToMSecsSinceEpoch(const QDateTime& datetime) -{ - return static_cast<::std::int64_t>(datetime.toMSecsSinceEpoch()); -} - -::std::int64_t -qdatetimeToSecsSinceEpoch(const QDateTime& datetime) -{ - return static_cast<::std::int64_t>(datetime.toSecsSinceEpoch()); -} - void qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone) { diff --git a/crates/cxx-qt-lib/src/core/qdatetime.rs b/crates/cxx-qt-lib/src/core/qdatetime.rs index 6ef13f910..1894979a5 100644 --- a/crates/cxx-qt-lib/src/core/qdatetime.rs +++ b/crates/cxx-qt-lib/src/core/qdatetime.rs @@ -16,6 +16,11 @@ mod ffi { type TimeSpec = crate::TimeSpec; } + unsafe extern "C++" { + include!("cxx-qt-lib/qtypes.h"); + type qint64 = crate::qint64; + } + unsafe extern "C++" { include!("cxx-qt-lib/qdate.h"); type QDate = crate::QDate; @@ -28,10 +33,22 @@ mod ffi { include!("cxx-qt-lib/qtimezone.h"); type QTimeZone = crate::QTimeZone; + /// Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (or earlier if ndays is negative). + #[rust_name = "add_days"] + fn addDays(self: &QDateTime, ndays: qint64) -> QDateTime; + /// Returns a QDateTime object containing a datetime nmonths months later than the datetime of this object (or earlier if nmonths is negative). #[rust_name = "add_months"] fn addMonths(self: &QDateTime, nmonths: i32) -> QDateTime; + /// Returns a QDateTime object containing a datetime msecs milliseconds later than the datetime of this object (or earlier if msecs is negative). + #[rust_name = "add_msecs"] + fn addMSecs(self: &QDateTime, msecs: qint64) -> QDateTime; + + /// Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or earlier if s is negative). + #[rust_name = "add_secs"] + fn addSecs(self: &QDateTime, secs: qint64) -> QDateTime; + /// Returns a QDateTime object containing a datetime nyears years later than the datetime of this object (or earlier if nyears is negative). #[rust_name = "add_years"] fn addYears(self: &QDateTime, nyears: i32) -> QDateTime; @@ -39,6 +56,12 @@ mod ffi { /// Returns the date part of the datetime. fn date(self: &QDateTime) -> QDate; + /// Returns the number of days from this datetime to the other datetime. + /// The number of days is counted as the number of times midnight is reached between this datetime to the other datetime. + /// This means that a 10 minute difference from 23:55 to 0:05 the next day counts as one day. + #[rust_name = "days_to"] + fn daysTo(self: &QDateTime, other: &QDateTime) -> qint64; + /// Returns if this datetime falls in Daylight-Saving Time. #[rust_name = "is_daylight_time"] fn isDaylightTime(self: &QDateTime) -> bool; @@ -55,6 +78,21 @@ mod ffi { #[rust_name = "offset_from_utc"] fn offsetFromUtc(self: &QDateTime) -> i32; + /// Returns the number of milliseconds from this datetime to the other datetime. + /// If the other datetime is earlier than this datetime, the value returned is negative. + #[rust_name = "msecs_to"] + fn msecsTo(self: &QDateTime, other: &QDateTime) -> qint64; + + /// Returns the number of seconds from this datetime to the other datetime. + /// If the other datetime is earlier than this datetime, the value returned is negative. + #[rust_name = "secs_to"] + fn secsTo(self: &QDateTime, other: &QDateTime) -> qint64; + + /// Sets the date and time given the number of milliseconds msecs that have passed since 1970-01-01T00:00:00.000, + /// Coordinated Universal Time (Qt::UTC). On systems that do not support time zones this function will behave as if local time were Qt::UTC. + #[rust_name = "set_msecs_since_epoch"] + fn setMSecsSinceEpoch(self: &mut QDateTime, msecs: qint64); + /// Sets the timeSpec() to Qt::OffsetFromUTC and the offset to offsetSeconds. /// /// Note this method is only available with Qt < 6.8 @@ -62,6 +100,11 @@ mod ffi { #[rust_name = "set_offset_from_utc"] fn setOffsetFromUtc(self: &mut QDateTime, offset_seconds: i32); + /// Sets the date and time given the number of seconds secs that have passed since 1970-01-01T00:00:00.000, + /// Coordinated Universal Time (Qt::UTC). On systems that do not support time zones this function will behave as if local time were Qt::UTC. + #[rust_name = "set_secs_since_epoch"] + fn setSecsSinceEpoch(self: &mut QDateTime, secs: qint64); + /// Sets the time specification used in this datetime to spec. The datetime will refer to a different point in time. /// /// Note this method is only available with Qt < 6.8 @@ -84,10 +127,18 @@ mod ffi { #[rust_name = "to_local_time"] fn toLocalTime(self: &QDateTime) -> QDateTime; + /// Returns the datetime as the number of milliseconds that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). + #[rust_name = "to_msecs_since_epoch"] + fn toMSecsSinceEpoch(self: &QDateTime) -> qint64; + /// Returns a copy of this datetime converted to a spec of Qt::OffsetFromUTC with the given offsetSeconds. #[rust_name = "to_offset_from_utc"] fn toOffsetFromUtc(self: &QDateTime, offset_seconds: i32) -> QDateTime; + /// Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). + #[rust_name = "to_secs_since_epoch"] + fn toSecsSinceEpoch(self: &QDateTime) -> qint64; + /// Returns a copy of this datetime converted to the given time spec. /// /// Note this method is only available with Qt < 6.8 @@ -106,15 +157,6 @@ mod ffi { #[namespace = "rust::cxxqtlib1"] unsafe extern "C++" { - #[doc(hidden)] - #[rust_name = "qdatetime_add_days"] - fn qdatetimeAddDays(datetime: &QDateTime, ndays: i64) -> QDateTime; - #[doc(hidden)] - #[rust_name = "qdatetime_add_msecs"] - fn qdatetimeAddMSecs(datetime: &QDateTime, msecs: i64) -> QDateTime; - #[doc(hidden)] - #[rust_name = "qdatetime_add_secs"] - fn qdatetimeAddSecs(datetime: &QDateTime, secs: i64) -> QDateTime; #[doc(hidden)] #[rust_name = "qdatetime_current_date_time"] fn qdatetimeCurrentDateTime() -> QDateTime; @@ -123,25 +165,16 @@ mod ffi { fn qdatetimeCurrentDateTimeUtc() -> QDateTime; #[doc(hidden)] #[rust_name = "qdatetime_current_msecs_since_epoch"] - fn qdatetimeCurrentMSecsSinceEpoch() -> i64; + fn qdatetimeCurrentMSecsSinceEpoch() -> qint64; #[doc(hidden)] #[rust_name = "qdatetime_current_secs_since_epoch"] - fn qdatetimeCurrentSecsSinceEpoch() -> i64; - #[doc(hidden)] - #[rust_name = "qdatetime_days_to"] - fn qdatetimeDaysTo(datetime: &QDateTime, other: &QDateTime) -> i64; + fn qdatetimeCurrentSecsSinceEpoch() -> qint64; #[doc(hidden)] #[rust_name = "qdatetime_from_msecs_since_epoch"] - fn qdatetimeFromMSecsSinceEpoch(msecs: i64, time_zone: &QTimeZone) -> QDateTime; + fn qdatetimeFromMSecsSinceEpoch(msecs: qint64, time_zone: &QTimeZone) -> QDateTime; #[doc(hidden)] #[rust_name = "qdatetime_from_secs_since_epoch"] - fn qdatetimeFromSecsSinceEpoch(secs: i64, time_zone: &QTimeZone) -> QDateTime; - #[doc(hidden)] - #[rust_name = "qdatetime_msecs_to"] - fn qdatetimeMSecsTo(datetime: &QDateTime, other: &QDateTime) -> i64; - #[doc(hidden)] - #[rust_name = "qdatetime_secs_to"] - fn qdatetimeSecsTo(datetime: &QDateTime, other: &QDateTime) -> i64; + fn qdatetimeFromSecsSinceEpoch(secs: qint64, time_zone: &QTimeZone) -> QDateTime; // Note that Qt 5 takes const-ref and Qt 6 takes by-value // for QDateTime::setDate and QDateTime::setTime // @@ -150,23 +183,11 @@ mod ffi { #[rust_name = "qdatetime_set_date"] fn qdatetimeSetDate(datetime: &mut QDateTime, date: QDate); #[doc(hidden)] - #[rust_name = "qdatetime_set_msecs_since_epoch"] - fn qdatetimeSetMSecsSinceEpoch(datetime: &mut QDateTime, msecs: i64); - #[doc(hidden)] - #[rust_name = "qdatetime_set_secs_since_epoch"] - fn qdatetimeSetSecsSinceEpoch(datetime: &mut QDateTime, secs: i64); - #[doc(hidden)] #[rust_name = "qdatetime_set_time"] fn qdatetimeSetTime(datetime: &mut QDateTime, time: QTime); #[doc(hidden)] #[rust_name = "qdatetime_time_zone"] fn qdatetimeTimeZone(datetime: &QDateTime) -> UniquePtr; - #[doc(hidden)] - #[rust_name = "qdatetime_to_msecs_since_epoch"] - fn qdatetimeToMSecsSinceEpoch(datetime: &QDateTime) -> i64; - #[doc(hidden)] - #[rust_name = "qdatetime_to_secs_since_epoch"] - fn qdatetimeToSecsSinceEpoch(datetime: &QDateTime) -> i64; #[rust_name = "qdatetime_settimezone"] fn qdatetimeSetTimeZone(datetime: &mut QDateTime, time_zone: &QTimeZone); } @@ -221,21 +242,6 @@ impl QDateTime { ffi::qdatetime_settimezone(self, time_zone) } - /// Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (or earlier if ndays is negative). - pub fn add_days(&self, ndays: i64) -> Self { - ffi::qdatetime_add_days(self, ndays) - } - - /// Returns a QDateTime object containing a datetime msecs milliseconds later than the datetime of this object (or earlier if msecs is negative). - pub fn add_msecs(&self, msecs: i64) -> Self { - ffi::qdatetime_add_msecs(self, msecs) - } - - /// Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or earlier if s is negative). - pub fn add_secs(&self, secs: i64) -> Self { - ffi::qdatetime_add_secs(self, secs) - } - /// Returns the current datetime, as reported by the system clock, in the local time zone. pub fn current_date_time() -> Self { ffi::qdatetime_current_date_time() @@ -248,22 +254,15 @@ impl QDateTime { /// Returns the number of milliseconds since 1970-01-01T00:00:00 Universal Coordinated Time. /// This number is like the POSIX time_t variable, but expressed in milliseconds instead. - pub fn current_msecs_since_epoch() -> i64 { + pub fn current_msecs_since_epoch() -> ffi::qint64 { ffi::qdatetime_current_msecs_since_epoch() } /// Returns the number of seconds since 1970-01-01T00:00:00 Universal Coordinated Time. - pub fn current_secs_since_epoch() -> i64 { + pub fn current_secs_since_epoch() -> ffi::qint64 { ffi::qdatetime_current_secs_since_epoch() } - /// Returns the number of days from this datetime to the other datetime. - /// The number of days is counted as the number of times midnight is reached between this datetime to the other datetime. - /// This means that a 10 minute difference from 23:55 to 0:05 the next day counts as one day. - pub fn days_to(&self, other: &Self) -> i64 { - ffi::qdatetime_days_to(self, other) - } - /// Construct a Rust QDateTime from a given QDate, QTime, and QTimeZone pub fn from_date_and_time_time_zone( date: &QDate, @@ -288,46 +287,22 @@ impl QDateTime { /// Returns a datetime whose date and time are the number of milliseconds msecs that have passed since 1970-01-01T00:00:00.000, /// Coordinated Universal Time (Qt::UTC) and with the given timeZone. - pub fn from_msecs_since_epoch(msecs: i64, time_zone: &ffi::QTimeZone) -> Self { + pub fn from_msecs_since_epoch(msecs: ffi::qint64, time_zone: &ffi::QTimeZone) -> Self { ffi::qdatetime_from_msecs_since_epoch(msecs, time_zone) } /// Returns a datetime whose date and time are the number of seconds secs that have passed since 1970-01-01T00:00:00.000, /// Coordinated Universal Time (Qt::UTC) and converted to the given spec. - pub fn from_secs_since_epoch(secs: i64, time_zone: &ffi::QTimeZone) -> Self { + pub fn from_secs_since_epoch(secs: ffi::qint64, time_zone: &ffi::QTimeZone) -> Self { ffi::qdatetime_from_secs_since_epoch(secs, time_zone) } - /// Returns the number of milliseconds from this datetime to the other datetime. - /// If the other datetime is earlier than this datetime, the value returned is negative. - pub fn msecs_to(&self, other: &Self) -> i64 { - ffi::qdatetime_msecs_to(self, other) - } - - /// Returns the number of seconds from this datetime to the other datetime. - /// If the other datetime is earlier than this datetime, the value returned is negative. - pub fn secs_to(&self, other: &Self) -> i64 { - ffi::qdatetime_secs_to(self, other) - } - /// Sets the date part of this datetime to date. If no time is set yet, it is set to midnight. /// If date is invalid, this QDateTime becomes invalid. pub fn set_date(&mut self, date: QDate) { ffi::qdatetime_set_date(self, date); } - /// Sets the date and time given the number of milliseconds msecs that have passed since 1970-01-01T00:00:00.000, - /// Coordinated Universal Time (Qt::UTC). On systems that do not support time zones this function will behave as if local time were Qt::UTC. - pub fn set_msecs_since_epoch(&mut self, msecs: i64) { - ffi::qdatetime_set_msecs_since_epoch(self, msecs); - } - - /// Sets the date and time given the number of seconds secs that have passed since 1970-01-01T00:00:00.000, - /// Coordinated Universal Time (Qt::UTC). On systems that do not support time zones this function will behave as if local time were Qt::UTC. - pub fn set_secs_since_epoch(&mut self, secs: i64) { - ffi::qdatetime_set_secs_since_epoch(self, secs); - } - /// Sets the time part of this datetime to time. If time is not valid, this function sets it to midnight. /// Therefore, it's possible to clear any set time in a QDateTime by setting it to a default QTime. pub fn set_time(&mut self, time: QTime) { @@ -338,16 +313,6 @@ impl QDateTime { pub fn time_zone(&self) -> cxx::UniquePtr { ffi::qdatetime_time_zone(self) } - - /// Returns the datetime as the number of milliseconds that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). - pub fn to_msecs_since_epoch(&self) -> i64 { - ffi::qdatetime_to_msecs_since_epoch(self) - } - - /// Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). - pub fn to_secs_since_epoch(&self) -> i64 { - ffi::qdatetime_to_secs_since_epoch(self) - } } impl Clone for QDateTime { From 91759c23ccf72cdd7d59c100e214cdb3828fcece Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 11 Oct 2024 11:58:06 +0100 Subject: [PATCH 3/3] cxx-qt-lib: use quintptr for QModelIndex --- crates/cxx-qt-lib/include/core/qmodelindex.h | 9 --------- crates/cxx-qt-lib/src/core/qmodelindex.cpp | 13 ------------- crates/cxx-qt-lib/src/core/qmodelindex.rs | 17 ++++------------- .../qt_types_standalone/rust/src/qmodelindex.rs | 7 +++++-- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/crates/cxx-qt-lib/include/core/qmodelindex.h b/crates/cxx-qt-lib/include/core/qmodelindex.h index 11df7f320..f49b2bd3f 100644 --- a/crates/cxx-qt-lib/include/core/qmodelindex.h +++ b/crates/cxx-qt-lib/include/core/qmodelindex.h @@ -9,12 +9,3 @@ #include #include - -namespace rust { -namespace cxxqtlib1 { - -::std::size_t -qmodelindexInternalId(const QModelIndex& index); - -} -} \ No newline at end of file diff --git a/crates/cxx-qt-lib/src/core/qmodelindex.cpp b/crates/cxx-qt-lib/src/core/qmodelindex.cpp index 75aeb604f..9664d4155 100644 --- a/crates/cxx-qt-lib/src/core/qmodelindex.cpp +++ b/crates/cxx-qt-lib/src/core/qmodelindex.cpp @@ -19,16 +19,3 @@ assert_alignment_and_size(QModelIndex, { }); static_assert(::std::is_trivially_copyable::value); - -namespace rust { -namespace cxxqtlib1 { - -::std::size_t -qmodelindexInternalId(const QModelIndex& index) -{ - // TODO: need to add support for quintptr - return static_cast<::std::size_t>(index.internalId()); -} - -} -} diff --git a/crates/cxx-qt-lib/src/core/qmodelindex.rs b/crates/cxx-qt-lib/src/core/qmodelindex.rs index de029dabc..d6c291020 100644 --- a/crates/cxx-qt-lib/src/core/qmodelindex.rs +++ b/crates/cxx-qt-lib/src/core/qmodelindex.rs @@ -14,6 +14,7 @@ mod ffi { type QModelIndex = super::QModelIndex; type QString = crate::QString; + type quintptr = crate::quintptr; /// Returns the column this model index refers to. fn column(self: &QModelIndex) -> i32; @@ -35,6 +36,9 @@ mod ffi { #[rust_name = "sibling_at_row"] fn siblingAtRow(self: &QModelIndex, row: i32) -> QModelIndex; + /// Returns a `quintptr` used by the model to associate the index with the internal data structure. + #[rust_name = "internal_id"] + fn internalId(self: &QModelIndex) -> quintptr; /// Returns a `*mut c_void` pointer used by the model to associate the index with the internal data structure. #[rust_name = "internal_pointer_mut"] fn internalPointer(self: &QModelIndex) -> *mut c_void; @@ -54,10 +58,6 @@ mod ffi { #[doc(hidden)] #[rust_name = "qmodelindex_to_qstring"] fn toQString(value: &QModelIndex) -> QString; - - #[doc(hidden)] - #[rust_name = "qmodelindex_internal_id"] - fn qmodelindexInternalId(index: &QModelIndex) -> usize; } } @@ -71,15 +71,6 @@ pub struct QModelIndex { _m: MaybeUninit, } -impl QModelIndex { - /// Returns a `usize` used by the model to associate the index with the internal data structure. - // - // TODO: need to add support for quintptr - pub fn internal_id(&self) -> usize { - ffi::qmodelindex_internal_id(self) - } -} - impl Default for QModelIndex { /// Creates a new empty model index. This type of model index is used to indicate that the position in the model is invalid. fn default() -> Self { diff --git a/tests/qt_types_standalone/rust/src/qmodelindex.rs b/tests/qt_types_standalone/rust/src/qmodelindex.rs index 8ed12131c..92ff112bd 100644 --- a/tests/qt_types_standalone/rust/src/qmodelindex.rs +++ b/tests/qt_types_standalone/rust/src/qmodelindex.rs @@ -10,6 +10,9 @@ mod qmodelindex_cxx { unsafe extern "C++" { include!("cxx-qt-lib/qmodelindex.h"); type QModelIndex = cxx_qt_lib::QModelIndex; + + include!("cxx-qt-lib/qtypes.h"); + type quintptr = cxx_qt_lib::quintptr; } #[namespace = "rust::cxxqtlib1"] @@ -23,7 +26,7 @@ mod qmodelindex_cxx { fn read_qmodelindex(i: &QModelIndex) -> bool; fn clone_qmodelindex(i: &QModelIndex) -> QModelIndex; fn internal_pointer_qmodelindex(i: &QModelIndex) -> *mut c_void; - fn internal_id_qmodelindex(i: &QModelIndex) -> usize; + fn internal_id_qmodelindex(i: &QModelIndex) -> quintptr; } } @@ -43,6 +46,6 @@ fn internal_pointer_qmodelindex(i: &QModelIndex) -> *mut qmodelindex_cxx::c_void i.internal_pointer_mut() } -fn internal_id_qmodelindex(i: &QModelIndex) -> usize { +fn internal_id_qmodelindex(i: &QModelIndex) -> cxx_qt_lib::quintptr { i.internal_id() }