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

Implement Debug for all types that work with QDebug #1162

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions crates/cxx-qt-lib/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ drop(T& value)
template<typename T>
QString
toQString(const T& value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This template should no longer be necessary.
If toString() is already a member function, we don't need to create a free function toQString, but can just access it directly via CXX:
i.e.:

// Maybe with #[doc(hidden)], depending on the situation
#[rust_name="to_qstring"]
fn toQString(self: &T);

Please remove this function and replace the existing references with direct member access.

Copy link
Contributor Author

@jnbooth jnbooth Jan 20, 2025

Choose a reason for hiding this comment

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

The idea of this template is to allow types to have more flexibility in how they define to_qstring(). Some types have extra parameters for toString. For others, we might not want to have a to_qstring method at all (rather than relying on #[doc(hidden)]. Or we might decide not to have methods like that in general and use From<&Type> for QString, which is more idiomatic. So by using this template, types aren't "locked in" to a particular implementation; we can keep the Display logic separate from everything else.

That said, I of course defer to you. What should be the rubric for whether or not to use #[doc(hidden)]?

{
return value.toString();
}

template<typename T>
QString
toDebugQString(const T& value)
{
// We can't convert value directly into a string.
// However most Qt types are able to stream into a QDebug object such as
Expand Down
7 changes: 7 additions & 0 deletions crates/cxx-qt-lib/include/core/qtimezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <QtCore/QList>
#include <QtCore/QTimeZone>

using QTimeZoneNameType = QTimeZone::NameType;
using QTimeZoneTimeType = QTimeZone::TimeType;

namespace rust {
namespace cxxqtlib1 {

Expand All @@ -22,6 +25,10 @@ ::std::unique_ptr<QTimeZone>
qtimezoneClone(const QTimeZone& timezone);
::std::unique_ptr<QTimeZone>
qtimezoneDefault();
QString
qtimezoneDisplayName(const QTimeZone& timezone,
QTimeZoneTimeType timeType,
QTimeZoneNameType nameType);
::std::unique_ptr<QTimeZone>
qtimezoneFromOffsetSeconds(::std::int32_t offsetSeconds);
::std::unique_ptr<QTimeZone>
Expand Down
2 changes: 0 additions & 2 deletions crates/cxx-qt-lib/include/core/qurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ qurlToPercentEncoding(const QString& input,
const QByteArray& exclude,
const QByteArray& include);
QString
qurlToQString(const QUrl& url);
QString
qurlUserInfo(const QUrl& url);
QString
qurlUserName(const QUrl& url);
Expand Down
13 changes: 13 additions & 0 deletions crates/cxx-qt-lib/include/gui/qpolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <QtCore/QPoint>
#include <QtCore/QVector>
#include <QtGui/QPolygon>

#include "rust/cxx.h"
Expand All @@ -19,3 +21,14 @@ struct IsRelocatable<QPolygon> : ::std::true_type
{};

} // namespace rust

namespace rust {
namespace cxxqtlib1 {

const QVector<QPoint>&
qpolygonAsQVectorQPointRef(const QPolygon& shape);
QVector<QPoint>&
qpolygonAsQVectorQPointRef(QPolygon& shape);

}
}
13 changes: 13 additions & 0 deletions crates/cxx-qt-lib/include/gui/qpolygonf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <QtCore/QPointF>
#include <QtCore/QVector>
#include <QtGui/QPolygonF>

#include "rust/cxx.h"
Expand All @@ -19,3 +21,14 @@ struct IsRelocatable<QPolygonF> : ::std::true_type
{};

} // namespace rust

namespace rust {
namespace cxxqtlib1 {

const QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(const QPolygonF& shape);
QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(QPolygonF& shape);

}
}
17 changes: 17 additions & 0 deletions crates/cxx-qt-lib/src/core/qanystringview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::ffi::c_void;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use cxx::{type_id, ExternType};
use std::fmt;

#[cxx::bridge]
mod ffi {
Expand Down Expand Up @@ -60,6 +61,10 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "QAnyStringView_len"]
fn qanystringviewLen(string: &QAnyStringView) -> isize;

#[doc(hidden)]
#[rust_name = "QAnyStringView_to_qstring"]
fn toQString(string: &QAnyStringView) -> QString;
jnbooth marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -126,6 +131,18 @@ impl QAnyStringView<'_> {
}
}

impl fmt::Display for QAnyStringView<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::QAnyStringView_to_qstring(self))
}
}

impl fmt::Debug for QAnyStringView<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
Expand Down
9 changes: 5 additions & 4 deletions crates/cxx-qt-lib/src/core/qbytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use cxx::{type_id, ExternType};
use std::fmt;
use std::mem::MaybeUninit;

