Skip to content

Commit

Permalink
cxx-qt-lib: remove toQString from common.h
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbooth committed Feb 14, 2025
1 parent 96ba9b8 commit 1990db9
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 87 deletions.
7 changes: 0 additions & 7 deletions crates/cxx-qt-lib/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ drop(T& value)
value.~T();
}

template<typename T>
QString
toQString(const T& value)
{
return value.toString();
}

template<typename T>
QString
toDebugQString(const T& value)
Expand Down
7 changes: 2 additions & 5 deletions crates/cxx-qt-lib/include/core/qurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ struct IsRelocatable<QUrl> : ::std::true_type
namespace rust {
namespace cxxqtlib1 {

QUrl
qurlInitFromString(::rust::Str string);
::rust::String
qurlToRustString(const QUrl& url);

// Bitwise enums don't work well with Rust and CXX, so lets just use the
// defaults for now
QString
Expand Down Expand Up @@ -84,6 +79,8 @@ QString
qurlToDisplayString(const QUrl& url);
QByteArray
qurlToEncoded(const QUrl& url);
QString
qurlToQString(const QUrl& url);
QByteArray
qurlToPercentEncoding(const QString& input,
const QByteArray& exclude,
Expand Down
2 changes: 0 additions & 2 deletions crates/cxx-qt-lib/include/gui/qcolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ qcolorInitFromRgb(::std::int32_t red,
::std::int32_t alpha);
QColor
qcolorInitFromRgbF(float red, float green, float blue, float alpha);
QColor
qcolorInitFromRustString(::rust::Str string);

// Qt 5 uses qreal and Qt 6 uses float, so cast all to floats
float
Expand Down
6 changes: 1 addition & 5 deletions crates/cxx-qt-lib/src/core/qanystringview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ 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;
}
}

Expand Down Expand Up @@ -133,7 +129,7 @@ impl QAnyStringView<'_> {

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

Expand Down
5 changes: 1 addition & 4 deletions crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ mod ffi {
#[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 All @@ -146,7 +143,7 @@ impl Default for QDate {

impl fmt::Display for QDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qdate_to_qstring(self))
write!(f, "{}", self.format_enum(ffi::DateFormat::TextDate))
}
}

Expand Down
9 changes: 5 additions & 4 deletions crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ mod ffi {
#[rust_name = "to_secs_since_epoch"]
fn toSecsSinceEpoch(self: &QDateTime) -> qint64;

/// Returns the time as a string. The format parameter determines the format of the string.
#[rust_name = "format_enum"]
fn toString(self: &QDateTime, format: DateFormat) -> QString;

/// Returns a copy of this datetime converted to the given time spec.
///
/// Note this method is only available with Qt < 6.8
Expand Down Expand Up @@ -230,9 +234,6 @@ mod ffi {
#[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 @@ -367,7 +368,7 @@ impl Ord for QDateTime {

impl fmt::Display for QDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qdatetime_to_qstring(self))
write!(f, "{}", self.format_enum(ffi::DateFormat::TextDate))
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/cxx-qt-lib/src/core/qtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qtime_to_debug_qstring"]
fn toDebugQString(value: &QTime) -> QString;
#[doc(hidden)]
#[rust_name = "qtime_to_qstring"]
fn toQString(value: &QTime) -> QString;
}
}

Expand Down Expand Up @@ -181,7 +178,7 @@ impl Default for QTime {

impl fmt::Display for QTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qtime_to_qstring(self))
write!(f, "{}", self.format_enum(ffi::DateFormat::TextDate))
}
}

Expand Down
22 changes: 6 additions & 16 deletions crates/cxx-qt-lib/src/core/qurl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#include "cxx-qt-lib/qurl.h"
#include "cxx-qt-lib/qstring.h"

#include <cxx-qt-lib/assertion_utils.h>

