chain_debug/lib.rs
1//! # Example
2//!
3//! ```rust
4//! use chain_debug::DebugExt;
5//!
6//! struct A {
7//! inner: B,
8//! }
9//!
10//! #[derive(Debug)]
11//! struct B {
12//! s: String,
13//! }
14//!
15//! impl A {
16//! fn inner(&self) -> &B {
17//! &self.inner
18//! }
19//! }
20//!
21//! let a = A { ... };
22//!
23//! a
24//! .inner()
25//! .debug()
26//! .s
27//! .debug()
28//! ```
29//!
30
31use std::fmt::Debug;
32
33pub trait DebugExt {
34 fn debug(&self) -> &Self
35 where
36 Self: Debug,
37 {
38 println!("{self:?}");
39 self
40 }
41}
42
43impl<D: Debug> DebugExt for D {}
44
45#[test]
46fn test() {
47 #[derive(Debug)]
48 struct S {
49 inner: String,
50 }
51
52 impl S {
53 fn upper(&self) -> &str {
54 &self.inner
55 }
56 }
57
58 struct G {
59 inner: S,
60 }
61
62 impl G {
63 fn inner(&self) -> &S {
64 &self.inner
65 }
66 }
67
68 let s = S {
69 inner: "foo".to_string(),
70 };
71 let g = G { inner: s };
72
73 g.inner().debug().upper().debug();
74}