Skip to content

Commit

Permalink
qanystringview: improve lifetimes to match C++ closer
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Nov 15, 2024
1 parent 2aeec1b commit af371b7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
4 changes: 0 additions & 4 deletions crates/cxx-qt-lib/include/core/qanystringview.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ namespace cxxqtlib1 {

QAnyStringView
qanystringviewInitFromRustString(::rust::Str string);
QAnyStringView
qanystringviewInitFromQString(const QString& string);
::rust::String
qanystringviewToRustString(const QAnyStringView& string);

::rust::isize
qanystringviewLen(const QAnyStringView& string);
Expand Down
6 changes: 0 additions & 6 deletions crates/cxx-qt-lib/src/core/qanystringview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ qanystringviewInitFromRustString(::rust::Str string)
return QAnyStringView(string.data(), string.size());
}

QAnyStringView
qanystringviewInitFromQString(const QString& string)
{
return QAnyStringView(string);
}

::rust::isize
qanystringviewLen(const QAnyStringView& string)
{
Expand Down
31 changes: 24 additions & 7 deletions crates/cxx-qt-lib/src/core/qanystringview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileContributor: Joshua Goins <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::QString;
use crate::{QByteArray, QString};
use core::ffi::c_void;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
Expand All @@ -14,6 +14,9 @@ mod ffi {
include!("cxx-qt-lib/qanystringview.h");
type QAnyStringView<'a> = super::QAnyStringView<'a>;

include!("cxx-qt-lib/qbytearray.h");
type QByteArray = crate::QByteArray;

include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;

Expand All @@ -24,6 +27,10 @@ mod ffi {
/// Returns true if this string is null; otherwise returns false.
#[rust_name = "is_null"]
fn isNull(self: &QAnyStringView) -> bool;

/// Returns a deep copy of this string view's data as a QString.
#[rust_name = "to_qstring"]
fn toString(self: &QAnyStringView) -> QString;
}

#[namespace = "rust::cxxqtlib1"]
Expand All @@ -35,13 +42,16 @@ mod ffi {
fn construct() -> QAnyStringView<'static>;
#[doc(hidden)]
#[rust_name = "QAnyStringView_init_from_rust_string"]
fn qanystringviewInitFromRustString<'a>(string: &str) -> QAnyStringView<'a>;
fn qanystringviewInitFromRustString<'a>(string: &'a str) -> QAnyStringView<'a>;
#[doc(hidden)]
#[rust_name = "QAnyStringView_init_from_qbytearray"]
fn construct<'a>(bytes: &'a QByteArray) -> QAnyStringView<'a>;
#[doc(hidden)]
#[rust_name = "QAnyStringView_init_from_qstring"]
fn qanystringviewInitFromQString<'a>(string: &QString) -> QAnyStringView<'a>;
fn construct<'a>(string: &'a QString) -> QAnyStringView<'a>;
#[doc(hidden)]
#[rust_name = "QAnyStringView_init_from_QAnyStringView"]
fn construct<'a>(string: &QAnyStringView) -> QAnyStringView<'a>;
fn construct<'a>(string: &QAnyStringView<'a>) -> QAnyStringView<'a>;

#[doc(hidden)]
#[rust_name = "QAnyStringView_eq"]
Expand Down Expand Up @@ -90,14 +100,21 @@ impl Eq for QAnyStringView<'_> {}

impl<'a> From<&'a str> for QAnyStringView<'a> {
/// Constructs a QAnyStringView from a Rust string
fn from(str: &str) -> Self {
fn from(str: &'a str) -> QAnyStringView<'a> {
ffi::QAnyStringView_init_from_rust_string(str)
}
}

impl From<&QString> for QAnyStringView<'_> {
impl<'a> From<&'a QByteArray> for QAnyStringView<'a> {
/// Constructs a QAnyStringView from a QByteArray
fn from(bytes: &'a QByteArray) -> QAnyStringView<'a> {
ffi::QAnyStringView_init_from_qbytearray(bytes)
}
}

impl<'a> From<&'a QString> for QAnyStringView<'a> {
/// Constructs a QAnyStringView from a QString
fn from(string: &QString) -> Self {
fn from(string: &'a QString) -> QAnyStringView<'a> {
ffi::QAnyStringView_init_from_qstring(string)
}
}
Expand Down
18 changes: 14 additions & 4 deletions tests/qt_types_standalone/rust/src/qanystringview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@ mod qanystringview_cxx {

extern "Rust" {
fn construct_qanystringview(str: &str) -> QAnyStringView;
}

// This method must be unsafe otherwise we hit
// must be `unsafe fn` in order to expose explicit lifetimes to C++
//
// But then Rust complains about unused unsafe so we need to allow for this
#[allow(unused_unsafe)]
extern "Rust" {
unsafe fn construct_qanystringview_qstring<'a>(str: &'a QString) -> QAnyStringView<'a>;
unsafe fn clone_qanystringview<'a>(l: &'a QAnyStringView) -> QAnyStringView<'a>;
unsafe fn clone_qanystringview<'a>(l: &QAnyStringView<'a>) -> QAnyStringView<'a>;
}
}

fn construct_qanystringview(str: &str) -> QAnyStringView {
fn construct_qanystringview<'a>(str: &'a str) -> QAnyStringView<'a> {
QAnyStringView::from(str)
}
fn construct_qanystringview_qstring(str: &QString) -> QAnyStringView {

fn construct_qanystringview_qstring<'a>(str: &'a QString) -> QAnyStringView<'a> {
QAnyStringView::from(str)
}
fn clone_qanystringview<'a>(l: &'a QAnyStringView) -> QAnyStringView<'a> {

fn clone_qanystringview<'a>(l: &QAnyStringView<'a>) -> QAnyStringView<'a> {
l.clone()
}

0 comments on commit af371b7

Please sign in to comment.