-
Notifications
You must be signed in to change notification settings - Fork 87
Implement QAnyStringView #995
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// clang-format on | ||
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QAnyStringView> | ||
#include <QtCore/QByteArray> | ||
#include <QtCore/QString> | ||
|
||
#include "rust/cxx.h" | ||
|
||
// Define namespace otherwise we hit a GCC bug | ||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 | ||
namespace rust { | ||
|
||
template<> | ||
struct IsRelocatable<QAnyStringView> : ::std::true_type | ||
{}; | ||
|
||
} // namespace rust | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
QAnyStringView | ||
qanystringviewInitFromRustString(::rust::Str string); | ||
QAnyStringView | ||
qanystringviewInitFromQString(const QString& string); | ||
::rust::String | ||
qanystringviewToRustString(const QAnyStringView& string); | ||
|
||
::rust::isize | ||
qanystringviewLen(const QAnyStringView& string); | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// clang-format on | ||
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#include "cxx-qt-lib/qanystringview.h" | ||
|
||
#include <cxx-qt-lib/assertion_utils.h> | ||
|
||
// QAnyStringView has two members. | ||
// A union of (void*, char*, char_16*) and a size_t. | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/text/qanystringview.h | ||
assert_alignment_and_size(QAnyStringView, { | ||
::std::size_t a0; | ||
void* a1; | ||
}); | ||
|
||
static_assert(::std::is_trivially_copy_assignable<QAnyStringView>::value); | ||
static_assert(::std::is_trivially_copy_constructible<QAnyStringView>::value); | ||
|
||
static_assert(::std::is_trivially_destructible<QAnyStringView>::value); | ||
ahayzen-kdab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static_assert(QTypeInfo<QAnyStringView>::isRelocatable); | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
QAnyStringView | ||
qanystringviewInitFromRustString(::rust::Str string) | ||
{ | ||
return QAnyStringView(string.data(), string.size()); | ||
} | ||
|
||
QAnyStringView | ||
qanystringviewInitFromQString(const QString& string) | ||
{ | ||
return QAnyStringView(string); | ||
} | ||
|
||
::rust::isize | ||
qanystringviewLen(const QAnyStringView& string) | ||
{ | ||
return static_cast<::rust::isize>(string.size()); | ||
} | ||
ahayzen-kdab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// SPDX-FileContributor: Joshua Goins <josh@redstrate.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use crate::QString; | ||
use core::ffi::c_void; | ||
use core::marker::PhantomData; | ||
use core::mem::MaybeUninit; | ||
use cxx::{type_id, ExternType}; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qanystringview.h"); | ||
type QAnyStringView<'a> = super::QAnyStringView<'a>; | ||
|
||
include!("cxx-qt-lib/qstring.h"); | ||
type QString = crate::QString; | ||
|
||
/// Returns true if the string has no characters; otherwise returns false. | ||
#[rust_name = "is_empty"] | ||
fn isEmpty(self: &QAnyStringView) -> bool; | ||
|
||
/// Returns true if this string is null; otherwise returns false. | ||
#[rust_name = "is_null"] | ||
fn isNull(self: &QAnyStringView) -> bool; | ||
} | ||
|
||
#[namespace = "rust::cxxqtlib1"] | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/common.h"); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_init_default"] | ||
fn construct() -> QAnyStringView<'static>; | ||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_init_from_rust_string"] | ||
fn qanystringviewInitFromRustString<'a>(string: &str) -> QAnyStringView<'a>; | ||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_init_from_qstring"] | ||
fn qanystringviewInitFromQString<'a>(string: &QString) -> QAnyStringView<'a>; | ||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_init_from_QAnyStringView"] | ||
fn construct<'a>(string: &QAnyStringView) -> QAnyStringView<'a>; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_eq"] | ||
fn operatorEq(a: &QAnyStringView, b: &QAnyStringView) -> bool; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "QAnyStringView_len"] | ||
fn qanystringviewLen(string: &QAnyStringView) -> isize; | ||
} | ||
} | ||
|
||
/// The QAnyStringView class provides a unified view of a Latin-1, UTF-8, or UTF-16 string. | ||
#[repr(C)] | ||
pub struct QAnyStringView<'a> { | ||
/// QAnyStringView has two members, a pointer and a size_t | ||
_space: MaybeUninit<[usize; 1]>, | ||
_space2: MaybeUninit<[c_void; 1]>, | ||
|
||
/// Needed to keep the lifetime in check | ||
_phantom: PhantomData<&'a usize>, | ||
} | ||
|
||
impl<'a> Clone for QAnyStringView<'a> { | ||
/// Constructs a copy of other. | ||
/// | ||
/// This operation takes constant time, because QAnyStringView is a view-only string. | ||
fn clone(&self) -> QAnyStringView<'a> { | ||
ffi::QAnyStringView_init_from_QAnyStringView(self) | ||
} | ||
} | ||
|
||
impl Default for QAnyStringView<'_> { | ||
/// Constructs a null string. Null strings are also empty. | ||
fn default() -> Self { | ||
ffi::QAnyStringView_init_default() | ||
} | ||
} | ||
|
||
impl PartialEq for QAnyStringView<'_> { | ||
fn eq(&self, other: &Self) -> bool { | ||
ffi::QAnyStringView_eq(self, other) | ||
} | ||
} | ||
|
||
impl Eq for QAnyStringView<'_> {} | ||
|
||
impl<'a> From<&'a str> for QAnyStringView<'a> { | ||
/// Constructs a QAnyStringView from a Rust string | ||
fn from(str: &str) -> Self { | ||
ffi::QAnyStringView_init_from_rust_string(str) | ||
} | ||
} | ||
|
||
redstrate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl From<&QString> for QAnyStringView<'_> { | ||
/// Constructs a QAnyStringView from a QString | ||
fn from(string: &QString) -> Self { | ||
ffi::QAnyStringView_init_from_qstring(string) | ||
} | ||
} | ||
|
||
impl QAnyStringView<'_> { | ||
/// Returns the number of characters in this string. | ||
pub fn len(&self) -> isize { | ||
ffi::QAnyStringView_len(self) | ||
} | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size is the same. | ||
unsafe impl ExternType for QAnyStringView<'_> { | ||
type Id = type_id!("QAnyStringView"); | ||
type Kind = cxx::kind::Trivial; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// clang-format on | ||
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QAnyStringView> | ||
#include <QtTest/QTest> | ||
|
||
#include "qt_types_standalone/src/qanystringview.cxx.h" | ||
|
||
class QAnyStringViewTest : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void construct() | ||
{ | ||
const auto s = construct_qanystringview("String constructed by Rust"); | ||
QCOMPARE(s, QByteArrayLiteral("String constructed by Rust")); | ||
} | ||
|
||
void construct_qstring() | ||
{ | ||
const auto s = construct_qanystringview_qstring( | ||
QStringLiteral("String constructed by Rust")); | ||
QCOMPARE(s, QByteArrayLiteral("String constructed by Rust")); | ||
} | ||
|
||
void clone() | ||
{ | ||
const auto l = QAnyStringView("Test"); | ||
const auto c = clone_qanystringview(l); | ||
QCOMPARE(c, l); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.