#[cxx::bridge]
Expand Down Expand Up @@ -151,9 +152,9 @@ impl std::cmp::PartialEq for QByteArray {

impl std::cmp::Eq for QByteArray {}

impl std::fmt::Display for QByteArray {
impl fmt::Display for QByteArray {
/// Convert the QByteArray to a Rust string
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Ok(string) = String::from_utf8(self.into()) {
write!(f, "{string}")
} else {
Expand All @@ -162,8 +163,8 @@ impl std::fmt::Display for QByteArray {
}
}

impl std::fmt::Debug for QByteArray {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl fmt::Debug for QByteArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ mod ffi {
#[rust_name = "qdate_init"]
fn construct(y: i32, m: i32, d: i32) -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_to_debug_qstring"]
fn toDebugQString(value: &QDate) -> QString;
#[doc(hidden)]
#[rust_name = "qdate_to_qstring"]
fn toQString(value: &QDate) -> QString;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ impl fmt::Display for QDate {

impl fmt::Debug for QDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
write!(f, "{}", ffi::qdate_to_debug_qstring(self))
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ mod ffi {
#[rust_name = "qdatetime_cmp"]
fn operatorCmp(a: &QDateTime, b: &QDateTime) -> i8;
#[doc(hidden)]
#[rust_name = "qdatetime_to_debug_qstring"]
fn toDebugQString(value: &QDateTime) -> QString;
#[doc(hidden)]
#[rust_name = "qdatetime_to_qstring"]
fn toQString(value: &QDateTime) -> QString;
}
Expand Down Expand Up @@ -405,7 +408,7 @@ impl fmt::Display for QDateTime {

impl fmt::Debug for QDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
write!(f, "{}", ffi::qdatetime_to_debug_qstring(self))
}
}

Expand Down
12 changes: 12 additions & 0 deletions crates/cxx-qt-lib/src/core/qhash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
use std::fmt;

/// The QHash class is a template class that provides a hash-table-based dictionary.
///
Expand Down Expand Up @@ -142,6 +143,17 @@ where
}
}

impl<T> fmt::Debug for QHash<T>
where
T: QHashPair,
T::Key: fmt::Debug,
T::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}

unsafe impl<T> ExternType for QHash<T>
where
T: QHashPair,
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-lib/src/core/qline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ mod ffi {
fn construct(pt1: QPoint, pt2: QPoint) -> QLine;

#[doc(hidden)]
#[rust_name = "qline_to_qstring"]
fn toQString(value: &QLine) -> QString;
#[rust_name = "qline_to_debug_qstring"]
fn toDebugQString(value: &QLine) -> QString;
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ impl Default for QLine {

impl fmt::Display for QLine {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qline_to_qstring(self))
write!(f, "{}", ffi::qline_to_debug_qstring(self))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-lib/src/core/qlinef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ mod ffi {
fn construct(line: &QLine) -> QLineF;

#[doc(hidden)]
#[rust_name = "qlinef_to_qstring"]
fn toQString(value: &QLineF) -> QString;
#[rust_name = "qlinef_to_debug_qstring"]
fn toDebugQString(value: &QLineF) -> QString;
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl From<QLineF> for ffi::QLine {

impl fmt::Display for QLineF {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qlinef_to_qstring(self))
write!(f, "{}", ffi::qlinef_to_debug_qstring(self))
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/cxx-qt-lib/src/core/qlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
};
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
use std::fmt;

/// The QList class is a template class that provides a dynamic array.
///
Expand Down Expand Up @@ -75,6 +76,15 @@ where

impl<T> Eq for QList<T> where T: QListElement + Eq {}

impl<T> fmt::Debug for QList<T>
where
T: QListElement + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}

impl<T> QList<T>
where
T: QListElement,
Expand Down
12 changes: 12 additions & 0 deletions crates/cxx-qt-lib/src/core/qmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
use std::fmt;

/// The QMap class is a template class that provides an associative array.
///
Expand Down Expand Up @@ -65,6 +66,17 @@ where
{
}

impl<T> fmt::Debug for QMap<T>
where
T: QMapPair,
T::Key: fmt::Debug,
T::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}

impl<T> QMap<T>
where
T: QMapPair,
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-lib/src/core/qmargins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ mod ffi {
#[rust_name = "qmargins_new"]
fn construct(left: i32, top: i32, right: i32, bottom: i32) -> QMargins;
#[doc(hidden)]
#[rust_name = "qmargins_to_qstring"]
fn toQString(value: &QMargins) -> QString;
#[rust_name = "qmargins_to_debug_qstring"]
fn toDebugQString(value: &QMargins) -> QString;
#[doc(hidden)]
#[rust_name = "qmargins_plus"]
fn operatorPlus(a: &QMargins, b: &QMargins) -> QMargins;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Default for QMargins {

impl fmt::Display for QMargins {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qmargins_to_qstring(self))
write!(f, "{}", ffi::qmargins_to_debug_qstring(self))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-lib/src/core/qmarginsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ mod ffi {
#[rust_name = "qmarginsf_new"]
fn construct(left: f64, top: f64, right: f64, bottom: f64) -> QMarginsF;
#[doc(hidden)]
#[rust_name = "qmarginsf_to_qstring"]
fn toQString(value: &QMarginsF) -> QString;
#[rust_name = "qmarginsf_to_debug_qstring"]
fn toDebugQString(value: &QMarginsF) -> QString;
#[doc(hidden)]
#[rust_name = "qmarginsf_plus"]
fn operatorPlus(a: &QMarginsF, b: &QMarginsF) -> QMarginsF;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Default for QMarginsF {

impl fmt::Display for QMarginsF {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qmarginsf_to_qstring(self))
write!(f, "{}", ffi::qmarginsf_to_debug_qstring(self))
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/cxx-qt-lib/src/core/qmodelindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ mod ffi {
#[rust_name = "qmodelindex_eq"]
fn operatorEq(a: &QModelIndex, b: &QModelIndex) -> bool;
#[doc(hidden)]
#[rust_name = "qmodelindex_to_qstring"]
fn toQString(value: &QModelIndex) -> QString;
#[rust_name = "qmodelindex_to_debug_qstring"]
fn toDebugQString(value: &QModelIndex) -> QString;

#[doc(hidden)]
#[rust_name = "qmodelindex_internal_id"]
Expand Down Expand Up @@ -97,13 +97,13 @@ impl std::cmp::Eq for QModelIndex {}

impl fmt::Display for QModelIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qmodelindex_to_qstring(self))
write!(f, "{self:?}")
}
}

impl fmt::Debug for QModelIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
write!(f, "{}", ffi::qmodelindex_to_debug_qstring(self))
}
}

Expand Down
Loading
Loading