Expand All @@ -26,21 +25,6 @@ static_assert(QTypeInfo<QUrl>::isRelocatable);
namespace rust {
namespace cxxqtlib1 {

QUrl
qurlInitFromString(::rust::Str string)
{
// Note that rust::Str here is borrowed
// and we convert back from UTF-8 to UTF-16
return QUrl(qstringInitFromRustString(string));
}

::rust::String
qurlToRustString(const QUrl& url)
{
// Note that this changes UTF-16 to UTF-8
return qstringToRustString(url.toString());
}

QString
qurlAuthority(const QUrl& url)
{
Expand Down Expand Up @@ -191,6 +175,12 @@ qurlToEncoded(const QUrl& url)
return url.toEncoded();
}

QString
qurlToQString(const QUrl& url)
{
return url.toString();
}

QByteArray
qurlToPercentEncoding(const QString& input,
const QByteArray& exclude,
Expand Down
18 changes: 5 additions & 13 deletions crates/cxx-qt-lib/src/core/qurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ mod ffi {
// Bitwise enums don't work well with Rust and CXX, so lets just use the defaults for now
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qurl_init_from_string"]
fn qurlInitFromString(string: &str) -> QUrl;
#[doc(hidden)]
#[rust_name = "qurl_to_rust_string"]
fn qurlToRustString(url: &QUrl) -> String;

#[rust_name = "qurl_authority"]
fn qurlAuthority(url: &QUrl) -> QString;
#[rust_name = "qurl_file_name"]
Expand Down Expand Up @@ -147,6 +140,8 @@ mod ffi {
fn qurlToDisplayString(url: &QUrl) -> QString;
#[rust_name = "qurl_to_encoded"]
fn qurlToEncoded(url: &QUrl) -> QByteArray;
#[rust_name = "qurl_to_qstring"]
fn qurlToQString(url: &QUrl) -> QString;
#[rust_name = "qurl_to_percent_encoding"]
fn qurlToPercentEncoding(
input: &QString,
Expand Down Expand Up @@ -184,9 +179,6 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qurl_to_debug_qstring"]
fn toDebugQString(url: &QUrl) -> QString;
#[doc(hidden)]
#[rust_name = "qurl_to_qstring"]
fn toQString(url: &QUrl) -> QString;
}
}

Expand Down Expand Up @@ -435,7 +427,7 @@ impl fmt::Display for QUrl {
///
/// Note that this converts from UTF-16 to UTF-8
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", ffi::qurl_to_rust_string(self))
write!(f, "{}", ffi::qurl_to_display_string(self))
}
}

Expand Down Expand Up @@ -464,7 +456,7 @@ impl From<&str> for QUrl {
///
/// Note that this converts from UTF-8 to UTF-16
fn from(str: &str) -> Self {
ffi::qurl_init_from_string(str)
Self::from(&ffi::QString::from(str))
}
}

Expand All @@ -473,7 +465,7 @@ impl From<&String> for QUrl {
///
/// Note that this converts from UTF-8 to UTF-16
fn from(str: &String) -> Self {
ffi::qurl_init_from_string(str)
Self::from(str.as_str())
}
}

Expand Down
7 changes: 0 additions & 7 deletions crates/cxx-qt-lib/src/gui/qcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

#include "cxx-qt-lib/qcolor.h"
#include "cxx-qt-lib/qstring.h"

#include <cxx-qt-lib/assertion_utils.h>

Expand Down Expand Up @@ -138,12 +137,6 @@ qcolorInitFromRgbF(float red, float green, float blue, float alpha)
#endif
}

QColor
qcolorInitFromRustString(::rust::Str string)
{
return QColor(qstringInitFromRustString(string));
}

// Qt 5 uses qreal and Qt 6 uses float, so cast all to floats

float
Expand Down
17 changes: 2 additions & 15 deletions crates/cxx-qt-lib/src/gui/qcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qcolor_init_from_rgb_f"]
fn qcolorInitFromRgbF(red: f32, green: f32, blue: f32, alpha: f32) -> QColor;
#[doc(hidden)]
#[rust_name = "qcolor_init_from_rust_string"]
fn qcolorInitFromRustString(string: &str) -> QColor;

#[doc(hidden)]
#[rust_name = "qcolor_alpha_f"]
Expand Down Expand Up @@ -520,25 +517,15 @@ impl TryFrom<&str> for QColor {
type Error = &'static str;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let color = ffi::qcolor_init_from_rust_string(value);
if color.is_valid() {
Ok(color)
} else {
Err("Invalid color")
}
Self::try_from(&ffi::QString::from(value))
}
}

impl TryFrom<&String> for QColor {
type Error = &'static str;

fn try_from(value: &String) -> Result<Self, Self::Error> {
let color = ffi::qcolor_init_from_rust_string(value);
if color.is_valid() {
Ok(color)
} else {
Err("Invalid color")
}
Self::try_from(value.as_str())
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/cxx-qt-lib/src/gui/qfont.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ mod ffi {
#[rust_name = "style_strategy"]
fn styleStrategy(self: &QFont) -> QFontStyleStrategy;

/// Returns the font as a string.
#[rust_name = "to_qstring"]
fn toString(self: &QFont) -> QString;

/// Returns true if underline has been set; otherwise returns false.
fn underline(self: &QFont) -> bool;

Expand Down Expand Up @@ -358,9 +362,6 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qfont_to_debug_qstring"]
fn toDebugQString(value: &QFont) -> QString;
#[doc(hidden)]
#[rust_name = "qfont_to_qstring"]
fn toQString(value: &QFont) -> QString;
}
}

Expand Down Expand Up @@ -395,13 +396,13 @@ impl Clone for QFont {

impl fmt::Display for QFont {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qfont_to_qstring(self))
write!(f, "{}", self.to_qstring())
}
}

impl fmt::Debug for QFont {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qfont_to_debug_qstring(self))
write!(f, "{}", self.to_qstring())
}
}

Expand Down

0 comments on commit 1990db9

Please sign in to comment.