From e84662a5e8e8dc649b3e2b4fe4298e0dc74026e8 Mon Sep 17 00:00:00 2001 From: Joshua Booth Date: Tue, 14 Jan 2025 01:36:30 -0800 Subject: [PATCH] cxx-qt-lib: Add support for QUuid (#1149) --------- Co-authored-by: Leon Matthes --- CHANGELOG.md | 1 + crates/cxx-qt-lib/Cargo.toml | 4 +- crates/cxx-qt-lib/build.rs | 6 + crates/cxx-qt-lib/include/core/qlist.h | 2 + crates/cxx-qt-lib/include/core/qset.h | 2 + crates/cxx-qt-lib/include/core/quuid.h | 40 ++ crates/cxx-qt-lib/include/core/qvariant.h | 2 + crates/cxx-qt-lib/include/core/qvector.h | 2 + crates/cxx-qt-lib/src/core/mod.rs | 3 + crates/cxx-qt-lib/src/core/qlist/generate.sh | 1 + crates/cxx-qt-lib/src/core/qlist/mod.rs | 3 +- crates/cxx-qt-lib/src/core/qlist/qlist.cpp | 1 + .../cxx-qt-lib/src/core/qlist/qlist_quuid.rs | 92 ++++ crates/cxx-qt-lib/src/core/qset/generate.sh | 1 + crates/cxx-qt-lib/src/core/qset/mod.rs | 3 +- crates/cxx-qt-lib/src/core/qset/qset.cpp | 1 + crates/cxx-qt-lib/src/core/qset/qset_quuid.rs | 70 +++ crates/cxx-qt-lib/src/core/quuid.cpp | 83 ++++ crates/cxx-qt-lib/src/core/quuid.rs | 460 ++++++++++++++++++ .../cxx-qt-lib/src/core/qvariant/generate.sh | 1 + crates/cxx-qt-lib/src/core/qvariant/mod.rs | 1 + .../cxx-qt-lib/src/core/qvariant/qvariant.cpp | 1 + .../src/core/qvariant/qvariant_quuid.rs | 37 ++ .../cxx-qt-lib/src/core/qvector/generate.sh | 1 + crates/cxx-qt-lib/src/core/qvector/mod.rs | 3 +- .../cxx-qt-lib/src/core/qvector/qvector.cpp | 1 + .../src/core/qvector/qvector_quuid.rs | 92 ++++ 27 files changed, 910 insertions(+), 4 deletions(-) create mode 100644 crates/cxx-qt-lib/include/core/quuid.h create mode 100644 crates/cxx-qt-lib/src/core/qlist/qlist_quuid.rs create mode 100644 crates/cxx-qt-lib/src/core/qset/qset_quuid.rs create mode 100644 crates/cxx-qt-lib/src/core/quuid.cpp create mode 100644 crates/cxx-qt-lib/src/core/quuid.rs create mode 100644 crates/cxx-qt-lib/src/core/qvariant/qvariant_quuid.rs create mode 100644 crates/cxx-qt-lib/src/core/qvector/qvector_quuid.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index c57caf5ba..6c4b62060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `QDateTime::from_string` to parse `QDateTime` from a `QString`. +- Support for further types: `QUuid` ### Fixed diff --git a/crates/cxx-qt-lib/Cargo.toml b/crates/cxx-qt-lib/Cargo.toml index e59bd23fb..d6b534b44 100644 --- a/crates/cxx-qt-lib/Cargo.toml +++ b/crates/cxx-qt-lib/Cargo.toml @@ -29,6 +29,7 @@ http = { version = "1.0", optional = true } rgb = { version = "0.8", optional = true } time = { version = "0.3.20", optional = true } url = { version = "2.3", optional = true } +uuid = { version = "1.1.0", optional = true } serde = { version = "1", features=["derive"], optional = true } [build-dependencies] @@ -36,7 +37,7 @@ cxx-qt-build.workspace = true qt-build-utils.workspace = true [features] -full = ["qt_full", "serde", "url", "time", "rgb", "http", "chrono", "bytes"] +full = ["qt_full", "serde", "url", "uuid", "time", "rgb", "http", "chrono", "bytes"] default = [] qt_full = ["qt_gui", "qt_qml", "qt_quickcontrols"] @@ -51,6 +52,7 @@ rgb = ["dep:rgb"] time = ["dep:time"] url = ["dep:url"] serde = ["dep:serde"] +uuid = ["dep:uuid"] link_qt_object_files = ["cxx-qt-build/link_qt_object_files"] [lints] diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index bf4132961..2970c8484 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -110,6 +110,7 @@ fn main() { "core/qlist/qlist_qstring", "core/qlist/qlist_qtime", "core/qlist/qlist_qurl", + "core/qlist/qlist_quuid", "core/qlist/qlist_qvariant", "core/qlist/qlist_u8", "core/qlist/qlist_u16", @@ -137,6 +138,7 @@ fn main() { "core/qset/qset_qstring", "core/qset/qset_qtime", "core/qset/qset_qurl", + "core/qset/qset_quuid", "core/qset/qset_u8", "core/qset/qset_u16", "core/qset/qset_u32", @@ -148,6 +150,7 @@ fn main() { "core/qt", "core/qtime", "core/qurl", + "core/quuid", "core/qvariant/mod", "core/qvariant/qvariant_bool", "core/qvariant/qvariant_f32", @@ -170,6 +173,7 @@ fn main() { "core/qvariant/qvariant_qstringlist", "core/qvariant/qvariant_qtime", "core/qvariant/qvariant_qurl", + "core/qvariant/qvariant_quuid", "core/qvariant/qvariant_u8", "core/qvariant/qvariant_u16", "core/qvariant/qvariant_u32", @@ -197,6 +201,7 @@ fn main() { "core/qvector/qvector_qstring", "core/qvector/qvector_qtime", "core/qvector/qvector_qurl", + "core/qvector/qvector_quuid", "core/qvector/qvector_qvariant", "core/qvector/qvector_u8", "core/qvector/qvector_u16", @@ -272,6 +277,7 @@ fn main() { "core/qstringlist", "core/qtime", "core/qurl", + "core/quuid", "core/qvariant/qvariant", "core/qvector/qvector", ]; diff --git a/crates/cxx-qt-lib/include/core/qlist.h b/crates/cxx-qt-lib/include/core/qlist.h index a7227f0c3..b0b86091c 100644 --- a/crates/cxx-qt-lib/include/core/qlist.h +++ b/crates/cxx-qt-lib/include/core/qlist.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #ifdef CXX_QT_GUI_FEATURE @@ -170,6 +171,7 @@ using QList_QSizeF = QList<::QSizeF>; using QList_QString = QList<::QString>; using QList_QTime = QList<::QTime>; using QList_QUrl = QList<::QUrl>; +using QList_QUuid = QList<::QUuid>; using QList_QVariant = QList<::QVariant>; using QList_u8 = QList<::std::uint8_t>; using QList_u16 = QList<::std::uint16_t>; diff --git a/crates/cxx-qt-lib/include/core/qset.h b/crates/cxx-qt-lib/include/core/qset.h index d7f041b24..a92d474b1 100644 --- a/crates/cxx-qt-lib/include/core/qset.h +++ b/crates/cxx-qt-lib/include/core/qset.h @@ -17,6 +17,7 @@ #include #include #include +#include #include "rust/cxx.h" @@ -84,6 +85,7 @@ using QSet_QPersistentModelIndex = QSet<::QPersistentModelIndex>; using QSet_QString = QSet<::QString>; using QSet_QTime = QSet<::QTime>; using QSet_QUrl = QSet<::QUrl>; +using QSet_QUuid = QSet<::QUuid>; using QSet_u8 = QSet<::std::uint8_t>; using QSet_u16 = QSet<::std::uint16_t>; using QSet_u32 = QSet<::std::uint32_t>; diff --git a/crates/cxx-qt-lib/include/core/quuid.h b/crates/cxx-qt-lib/include/core/quuid.h new file mode 100644 index 000000000..c4ff8b724 --- /dev/null +++ b/crates/cxx-qt-lib/include/core/quuid.h @@ -0,0 +1,40 @@ +// clang-format off +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Joshua Booth +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include + +#include "rust/cxx.h" + +using QUuidVariant = QUuid::Variant; +using QUuidVersion = QUuid::Version; + +namespace rust { +namespace cxxqtlib1 { +QUuid +quuidCreateUuidV3(const QUuid& ns, ::rust::Slice slice); + +QUuid +quuidCreateUuid(); + +QUuid +quuidCreateUuidV5(const QUuid& ns, ::rust::Slice slice); + +QString +quuidToString(const QUuid& uuid); + +QUuid +quuidFromString(const QString& string); + +QUuid +quuidFromStr(rust::Str string); + +QUuid +quuidFromRfc4122(const QByteArray& bytes); + +} +} diff --git a/crates/cxx-qt-lib/include/core/qvariant.h b/crates/cxx-qt-lib/include/core/qvariant.h index 2b23c466e..52c8e7625 100644 --- a/crates/cxx-qt-lib/include/core/qvariant.h +++ b/crates/cxx-qt-lib/include/core/qvariant.h @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef CXX_QT_GUI_FEATURE #include @@ -91,6 +92,7 @@ CXX_QT_QVARIANT_CAN_CONVERT(QString) CXX_QT_QVARIANT_CAN_CONVERT(QStringList) CXX_QT_QVARIANT_CAN_CONVERT(QTime) CXX_QT_QVARIANT_CAN_CONVERT(QUrl) +CXX_QT_QVARIANT_CAN_CONVERT(QUuid) CXX_QT_QVARIANT_CAN_CONVERT(U8) CXX_QT_QVARIANT_CAN_CONVERT(U16) CXX_QT_QVARIANT_CAN_CONVERT(U32) diff --git a/crates/cxx-qt-lib/include/core/qvector.h b/crates/cxx-qt-lib/include/core/qvector.h index f61b96e94..8a4faa3f4 100644 --- a/crates/cxx-qt-lib/include/core/qvector.h +++ b/crates/cxx-qt-lib/include/core/qvector.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #ifdef CXX_QT_GUI_FEATURE @@ -167,6 +168,7 @@ using QVector_QSizeF = QVector<::QSizeF>; using QVector_QString = QVector<::QString>; using QVector_QTime = QVector<::QTime>; using QVector_QUrl = QVector<::QUrl>; +using QVector_QUuid = QVector<::QUuid>; using QVector_QVariant = QVector<::QVariant>; using QVector_u8 = QVector<::std::uint8_t>; using QVector_u16 = QVector<::std::uint16_t>; diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index a2e9bfa33..bf488d2e2 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -97,6 +97,9 @@ pub use qpointf::QPointF; mod qurl; pub use qurl::QUrl; +mod quuid; +pub use quuid::{QUuid, QUuidVariant, QUuidVersion}; + mod qvariant; pub use qvariant::{QVariant, QVariantValue}; diff --git a/crates/cxx-qt-lib/src/core/qlist/generate.sh b/crates/cxx-qt-lib/src/core/qlist/generate.sh index 037e72e40..70bb43ade 100755 --- a/crates/cxx-qt-lib/src/core/qlist/generate.sh +++ b/crates/cxx-qt-lib/src/core/qlist/generate.sh @@ -227,6 +227,7 @@ generate_bridge_qt "QSizeF" "qsizef" generate_bridge_qt "QString" "qstring" generate_bridge_qt "QTime" "qtime" generate_bridge_qt "QUrl" "qurl" +generate_bridge_qt "QUuid" "quuid" generate_bridge_qt "QVariant" "qvariant" generate_bridge_qt "QLine" "qline" generate_bridge_qt "QLineF" "qlinef" diff --git a/crates/cxx-qt-lib/src/core/qlist/mod.rs b/crates/cxx-qt-lib/src/core/qlist/mod.rs index 59c219256..baa075474 100644 --- a/crates/cxx-qt-lib/src/core/qlist/mod.rs +++ b/crates/cxx-qt-lib/src/core/qlist/mod.rs @@ -9,7 +9,7 @@ use crate::QColor; use crate::QDateTime; use crate::{ QByteArray, QDate, QLine, QLineF, QMargins, QMarginsF, QPersistentModelIndex, QPoint, QPointF, - QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QVariant, + QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QUuid, QVariant, }; use core::{marker::PhantomData, mem::MaybeUninit}; use cxx::{type_id, ExternType}; @@ -373,6 +373,7 @@ impl_qlist_element!(QSizeF, qlist_qsizef, "QList_QSizeF"); impl_qlist_element!(QString, qlist_qstring, "QList_QString"); impl_qlist_element!(QTime, qlist_qtime, "QList_QTime"); impl_qlist_element!(QUrl, qlist_qurl, "QList_QUrl"); +impl_qlist_element!(QUuid, qlist_quuid, "QList_QUuid"); impl_qlist_element!(QVariant, qlist_qvariant, "QList_QVariant"); impl_qlist_element!(u8, qlist_u8, "QList_u8"); impl_qlist_element!(u16, qlist_u16, "QList_u16"); diff --git a/crates/cxx-qt-lib/src/core/qlist/qlist.cpp b/crates/cxx-qt-lib/src/core/qlist/qlist.cpp index 78819a162..49a0a1c03 100644 --- a/crates/cxx-qt-lib/src/core/qlist/qlist.cpp +++ b/crates/cxx-qt-lib/src/core/qlist/qlist.cpp @@ -57,6 +57,7 @@ CXX_QT_QLIST_ASSERTS(::QSizeF, QSizeF); CXX_QT_QLIST_ASSERTS(::QString, QString); CXX_QT_QLIST_ASSERTS(::QTime, QTime); CXX_QT_QLIST_ASSERTS(::QUrl, QUrl); +CXX_QT_QLIST_ASSERTS(::QUuid, QUuid); CXX_QT_QLIST_ASSERTS(::QVariant, QVariant); CXX_QT_QLIST_ASSERTS(::std::uint8_t, u8); CXX_QT_QLIST_ASSERTS(::std::uint16_t, u16); diff --git a/crates/cxx-qt-lib/src/core/qlist/qlist_quuid.rs b/crates/cxx-qt-lib/src/core/qlist/qlist_quuid.rs new file mode 100644 index 000000000..d99b2b305 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qlist/qlist_quuid.rs @@ -0,0 +1,92 @@ +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/quuid.h"); + type QUuid = crate::QUuid; + + include!("cxx-qt-lib/qlist.h"); + type QList_QUuid = crate::QList; + } + + unsafe extern "C++" { + #[rust_name = "cxx_clear"] + fn clear(self: &mut QList_QUuid); + #[rust_name = "cxx_contains"] + fn contains(self: &QList_QUuid, _: &QUuid) -> bool; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + include!("cxx-qt-lib/common.h"); + + #[rust_name = "qlist_clone_QUuid"] + fn construct(_: &QList_QUuid) -> QList_QUuid; + #[rust_name = "qlist_default_QUuid"] + fn construct() -> QList_QUuid; + #[rust_name = "qlist_drop_QUuid"] + fn drop(_: &mut QList_QUuid); + } + + #[namespace = "rust::cxxqtlib1::qlist"] + unsafe extern "C++" { + #[rust_name = "reserve_QUuid"] + fn qlistReserve(_: &mut QList_QUuid, size: isize); + #[rust_name = "append_QUuid"] + fn qlistAppend(_: &mut QList_QUuid, _: &QUuid); + #[rust_name = "get_unchecked_QUuid"] + unsafe fn qlistGetUnchecked(set: &QList_QUuid, pos: isize) -> &QUuid; + #[rust_name = "index_of_QUuid"] + fn qlistIndexOf(_: &QList_QUuid, _: &QUuid) -> isize; + #[rust_name = "insert_QUuid"] + fn qlistInsert(_: &mut QList_QUuid, _: isize, _: &QUuid); + #[rust_name = "remove_QUuid"] + fn qlistRemove(_: &mut QList_QUuid, _: isize); + #[rust_name = "len_QUuid"] + fn qlistLen(_: &QList_QUuid) -> isize; + } +} + +pub(crate) fn reserve(v: &mut ffi::QList_QUuid, size: isize) { + ffi::reserve_QUuid(v, size); +} + +pub(crate) fn append(v: &mut ffi::QList_QUuid, value: &ffi::QUuid) { + ffi::append_QUuid(v, value); +} + +pub(crate) fn clone(s: &ffi::QList_QUuid) -> ffi::QList_QUuid { + ffi::qlist_clone_QUuid(s) +} + +pub(crate) fn default() -> ffi::QList_QUuid { + ffi::qlist_default_QUuid() +} + +pub(crate) fn drop(s: &mut ffi::QList_QUuid) { + ffi::qlist_drop_QUuid(s); +} + +pub(crate) unsafe fn get_unchecked(s: &ffi::QList_QUuid, pos: isize) -> &ffi::QUuid { + ffi::get_unchecked_QUuid(s, pos) +} + +pub(crate) fn index_of(v: &ffi::QList_QUuid, value: &ffi::QUuid) -> isize { + ffi::index_of_QUuid(v, value) +} + +pub(crate) fn insert(s: &mut ffi::QList_QUuid, pos: isize, value: &ffi::QUuid) { + ffi::insert_QUuid(s, pos, value); +} + +pub(crate) fn len(s: &ffi::QList_QUuid) -> isize { + ffi::len_QUuid(s) +} + +pub(crate) fn remove(s: &mut ffi::QList_QUuid, pos: isize) { + ffi::remove_QUuid(s, pos); +} diff --git a/crates/cxx-qt-lib/src/core/qset/generate.sh b/crates/cxx-qt-lib/src/core/qset/generate.sh index cc292c352..93b1da84d 100755 --- a/crates/cxx-qt-lib/src/core/qset/generate.sh +++ b/crates/cxx-qt-lib/src/core/qset/generate.sh @@ -174,6 +174,7 @@ generate_bridge_qt "QPersistentModelIndex" "qpersistentmodelindex" generate_bridge_qt "QString" "qstring" generate_bridge_qt "QTime" "qtime" generate_bridge_qt "QUrl" "qurl" +generate_bridge_qt "QUuid" "quuid" generate_bridge_primitive "u8" generate_bridge_primitive "u16" generate_bridge_primitive "u32" diff --git a/crates/cxx-qt-lib/src/core/qset/mod.rs b/crates/cxx-qt-lib/src/core/qset/mod.rs index 45e44ced0..7b76d2a11 100644 --- a/crates/cxx-qt-lib/src/core/qset/mod.rs +++ b/crates/cxx-qt-lib/src/core/qset/mod.rs @@ -4,7 +4,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 #[cfg(not(target_os = "emscripten"))] use crate::QDateTime; -use crate::{QByteArray, QDate, QPersistentModelIndex, QString, QTime, QUrl}; +use crate::{QByteArray, QDate, QPersistentModelIndex, QString, QTime, QUrl, QUuid}; use core::{marker::PhantomData, mem::MaybeUninit}; use cxx::{type_id, ExternType}; @@ -262,6 +262,7 @@ impl_qset_element!( impl_qset_element!(QString, qset_qstring, "QSet_QString"); impl_qset_element!(QTime, qset_qtime, "QSet_QTime"); impl_qset_element!(QUrl, qset_qurl, "QSet_QUrl"); +impl_qset_element!(QUuid, qset_quuid, "QSet_QUuid"); impl_qset_element!(u8, qset_u8, "QSet_u8"); impl_qset_element!(u16, qset_u16, "QSet_u16"); impl_qset_element!(u32, qset_u32, "QSet_u32"); diff --git a/crates/cxx-qt-lib/src/core/qset/qset.cpp b/crates/cxx-qt-lib/src/core/qset/qset.cpp index e8bbccf40..f1296f4af 100644 --- a/crates/cxx-qt-lib/src/core/qset/qset.cpp +++ b/crates/cxx-qt-lib/src/core/qset/qset.cpp @@ -34,6 +34,7 @@ CXX_QT_QSET_ASSERTS(::QPersistentModelIndex, QPersistentModelIndex); CXX_QT_QSET_ASSERTS(::QString, QString); CXX_QT_QSET_ASSERTS(::QTime, QTime); CXX_QT_QSET_ASSERTS(::QUrl, QUrl); +CXX_QT_QSET_ASSERTS(::QUuid, QUuid); CXX_QT_QSET_ASSERTS(::std::uint8_t, u8); CXX_QT_QSET_ASSERTS(::std::uint16_t, u16); CXX_QT_QSET_ASSERTS(::std::uint32_t, u32); diff --git a/crates/cxx-qt-lib/src/core/qset/qset_quuid.rs b/crates/cxx-qt-lib/src/core/qset/qset_quuid.rs new file mode 100644 index 000000000..c2a4cddae --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qset/qset_quuid.rs @@ -0,0 +1,70 @@ +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/quuid.h"); + type QUuid = crate::QUuid; + + include!("cxx-qt-lib/qset.h"); + type QSet_QUuid = crate::QSet; + } + + unsafe extern "C++" { + #[rust_name = "cxx_clear"] + fn clear(self: &mut QSet_QUuid); + #[rust_name = "cxx_contains"] + fn contains(self: &QSet_QUuid, _: &QUuid) -> bool; + #[rust_name = "cxx_remove"] + fn remove(self: &mut QSet_QUuid, _: &QUuid) -> bool; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + include!("cxx-qt-lib/common.h"); + + #[rust_name = "qset_clone_QUuid"] + fn construct(_: &QSet_QUuid) -> QSet_QUuid; + #[rust_name = "qset_default_QUuid"] + fn construct() -> QSet_QUuid; + #[rust_name = "qset_drop_QUuid"] + fn drop(_: &mut QSet_QUuid); + } + + #[namespace = "rust::cxxqtlib1::qset"] + unsafe extern "C++" { + #[rust_name = "get_unchecked_QUuid"] + unsafe fn qsetGetUnchecked(set: &QSet_QUuid, pos: isize) -> &QUuid; + #[rust_name = "insert_QUuid"] + fn qsetInsert(_: &mut QSet_QUuid, _: &QUuid); + #[rust_name = "len_QUuid"] + fn qsetLen(_: &QSet_QUuid) -> isize; + } +} + +pub(crate) fn clone(s: &ffi::QSet_QUuid) -> ffi::QSet_QUuid { + ffi::qset_clone_QUuid(s) +} + +pub(crate) fn default() -> ffi::QSet_QUuid { + ffi::qset_default_QUuid() +} + +pub(crate) fn drop(s: &mut ffi::QSet_QUuid) { + ffi::qset_drop_QUuid(s); +} + +pub(crate) unsafe fn get_unchecked(s: &ffi::QSet_QUuid, pos: isize) -> &ffi::QUuid { + ffi::get_unchecked_QUuid(s, pos) +} + +pub(crate) fn insert(s: &mut ffi::QSet_QUuid, value: &ffi::QUuid) { + ffi::insert_QUuid(s, value); +} + +pub(crate) fn len(s: &ffi::QSet_QUuid) -> isize { + ffi::len_QUuid(s) +} diff --git a/crates/cxx-qt-lib/src/core/quuid.cpp b/crates/cxx-qt-lib/src/core/quuid.cpp new file mode 100644 index 000000000..e96d5ed17 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/quuid.cpp @@ -0,0 +1,83 @@ +// clang-format off +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Joshua Booth +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#include "cxx-qt-lib/quuid.h" + +#include + +assert_alignment_and_size(QUuid, { + ::std::uint32_t data1; + ::std::uint16_t data2; + ::std::uint16_t data3; + ::std::uint8_t data4[8]; +}); + +static_assert(::std::is_trivially_copyable::value, + "QUuid must be trivially copyable!"); + +static_assert(QTypeInfo::isRelocatable, "QUuid must be relocatable!"); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) +#define byteView(slice) QByteArrayView(slice.data(), slice.length()) +#elif QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#define byteView(slice) \ + QByteArray::fromRawData(reinterpret_cast(slice.data()), \ + static_cast(slice.size())) +#else +#define byteView(slice) \ + QByteArray::fromRawData(reinterpret_cast(slice.data()), \ + static_cast(slice.size())) +#endif + +namespace rust { +namespace cxxqtlib1 { +QUuid +quuidCreateUuidV3(const QUuid& ns, ::rust::Slice slice) +{ + return QUuid::createUuidV3(ns, byteView(slice)); +} + +QUuid +quuidCreateUuid() +{ + return QUuid::createUuid(); +} + +QUuid +quuidCreateUuidV5(const QUuid& ns, ::rust::Slice slice) +{ + return QUuid::createUuidV5(ns, byteView(slice)); +} + +QString +quuidToString(const QUuid& uuid) +{ + return uuid.toString(); +} + +QUuid +quuidFromString(const QString& string) +{ + return QUuid::fromString(string); +} + +QUuid +quuidFromStr(rust::Str string) +{ +#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) + return QUuid::fromString(QAnyStringView(string.data(), string.length())); +#else + return QUuid::fromString(QString::fromLatin1(string.data(), string.length())); +#endif +} + +QUuid +quuidFromRfc4122(const QByteArray& bytes) +{ + return QUuid::fromRfc4122(bytes); +} +} +} diff --git a/crates/cxx-qt-lib/src/core/quuid.rs b/crates/cxx-qt-lib/src/core/quuid.rs new file mode 100644 index 000000000..aa4168b8d --- /dev/null +++ b/crates/cxx-qt-lib/src/core/quuid.rs @@ -0,0 +1,460 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Joshua Booth +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +use crate::{QByteArray, QString}; +use cxx::{type_id, ExternType}; +use std::{fmt, mem}; +#[cfg(feature = "uuid")] +use uuid::Uuid; + +#[cxx::bridge] +mod ffi { + #[repr(i32)] + #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + enum QUuidVariant { + /// Variant is unknown + VarUnknown = -1, + /// Reserved for NCS (Network Computing System) backward compatibility + NCS = 0, + /// Distributed Computing Environment, the scheme used by QUuid + DCE = 2, + /// Reserved for Microsoft backward compatibility (GUID) + Microsoft = 6, + /// Reserved for future definition + Reserved = 7, + } + + #[repr(i32)] + #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + enum QUuidVersion { + /// Version is unknown + VerUnknown = -1, + /// Time-based, by using timestamp, clock sequence, and MAC network card address (if + /// available) for the node sections + Time = 1, + /// DCE Security version, with embedded POSIX UUIDs + EmbeddedPOSIX = 2, + /// Name-based, by using values from a name for all sections + Md5 = 3, + /// Random-based, by using random numbers for all sections + Random = 4, + Sha1 = 5, + } + + unsafe extern "C++" { + include!("cxx-qt-lib/qbytearray.h"); + type QByteArray = crate::QByteArray; + } + + unsafe extern "C++" { + include!("cxx-qt-lib/qstring.h"); + type QString = crate::QString; + } + + unsafe extern "C++" { + include!("cxx-qt-lib/quuid.h"); + type QUuid = super::QUuid; + type QUuidVariant; + type QUuidVersion; + + /// Returns the value in the variant field of the UUID. If the return value is + /// `QUuidVariant::DCE`, call `version()` to see which layout it uses. The null UUID is + /// considered to be of an unknown variant. + fn variant(self: &QUuid) -> QUuidVariant; + + /// Returns the version field of the UUID, if the UUID's variant field is `QUuidVariant::DCE`. + /// Otherwise it returns `QUuidVariant::VerUnknown`. + fn version(self: &QUuid) -> QUuidVersion; + + /// Returns the binary representation of this UUID. The byte array is in big endian format, + /// and formatted according to RFC 4122, section 4.1.2 - "Layout and byte order". + #[rust_name = "to_rfc_4122"] + fn toRfc4122(self: &QUuid) -> QByteArray; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + #[doc(hidden)] + #[rust_name = "quuid_create_uuid_v3"] + fn quuidCreateUuidV3(ns: &QUuid, data: &[u8]) -> QUuid; + #[doc(hidden)] + #[rust_name = "quuid_create_uuid"] + fn quuidCreateUuid() -> QUuid; + #[doc(hidden)] + #[rust_name = "quuid_create_uuid_v5"] + fn quuidCreateUuidV5(ns: &QUuid, data: &[u8]) -> QUuid; + #[doc(hidden)] + #[rust_name = "quuid_to_string"] + fn quuidToString(uuid: &QUuid) -> QString; + #[doc(hidden)] + #[rust_name = "quuid_from_string"] + fn quuidFromString(string: &QString) -> QUuid; + #[doc(hidden)] + #[rust_name = "quuid_from_str"] + fn quuidFromStr(string: &str) -> QUuid; + #[doc(hidden)] + #[rust_name = "quuid_from_rfc_4122"] + fn quuidFromRfc4122(bytes: &QByteArray) -> QUuid; + } +} + +pub use ffi::{QUuidVariant, QUuidVersion}; + +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[repr(C)] +pub struct QUuid { + data1: u32, + data2: u16, + data3: u16, + data4: [u8; 8], +} + +impl Default for QUuid { + /// Creates the null UUID. `to_string()` will output the null UUID as + /// "{00000000-0000-0000-0000-000000000000}". + fn default() -> Self { + Self::null() + } +} + +impl fmt::Display for QUuid { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ffi::quuid_to_string(self).fmt(f) + } +} + +impl fmt::Debug for QUuid { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ffi::quuid_to_string(self).fmt(f) + } +} + +impl QUuid { + /// Creates the null UUID. `to_string()` will output the null UUID as + /// "{00000000-0000-0000-0000-000000000000}". + pub const fn null() -> Self { + Self { + data1: 0, + data2: 0, + data3: 0, + data4: [0; 8], + } + } + + /// Returns `true` if this is the null UUID {00000000-0000-0000-0000-000000000000}; + /// otherwise returns `false`. + pub const fn is_null(&self) -> bool { + (unsafe { std::mem::transmute::(*self) }) == 0 + } + + /// This function returns a new UUID with variant `QUuidVariant::DCE` and version + /// `QUuidVersion::Md5`. `namespace` is the namespace and `data` is the basic data as described + /// by RFC 4122. + pub fn create_uuid_v3(namespace: &Self, data: &[u8]) -> Self { + ffi::quuid_create_uuid_v3(namespace, data) + } + + /// On any platform other than Windows, this function returns a new UUID with variant + /// `QUuidVariant::DCE` and version `QUuidVersion::Random`. On Windows, a GUID is generated using + /// the Windows API and will be of the type that the API decides to create. + pub fn create_uuid() -> Self { + ffi::quuid_create_uuid() + } + + /// This function returns a new UUID with variant `QUuidVariant::DCE` and version + /// `QUuidVersion::Sha1`. `namespace` is the namespace and `data` is the basic data as described + /// by RFC 4122. + pub fn create_uuid_v5(namespace: &Self, data: &[u8]) -> Self { + ffi::quuid_create_uuid_v5(namespace, data) + } + + /// Creates a `QUuid` object from the binary representation of the UUID. + /// The byte array is in big endian format, and formatted according to RFC 4122, section 4.1.2 - + /// "Layout and byte order". + /// + /// The byte array accepted is NOT a human readable format. + /// + /// If the conversion fails, a null UUID is created. + pub fn from_rfc_4122(bytes: &QByteArray) -> Self { + ffi::quuid_from_rfc_4122(bytes) + } + + /// Creates a UUID with the value specified by the parameters. + pub const fn from_fields(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> Self { + Self { + data1, + data2, + data3, + data4, + } + } + + pub const fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) { + (self.data1, self.data2, self.data3, &self.data4) + } + + /// Creates a UUID from its representation as a byte array in big endian. + pub const fn from_bytes(bytes: [u8; 16]) -> Self { + // On big endian targets, this is a no-op. + // On little endian targets, it swaps the bytes of each integer field (data1, data2, data3). + unsafe { mem::transmute::<[u8; 16], Self>(bytes) }.to_be() + } + + /// Returns the memory representation of this UUID as a byte array in big-endian byte order. + pub const fn to_bytes(self) -> [u8; 16] { + // On big endian targets, this is a no-op. + // On little endian targets, it swaps the bytes of each integer field (data1, data2, data3). + unsafe { mem::transmute::(self.to_be()) } + } + + /// Creates a UUID from its representation as a 128-bit integer. + pub const fn from_u128(data: u128) -> Self { + Self::from_bytes(data.to_be_bytes()) + } + + /// Returns the memory representation of this UUID as a 128-bit integer. + pub const fn to_u128(&self) -> u128 { + u128::from_be_bytes(self.to_bytes()) + } + + /// Converts self to big endian from the target’s endianness. + // This function is analogous to [`u8::to_be`](https://doc.rust-lang.org/src/core/num/uint_macros.rs.html#399-431). + /// + /// On big endian this is a no-op. On little endian the bytes are swapped. + /// + /// This is useful for converting between QUuids and byte arrays because byte array + /// representations of UUIDs are always in big endian mode. + #[must_use = "this returns the result of the operation, without modifying the original"] + const fn to_be(self) -> Self { + #[cfg(target_endian = "big")] + { + self + } + #[cfg(target_endian = "little")] + { + Self { + data1: self.data1.swap_bytes(), + data2: self.data2.swap_bytes(), + data3: self.data3.swap_bytes(), + data4: self.data4, + } + } + } +} + +unsafe impl ExternType for QUuid { + type Id = type_id!("QUuid"); + type Kind = cxx::kind::Trivial; +} + +impl From for QString { + fn from(value: QUuid) -> Self { + ffi::quuid_to_string(&value) + } +} + +impl From for QUuid { + fn from(value: u128) -> Self { + Self::from_u128(value) + } +} + +impl From for u128 { + fn from(value: QUuid) -> Self { + value.to_u128() + } +} + +impl From<&QString> for QUuid { + /// Creates a QUuid object from the string text, which must be formatted as five hex fields + /// separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex + /// digit. The curly braces shown here are optional, but it is normal to include them. + /// + /// If the conversion fails, a null UUID is returned. + fn from(value: &QString) -> Self { + ffi::quuid_from_string(value) + } +} + +impl From<&str> for QUuid { + /// Creates a QUuid object from the string text, which must be formatted as five hex fields + /// separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex + /// digit. The curly braces shown here are optional, but it is normal to include them. + /// + /// If the conversion fails, a null UUID is returned. + fn from(value: &str) -> Self { + ffi::quuid_from_str(value) + } +} + +impl From<&String> for QUuid { + /// Creates a QUuid object from the string text, which must be formatted as five hex fields + /// separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex + /// digit. The curly braces shown here are optional, but it is normal to include them. + /// + /// If the conversion fails, a null UUID is returned. + fn from(value: &String) -> Self { + ffi::quuid_from_str(value) + } +} + +impl From<&QByteArray> for QUuid { + /// See [`QUuid::from_rfc_4122`]. + fn from(value: &QByteArray) -> Self { + ffi::quuid_from_rfc_4122(value) + } +} + +impl From for QByteArray { + /// See [`QUuid::to_rfc_4122`]. + fn from(value: QUuid) -> Self { + value.to_rfc_4122() + } +} + +#[cfg(feature = "uuid")] +impl From for QUuid { + fn from(value: Uuid) -> Self { + let (data1, data2, data3, &data4) = value.as_fields(); + Self { + data1, + data2, + data3, + data4, + } + } +} + +#[cfg(feature = "uuid")] +impl From for Uuid { + fn from(value: QUuid) -> Self { + Self::from_fields(value.data1, value.data2, value.data3, &value.data4) + } +} + +#[cfg(test)] +mod test { + use super::*; + + const NAMESPACE_DNS: &QUuid = &QUuid::from_u128(0x6ba7b8109dad11d180b400c04fd430c); + + #[test] + fn quuid_is_null() { + assert!(QUuid::null().is_null()) + } + + #[test] + fn quuid_is_not_null() { + assert!(!QUuid::create_uuid().is_null()) + } + + #[test] + fn quuid_variant() { + assert_eq!( + [QUuid::null().variant(), QUuid::create_uuid().variant()], + [QUuidVariant::VarUnknown, QUuidVariant::DCE] + ); + } + + #[test] + fn quuid_version() { + assert_eq!( + [ + QUuid::null().version(), + QUuid::create_uuid_v3(NAMESPACE_DNS, &[]).version(), + QUuid::create_uuid().version(), + QUuid::create_uuid_v5(NAMESPACE_DNS, &[]).version(), + ], + [ + QUuidVersion::VerUnknown, + QUuidVersion::Md5, + QUuidVersion::Random, + QUuidVersion::Sha1 + ] + ) + } + + #[test] + fn quuid_to_rfc_4122() { + let bytes = <[u8; 16]>::try_from("random test data".as_bytes()).unwrap(); + assert_eq!(Vec::from(&QUuid::from_bytes(bytes).to_rfc_4122()), bytes) + } + + #[test] + fn quuid_null() { + assert_eq!(QUuid::null(), QUuid::from_u128(0)); + } + + #[test] + fn quuid_new_v3() { + assert_eq!( + QUuid::create_uuid_v3(NAMESPACE_DNS, "testdata".as_bytes()), + QUuid::from_u128(0x5157facac7e1345c927671c2c6d41e7a) + ); + } + + #[test] + fn quuid_new_v4() { + assert_ne!(QUuid::create_uuid(), QUuid::create_uuid()); + } + + #[test] + fn quuid_new_v5() { + assert_eq!( + QUuid::create_uuid_v5(NAMESPACE_DNS, "testdata".as_bytes()), + QUuid::from_u128(0x7e95e361a22c51c18c297ac24cb61e83) + ); + } + + #[test] + fn quuid_to_string() { + assert_eq!( + QUuid::from_u128(0x7e95e361a22c51c18c297ac24cb61e83).to_string(), + "{7e95e361-a22c-51c1-8c29-7ac24cb61e83}" + ) + } + + #[test] + fn quuid_qstring_round_trip() { + let uuid = QUuid::create_uuid(); + let roundtrip = QUuid::from(&QString::from(&uuid.to_string())); + assert_eq!(uuid, roundtrip) + } + + #[test] + fn quuid_str_round_trip() { + let uuid = QUuid::create_uuid(); + let roundtrip = QUuid::from(&uuid.to_string()); + assert_eq!(uuid, roundtrip) + } + + #[test] + fn quuid_fields_round_trip() { + let uuid = QUuid::create_uuid(); + let (d1, d2, d3, &d4) = uuid.as_fields(); + let roundtrip = QUuid::from_fields(d1, d2, d3, d4); + assert_eq!(uuid, roundtrip) + } + + #[test] + fn quuid_bytes_round_trip() { + let uuid = QUuid::create_uuid(); + let roundtrip = QUuid::from_bytes(uuid.to_bytes()); + assert_eq!(uuid, roundtrip) + } + + #[test] + fn quuid_qbytearray_round_trip() { + let uuid = QUuid::create_uuid(); + let roundtrip = QUuid::from_rfc_4122(&uuid.to_rfc_4122()); + assert_eq!(uuid, roundtrip) + } + + #[test] + fn quuid_u128_round_trip() { + let uuid = QUuid::create_uuid(); + let roundtrip = QUuid::from_u128(uuid.to_u128()); + assert_eq!(uuid, roundtrip) + } +} diff --git a/crates/cxx-qt-lib/src/core/qvariant/generate.sh b/crates/cxx-qt-lib/src/core/qvariant/generate.sh index f9eab338e..2bb4701a1 100755 --- a/crates/cxx-qt-lib/src/core/qvariant/generate.sh +++ b/crates/cxx-qt-lib/src/core/qvariant/generate.sh @@ -116,6 +116,7 @@ generate_bridge_qt "QString" "qstring" generate_bridge_qt "QStringList" "qstringlist" generate_bridge_qt "QTime" "qtime" generate_bridge_qt "QUrl" "qurl" +generate_bridge_qt "QUuid" "quuid" generate_bridge_primitive "u8" "U8" generate_bridge_primitive "u16" "U16" generate_bridge_primitive "u32" "U32" diff --git a/crates/cxx-qt-lib/src/core/qvariant/mod.rs b/crates/cxx-qt-lib/src/core/qvariant/mod.rs index 8d196ca96..ccdd3f80b 100644 --- a/crates/cxx-qt-lib/src/core/qvariant/mod.rs +++ b/crates/cxx-qt-lib/src/core/qvariant/mod.rs @@ -181,6 +181,7 @@ impl_qvariant_value!(crate::QString, qvariant_qstring); impl_qvariant_value!(crate::QStringList, qvariant_qstringlist); impl_qvariant_value!(crate::QTime, qvariant_qtime); impl_qvariant_value!(crate::QUrl, qvariant_qurl); +impl_qvariant_value!(crate::QUuid, qvariant_quuid); impl_qvariant_value!(u8, qvariant_u8); impl_qvariant_value!(u16, qvariant_u16); impl_qvariant_value!(u32, qvariant_u32); diff --git a/crates/cxx-qt-lib/src/core/qvariant/qvariant.cpp b/crates/cxx-qt-lib/src/core/qvariant/qvariant.cpp index bf94ff721..5d812e501 100644 --- a/crates/cxx-qt-lib/src/core/qvariant/qvariant.cpp +++ b/crates/cxx-qt-lib/src/core/qvariant/qvariant.cpp @@ -83,6 +83,7 @@ CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::QString, QString) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::QStringList, QStringList) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::QTime, QTime) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::QUrl, QUrl) +CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::QUuid, QUuid) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::uint8_t, U8) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::uint16_t, U16) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::uint32_t, U32) diff --git a/crates/cxx-qt-lib/src/core/qvariant/qvariant_quuid.rs b/crates/cxx-qt-lib/src/core/qvariant/qvariant_quuid.rs new file mode 100644 index 000000000..c3dca7b65 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qvariant/qvariant_quuid.rs @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/quuid.h"); + type QUuid = crate::QUuid; + + include!("cxx-qt-lib/qvariant.h"); + type QVariant = crate::QVariant; + } + + #[namespace = "rust::cxxqtlib1::qvariant"] + unsafe extern "C++" { + #[rust_name = "can_convert_QUuid"] + fn qvariantCanConvertQUuid(variant: &QVariant) -> bool; + #[rust_name = "construct_QUuid"] + fn qvariantConstruct(value: &QUuid) -> QVariant; + #[rust_name = "value_or_default_QUuid"] + fn qvariantValueOrDefault(variant: &QVariant) -> QUuid; + } +} + +pub(crate) fn can_convert(variant: &ffi::QVariant) -> bool { + ffi::can_convert_QUuid(variant) +} + +pub(crate) fn construct(value: &ffi::QUuid) -> ffi::QVariant { + ffi::construct_QUuid(value) +} + +pub(crate) fn value_or_default(variant: &ffi::QVariant) -> ffi::QUuid { + ffi::value_or_default_QUuid(variant) +} diff --git a/crates/cxx-qt-lib/src/core/qvector/generate.sh b/crates/cxx-qt-lib/src/core/qvector/generate.sh index 519400a2a..229ca3c75 100755 --- a/crates/cxx-qt-lib/src/core/qvector/generate.sh +++ b/crates/cxx-qt-lib/src/core/qvector/generate.sh @@ -229,6 +229,7 @@ generate_bridge_qt "QSizeF" "qsizef" generate_bridge_qt "QString" "qstring" generate_bridge_qt "QTime" "qtime" generate_bridge_qt "QUrl" "qurl" +generate_bridge_qt "QUuid" "quuid" generate_bridge_qt "QVariant" "qvariant" generate_bridge_primitive "u8" generate_bridge_primitive "u16" diff --git a/crates/cxx-qt-lib/src/core/qvector/mod.rs b/crates/cxx-qt-lib/src/core/qvector/mod.rs index 1894418fe..2f1568287 100644 --- a/crates/cxx-qt-lib/src/core/qvector/mod.rs +++ b/crates/cxx-qt-lib/src/core/qvector/mod.rs @@ -9,7 +9,7 @@ use crate::QColor; use crate::QDateTime; use crate::{ QByteArray, QDate, QLine, QLineF, QMargins, QMarginsF, QPersistentModelIndex, QPoint, QPointF, - QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QVariant, + QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QUuid, QVariant, }; use core::{marker::PhantomData, mem::MaybeUninit}; use cxx::{type_id, ExternType}; @@ -373,6 +373,7 @@ impl_qvector_element!(QSizeF, qvector_qsizef, "QVector_QSizeF"); impl_qvector_element!(QString, qvector_qstring, "QVector_QString"); impl_qvector_element!(QTime, qvector_qtime, "QVector_QTime"); impl_qvector_element!(QUrl, qvector_qurl, "QVector_QUrl"); +impl_qvector_element!(QUuid, qvector_quuid, "QVector_QUuid"); impl_qvector_element!(QVariant, qvector_qvariant, "QVector_QVariant"); impl_qvector_element!(u8, qvector_u8, "QVector_u8"); impl_qvector_element!(u16, qvector_u16, "QVector_u16"); diff --git a/crates/cxx-qt-lib/src/core/qvector/qvector.cpp b/crates/cxx-qt-lib/src/core/qvector/qvector.cpp index 236f92e6f..7323c5df9 100644 --- a/crates/cxx-qt-lib/src/core/qvector/qvector.cpp +++ b/crates/cxx-qt-lib/src/core/qvector/qvector.cpp @@ -58,6 +58,7 @@ CXX_QT_QVECTOR_ASSERTS(::QSizeF, QSizeF); CXX_QT_QVECTOR_ASSERTS(::QString, QString); CXX_QT_QVECTOR_ASSERTS(::QTime, QTime); CXX_QT_QVECTOR_ASSERTS(::QUrl, QUrl); +CXX_QT_QVECTOR_ASSERTS(::QUuid, QUuid); CXX_QT_QVECTOR_ASSERTS(::QVariant, QVariant); CXX_QT_QVECTOR_ASSERTS(::std::uint8_t, u8); CXX_QT_QVECTOR_ASSERTS(::std::uint16_t, u16); diff --git a/crates/cxx-qt-lib/src/core/qvector/qvector_quuid.rs b/crates/cxx-qt-lib/src/core/qvector/qvector_quuid.rs new file mode 100644 index 000000000..8cf0a4702 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qvector/qvector_quuid.rs @@ -0,0 +1,92 @@ +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/quuid.h"); + type QUuid = crate::QUuid; + + include!("cxx-qt-lib/qvector.h"); + type QVector_QUuid = crate::QVector; + } + + unsafe extern "C++" { + #[rust_name = "cxx_clear"] + fn clear(self: &mut QVector_QUuid); + #[rust_name = "cxx_contains"] + fn contains(self: &QVector_QUuid, _: &QUuid) -> bool; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + include!("cxx-qt-lib/common.h"); + + #[rust_name = "qvector_clone_QUuid"] + fn construct(_: &QVector_QUuid) -> QVector_QUuid; + #[rust_name = "qvector_default_QUuid"] + fn construct() -> QVector_QUuid; + #[rust_name = "qvector_drop_QUuid"] + fn drop(_: &mut QVector_QUuid); + } + + #[namespace = "rust::cxxqtlib1::qvector"] + unsafe extern "C++" { + #[rust_name = "reserve_QUuid"] + fn qvectorReserve(_: &mut QVector_QUuid, size: isize); + #[rust_name = "append_QUuid"] + fn qvectorAppend(_: &mut QVector_QUuid, _: &QUuid); + #[rust_name = "get_unchecked_QUuid"] + unsafe fn qvectorGetUnchecked(set: &QVector_QUuid, pos: isize) -> &QUuid; + #[rust_name = "index_of_QUuid"] + fn qvectorIndexOf(_: &QVector_QUuid, _: &QUuid) -> isize; + #[rust_name = "insert_QUuid"] + fn qvectorInsert(_: &mut QVector_QUuid, _: isize, _: &QUuid); + #[rust_name = "remove_QUuid"] + fn qvectorRemove(_: &mut QVector_QUuid, _: isize); + #[rust_name = "len_QUuid"] + fn qvectorLen(_: &QVector_QUuid) -> isize; + } +} + +pub(crate) fn append(v: &mut ffi::QVector_QUuid, value: &ffi::QUuid) { + ffi::append_QUuid(v, value); +} + +pub(crate) fn clone(s: &ffi::QVector_QUuid) -> ffi::QVector_QUuid { + ffi::qvector_clone_QUuid(s) +} + +pub(crate) fn reserve(v: &mut ffi::QVector_QUuid, size: isize) { + ffi::reserve_QUuid(v, size); +} + +pub(crate) fn default() -> ffi::QVector_QUuid { + ffi::qvector_default_QUuid() +} + +pub(crate) fn drop(s: &mut ffi::QVector_QUuid) { + ffi::qvector_drop_QUuid(s); +} + +pub(crate) unsafe fn get_unchecked(s: &ffi::QVector_QUuid, pos: isize) -> &ffi::QUuid { + ffi::get_unchecked_QUuid(s, pos) +} + +pub(crate) fn index_of(v: &ffi::QVector_QUuid, value: &ffi::QUuid) -> isize { + ffi::index_of_QUuid(v, value) +} + +pub(crate) fn insert(s: &mut ffi::QVector_QUuid, pos: isize, value: &ffi::QUuid) { + ffi::insert_QUuid(s, pos, value); +} + +pub(crate) fn len(s: &ffi::QVector_QUuid) -> isize { + ffi::len_QUuid(s) +} + +pub(crate) fn remove(s: &mut ffi::QVector_QUuid, pos: isize) { + ffi::remove_QUuid(s, pos); +}