Skip to content

Commit 97a45a6

Browse files
committed
feat: enhance __str__ representation of types
1 parent 1b20aca commit 97a45a6

File tree

11 files changed

+29
-19
lines changed

11 files changed

+29
-19
lines changed

examples/header_map.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
print("keys_len (unique keys):", headers.keys_len())
1919
# Check if the map is empty
2020
print("is_empty:", headers.is_empty())
21+
# Print the entire header map
22+
print(headers)
2123
# Clear all headers
2224
headers.clear()
2325
print("After clear, is_empty:", headers.is_empty())

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl SocketAddr {
5454
}
5555
}
5656

57-
define_display!(SocketAddr);
57+
impl_print_str!(Display, SocketAddr);
5858

5959
/// A builder for `Client`.
6060
#[derive(Default)]

src/client/resp/ws/msg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,4 @@ impl Message {
173173
}
174174
}
175175

176-
define_display!(Message);
176+
impl_print_str!(Display, Message);

src/cookie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl Cookie {
181181
}
182182
}
183183

184-
define_display!(Cookie);
184+
impl_print_str!(Display, Cookie);
185185

186186
// ===== impl Cookies =====
187187

src/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl HeaderMap {
232232
}
233233
}
234234

235-
define_display!(HeaderMap);
235+
impl_print_str!(Debug, HeaderMap);
236236

237237
impl FromPyObject<'_, '_> for HeaderMap {
238238
type Error = PyErr;
@@ -338,7 +338,7 @@ impl OrigHeaderMap {
338338
}
339339
}
340340

341-
define_display!(OrigHeaderMap);
341+
impl_print_str!(Debug, OrigHeaderMap);
342342

343343
impl FromPyObject<'_, '_> for OrigHeaderMap {
344344
type Error = PyErr;

src/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ impl StatusCode {
8686
}
8787
}
8888

89-
define_display!(StatusCode);
89+
impl_print_str!(Display, StatusCode);

src/http1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ impl Http1Options {
107107
}
108108
}
109109

110-
define_display!(Http1Options);
110+
impl_print_str!(Debug, Http1Options);

src/http2.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl StreamId {
206206
}
207207
}
208208

209-
define_display!(StreamId);
209+
impl_print_str!(Display, StreamId);
210210

211211
// ===== impl StreamDependency =====
212212

@@ -224,7 +224,7 @@ impl StreamDependency {
224224
}
225225
}
226226

227-
define_display!(StreamDependency);
227+
impl_print_str!(Display, StreamDependency);
228228

229229
// ===== impl Priority =====
230230

@@ -238,7 +238,7 @@ impl Priority {
238238
}
239239
}
240240

241-
define_display!(Priority);
241+
impl_print_str!(Display, Priority);
242242

243243
// ===== impl Priorities =====
244244

@@ -256,7 +256,7 @@ impl Priorities {
256256
}
257257
}
258258

259-
define_display!(Priorities);
259+
impl_print_str!(Display, Priorities);
260260

261261
// ===== impl PseudoOrder =====
262262

@@ -274,7 +274,7 @@ impl PseudoOrder {
274274
}
275275
}
276276

277-
define_display!(PseudoOrder);
277+
impl_print_str!(Display, PseudoOrder);
278278

279279
// ===== impl SettingsOrder =====
280280

@@ -292,7 +292,7 @@ impl SettingsOrder {
292292
}
293293
}
294294

295-
define_display!(SettingsOrder);
295+
impl_print_str!(Display, SettingsOrder);
296296

297297
// ===== impl Builder =====
298298

@@ -462,4 +462,4 @@ impl Http2Options {
462462
}
463463
}
464464

465-
define_display!(Http2Options);
465+
impl_print_str!(Debug, Http2Options);

src/macros.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,21 @@ macro_rules! define_enum {
161161
};
162162
}
163163

164-
macro_rules! define_display {
165-
($typed:ident) => {
164+
macro_rules! impl_print_str {
165+
(Debug, $typed:ident) => {
166166
impl std::fmt::Display for $typed {
167167
#[inline]
168168
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169169
std::fmt::Debug::fmt(&self.0, f)
170170
}
171171
}
172172
};
173+
(Display, $typed:ident) => {
174+
impl std::fmt::Display for $typed {
175+
#[inline]
176+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177+
self.0.fmt(f)
178+
}
179+
}
180+
};
173181
}

src/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Proxy {
9393
}
9494
}
9595

96-
define_display!(Proxy);
96+
impl_print_str!(Debug, Proxy);
9797

9898
fn create_proxy<'py>(
9999
py: Python<'py>,

0 commit comments

Comments
 (0)