Skip to content

Commit c1b29b4

Browse files
committed
Improved docs
1 parent f043c8e commit c1b29b4

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

partialdebug-derive/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use quote::{quote, ToTokens};
44
use syn::parse::{Parse, ParseStream};
55
use syn::*;
66

7+
/// The non exhaustive version of `PartialDebug`
78
#[proc_macro_derive(NonExhaustivePartialDebug)]
89
pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
910
let input = parse_macro_input!(input as ItemStruct);
@@ -50,6 +51,7 @@ pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
5051
TokenStream::from(expanded)
5152
}
5253

54+
/// The placeholder version of `PartialDebug`
5355
#[proc_macro_derive(PlaceholderPartialDebug, attributes(debug_placeholder))]
5456
pub fn derive_placeholder(input: TokenStream) -> TokenStream {
5557
let input = parse_macro_input!(input as ItemStruct);

src/lib.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
//! Derive Debug for types where not all fields implement Debug
2+
//!
13
//! # Non Exhaustive
24
//!
35
//! ```
46
//! #![feature(debug_non_exhaustive)]
57
//! use partialdebug::non_exhaustive::PartialDebug;
68
//!
7-
//! # #[allow(dead_code)]
8-
//! # struct DNA {
9-
//! # sequence: &'static str,
10-
//! # }
9+
//! # struct DNA;
1110
//! #
1211
//! #[derive(PartialDebug)]
1312
//! struct Dog {
@@ -21,9 +20,7 @@
2120
//! # Dog {
2221
//! # legs: 4,
2322
//! # eyes: 2,
24-
//! # dna: DNA {
25-
//! # sequence: "",
26-
//! # },
23+
//! # dna: DNA,
2724
//! # }
2825
//! # }
2926
//! # }
@@ -36,10 +33,7 @@
3633
//! ```
3734
//! use partialdebug::placeholder::PartialDebug;
3835
//!
39-
//! # #[allow(dead_code)]
40-
//! # struct DNA {
41-
//! # sequence: &'static str,
42-
//! # }
36+
//! # struct DNA;
4337
//! #
4438
//! #[derive(PartialDebug)]
4539
//! struct Dog {
@@ -53,9 +47,7 @@
5347
//! # Dog {
5448
//! # legs: 4,
5549
//! # eyes: 2,
56-
//! # dna: DNA {
57-
//! # sequence: "",
58-
//! # },
50+
//! # dna: DNA,
5951
//! # }
6052
//! # }
6153
//! # }
@@ -68,10 +60,7 @@
6860
//! ```
6961
//! use partialdebug::placeholder::PartialDebug;
7062
//!
71-
//! # #[allow(dead_code)]
72-
//! # struct DNA {
73-
//! # sequence: &'static str,
74-
//! # }
63+
//! # struct DNA;
7564
//! #
7665
//! #[derive(PartialDebug)]
7766
//! #[debug_placeholder = "Unknown"]
@@ -86,9 +75,7 @@
8675
//! # Dog {
8776
//! # legs: 4,
8877
//! # eyes: 2,
89-
//! # dna: DNA {
90-
//! # sequence: "",
91-
//! # },
78+
//! # dna: DNA,
9279
//! # }
9380
//! # }
9481
//! # }
@@ -98,10 +85,20 @@
9885
9986
#![allow(incomplete_features)]
10087
#![feature(specialization)]
88+
#![warn(missing_docs, trivial_casts, rust_2018_idioms)]
10189

10290
use core::fmt::{Debug, Formatter, Result};
10391

92+
/// Specialized trait used to distinguish between types that implement Debug and one's that don't^.
93+
/// ```
94+
/// # use partialdebug::AsDebug;
95+
/// # struct DNA;
96+
/// # let dna = DNA;
97+
/// assert!(42.as_debug().is_some());
98+
/// assert!(dna.as_debug().is_none());
99+
/// ```
104100
pub trait AsDebug {
101+
/// Try to get a reference to `self` as `dyn Debug`
105102
fn as_debug(&self) -> Option<&dyn Debug>;
106103
}
107104

@@ -117,6 +114,11 @@ impl<T: Debug> AsDebug for T {
117114
}
118115
}
119116

117+
/// Placeholder struct for types that do not implement Debug
118+
/// ```
119+
/// # use partialdebug::Placeholder;
120+
/// assert_eq!(format!("{:?}", Placeholder("Foo")), "Foo")
121+
/// ```
120122
pub struct Placeholder(pub &'static str);
121123

122124
impl Debug for Placeholder {
@@ -125,10 +127,12 @@ impl Debug for Placeholder {
125127
}
126128
}
127129

130+
/// The non exhaustive version of `PartialDebug`
128131
pub mod non_exhaustive {
129132
pub use partialdebug_derive::NonExhaustivePartialDebug as PartialDebug;
130133
}
131134

135+
/// The placeholder version of `PartialDebug`
132136
pub mod placeholder {
133137
pub use partialdebug_derive::PlaceholderPartialDebug as PartialDebug;
134138
}

0 commit comments

Comments
 (0)