Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: qtypes: add support for Qt alias types that don't match #904

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/cxx-qt-lib-headers/include/core/qdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays);
QDate
qdateCurrentDate();
QDate
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);
Expand Down
28 changes: 4 additions & 24 deletions crates/cxx-qt-lib-headers/include/core/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,24 @@ struct IsRelocatable<QDateTime> : ::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<QTimeZone>
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);
}
Expand Down
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 <info@kdab.com>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024

// clang-format on
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// 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
10 changes: 2 additions & 8 deletions crates/cxx-qt-lib/src/core/qdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ static_assert(::std::is_trivially_copyable<QDate>::value,
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays)
{
return date.addDays(static_cast<qint64>(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
Expand Down
33 changes: 17 additions & 16 deletions crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ mod ffi {
type DateFormat = crate::DateFormat;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qtypes.h");
#[cxx_name = "qint64"]
type QInt64 = crate::QInt64;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;

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;
Expand Down Expand Up @@ -76,17 +88,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"]
Expand Down Expand Up @@ -147,13 +155,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()
Expand All @@ -162,7 +163,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)
}

Expand Down Expand Up @@ -271,15 +272,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")]
Expand Down
74 changes: 6 additions & 68 deletions crates/cxx-qt-lib/src/core/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@ static_assert(QTypeInfo<QDateTime>::isRelocatable);
namespace rust {
namespace cxxqtlib1 {

QDateTime
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays)
{
return datetime.addDays(static_cast<qint64>(ndays));
}

QDateTime
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs)
{
return datetime.addMSecs(static_cast<qint64>(msecs));
}

QDateTime
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs)
{
return datetime.addSecs(static_cast<qint64>(secs));
}

QDateTime
qdatetimeCurrentDateTime()
{
Expand All @@ -59,48 +41,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<qint64>(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<qint64>(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
Expand All @@ -109,18 +71,6 @@ qdatetimeSetDate(QDateTime& datetime, QDate date)
datetime.setDate(date);
}

void
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs)
{
datetime.setMSecsSinceEpoch(static_cast<qint64>(msecs));
}

void
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs)
{
datetime.setSecsSinceEpoch(static_cast<qint64>(secs));
}

void
qdatetimeSetTime(QDateTime& datetime, QTime time)
{
Expand All @@ -133,18 +83,6 @@ qdatetimeTimeZone(const QDateTime& datetime)
return ::std::make_unique<QTimeZone>(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)
{
Expand Down
Loading
Loading