Skip to content

Commit 4979207

Browse files
authored
Respect Formatter options in Debug and Display implementations (#1279)
* cxx-qt-lib: respect Formatter options in Debug and Display * cxx-qt-lib: restore stringlike Debug implementation for QByteArray
1 parent 88af234 commit 4979207

33 files changed

+71
-54
lines changed

crates/cxx-qt-lib/src/core/qanystringview.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ impl QAnyStringView<'_> {
129129

130130
impl fmt::Display for QAnyStringView<'_> {
131131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132-
write!(f, "{}", self.to_qstring())
132+
self.to_qstring().fmt(f)
133133
}
134134
}
135135

136136
impl fmt::Debug for QAnyStringView<'_> {
137137
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138-
write!(f, "{self}")
138+
self.to_qstring().fmt(f)
139139
}
140140
}
141141

crates/cxx-qt-lib/src/core/qbytearray.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use cxx::{type_id, ExternType};
66
use std::fmt;
77
use std::mem::MaybeUninit;
8+
use std::str;
89

910
#[cxx::bridge]
1011
mod ffi {
@@ -155,17 +156,18 @@ impl std::cmp::Eq for QByteArray {}
155156
impl fmt::Display for QByteArray {
156157
/// Convert the QByteArray to a Rust string
157158
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158-
if let Ok(string) = String::from_utf8(self.into()) {
159-
write!(f, "{string}")
159+
let slice = self.as_slice();
160+
if let Ok(string) = str::from_utf8(slice) {
161+
string.fmt(f)
160162
} else {
161-
write!(f, "{:?}", self.as_slice())
163+
fmt::Debug::fmt(slice, f)
162164
}
163165
}
164166
}
165167

166168
impl fmt::Debug for QByteArray {
167169
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168-
write!(f, "{self}")
170+
fmt::Display::fmt(self, f)
169171
}
170172
}
171173

@@ -379,7 +381,6 @@ unsafe impl ExternType for QByteArray {
379381

380382
#[cfg(test)]
381383
mod tests {
382-
#[cfg(feature = "bytes")]
383384
use super::*;
384385

385386
#[cfg(feature = "serde")]
@@ -399,4 +400,10 @@ mod tests {
399400
let bytes_bytes = bytes::Bytes::from(&qbytearray);
400401
assert_eq!(bytes, bytes_bytes)
401402
}
403+
404+
#[test]
405+
fn test_display_fmt() {
406+
let qbytearray = QByteArray::from("KDAB");
407+
assert_eq!(format!("{:-<8}", qbytearray), "KDAB----")
408+
}
402409
}

crates/cxx-qt-lib/src/core/qdate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ impl Default for QDate {
141141

142142
impl fmt::Display for QDate {
143143
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144-
write!(f, "{}", self.format_enum(ffi::DateFormat::TextDate))
144+
self.format_enum(ffi::DateFormat::TextDate).fmt(f)
145145
}
146146
}
147147

148148
impl fmt::Debug for QDate {
149149
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150-
write!(f, "{}", ffi::qdate_to_debug_qstring(self))
150+
ffi::qdate_to_debug_qstring(self).fmt(f)
151151
}
152152
}
153153

crates/cxx-qt-lib/src/core/qdatetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@ impl Ord for QDateTime {
393393

394394
impl fmt::Display for QDateTime {
395395
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
396-
write!(f, "{}", self.format_enum(ffi::DateFormat::TextDate))
396+
self.format_enum(ffi::DateFormat::TextDate).fmt(f)
397397
}
398398
}
399399

400400
impl fmt::Debug for QDateTime {
401401
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
402-
write!(f, "{}", ffi::qdatetime_to_debug_qstring(self))
402+
ffi::qdatetime_to_debug_qstring(self).fmt(f)
403403
}
404404
}
405405

crates/cxx-qt-lib/src/core/qline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Default for QLine {
122122

123123
impl fmt::Display for QLine {
124124
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125-
write!(f, "{}", ffi::qline_to_debug_qstring(self))
125+
ffi::qline_to_debug_qstring(self).fmt(f)
126126
}
127127
}
128128

crates/cxx-qt-lib/src/core/qlinef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl From<QLineF> for ffi::QLine {
170170

171171
impl fmt::Display for QLineF {
172172
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173-
write!(f, "{}", ffi::qlinef_to_debug_qstring(self))
173+
ffi::qlinef_to_debug_qstring(self).fmt(f)
174174
}
175175
}
176176

crates/cxx-qt-lib/src/core/qmargins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Default for QMargins {
122122

123123
impl fmt::Display for QMargins {
124124
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125-
write!(f, "{}", ffi::qmargins_to_debug_qstring(self))
125+
ffi::qmargins_to_debug_qstring(self).fmt(f)
126126
}
127127
}
128128

crates/cxx-qt-lib/src/core/qmarginsf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Default for QMarginsF {
118118

119119
impl fmt::Display for QMarginsF {
120120
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121-
write!(f, "{}", ffi::qmarginsf_to_debug_qstring(self))
121+
ffi::qmarginsf_to_debug_qstring(self).fmt(f)
122122
}
123123
}
124124

crates/cxx-qt-lib/src/core/qmodelindex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ impl std::cmp::Eq for QModelIndex {}
8888

8989
impl fmt::Display for QModelIndex {
9090
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91-
write!(f, "{self:?}")
91+
ffi::qmodelindex_to_debug_qstring(self).fmt(f)
9292
}
9393
}
9494

9595
impl fmt::Debug for QModelIndex {
9696
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97-
write!(f, "{}", ffi::qmodelindex_to_debug_qstring(self))
97+
ffi::qmodelindex_to_debug_qstring(self).fmt(f)
9898
}
9999
}
100100

crates/cxx-qt-lib/src/core/qpersistentmodelindex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ impl std::cmp::Eq for QPersistentModelIndex {}
9696

9797
impl fmt::Display for QPersistentModelIndex {
9898
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99-
write!(f, "{self:?}")
99+
ffi::qpersistentmodelindex_to_debug_qstring(self).fmt(f)
100100
}
101101
}
102102

103103
impl fmt::Debug for QPersistentModelIndex {
104104
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105-
write!(f, "{}", ffi::qpersistentmodelindex_to_debug_qstring(self))
105+
ffi::qpersistentmodelindex_to_debug_qstring(self).fmt(f)
106106
}
107107
}
108108

0 commit comments

Comments
 (0)