1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Easily check if the struct is empty.
//!
//! Say you have a struct full of `Option<T>` fields, and you want to know if all the fields are `None`.
//! ```
//! struct Foo {
//! client_ip: Option<String>,
//! client_country: Option<String>,
//! }
//! ```
//! You can manually check for each field like this:
//! ```
//!# struct Foo {
//!# client_ip: Option<String>,
//!# client_country: Option<String>,
//!# }
//! impl Foo {
//! fn is_empty(&self) -> bool {
//! self.client_ip.is_none() && self.client_country.is_none()
//! }
//! }
//! ```
//! But this becomes tedious as more and more fields are added to the struct.
//!
//! With this crate, you can derive the `IsEmpty` trait, and then call is_empty() on the struct.
//! ```
//! use is_empty::IsEmpty;
//!
//! #[derive(IsEmpty)]
//! struct Foo {
//! client_ip: Option<String>,
//! client_country: Option<String>,
//! }
//!
//! let empty_foo = Foo { client_ip: None, client_country: None };
//! assert!(empty_foo.is_empty());
//! ```
//!
//! You can also nest other `IsEmpty`-deriving struct inside the struct.
//! ```
//! use is_empty::IsEmpty;
//!
//! #[derive(IsEmpty)]
//! struct Foo {
//! bar: Bar,
//! baz: Option<u8>
//! }
//! #[derive(IsEmpty)]
//! struct Bar {
//! client_ip: Option<String>,
//! client_country: Option<String>,
//! }
//!
//! let empty_foo = Foo { bar: Bar { client_ip: None, client_country: None }, baz: None };
//! assert!(empty_foo.is_empty());
//! ```
//!
//! If you want to customize the logic for determining if the field is empty, you can use the `#[is_empty(if = "some_fn")]` attribute.
//! ```
//! use is_empty::IsEmpty;
//!
//! #[derive(IsEmpty)]
//! struct Foo {
//! #[is_empty(if = "Vec::is_empty")]
//! bar: Vec<u8>,
//! }
//!
//! let empty_foo = Foo { bar: vec![] };
//! assert!(empty_foo.is_empty());
//! ```
//!
//! This crate pairs well with serde's `#[serde(skip_serializing_if = "condition")]` attribute.
//! ```
//! use is_empty::IsEmpty;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, IsEmpty)]
//! struct Foo {
//! client_ip: Option<String>,
//! client_country: Option<String>,
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct Root {
//! #[serde(skip_serializing_if = "is_empty::is_empty")]
//! foo: Foo,
//! }
//!
//! let empty_foo = Foo { client_ip: None, client_country: None };
//! let root = Root { foo: empty_foo };
//! assert_eq!(serde_json::to_string(&root).unwrap(), "{}");
//! ```
//!
//! For support for additional types, enable "std_impls" feature:
//!```
//! # mod test_std_impl {
//! #![cfg(feature = "std_impls")]
//! # use std::collections::{HashSet, HashMap};
//! # use is_empty::IsEmpty;
//! # use serde::{Serialize, Deserialize};
//!
//! # fn main() {
//! #[derive(Serialize, Deserialize, IsEmpty)]
//! struct Bar {
//! owned_string: String,
//! os_string: std::ffi::OsString,
//!
//! vec: Vec<String>,
//! set: HashSet<String>,
//! map: HashMap<String, String>
//! }
//!
//! let bar = Bar {
//! owned_string: String::new(),
//! os_string: std::ffi::OsString::new(),
//! vec: vec![],
//! set: HashSet::new(),
//! map: HashMap::new(),
//! };
//!
//! assert!(bar.is_empty())
//! # }
//! # }
//! # #[cfg(not(feature = "std_impls"))]
//! # mod test_std_impl {
//! # fn main() {}
//! # }
//!
//!
//!
pub use *;
/// A trait for checking if a struct is empty.
/// See the [crate-level documentation](crate) for more information.
/// Thin wrapper function around [IsEmpty::is_empty]. Use it with serde's skip_serializing_if attribute.
/// ```
/// use is_empty::IsEmpty;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, IsEmpty)]
/// struct Foo {
/// client_ip: Option<String>,
/// client_country: Option<String>,
/// }
///
/// #[derive(Serialize, Deserialize)]
/// struct Root {
/// #[serde(skip_serializing_if = "is_empty::is_empty")]
/// foo: Foo,
/// }
///
/// let empty_foo = Foo { client_ip: None, client_country: None };
/// let root = Root { foo: empty_foo };
/// assert_eq!(serde_json::to_string(&root).unwrap(), "{}");
/// ```
/// Check if the `Option<T>` is really empty.
///
/// [is_empty] returns false for `Some(T)`, even if `T` is empty.
/// This function inspects the struct wrapped inside `Option::Some<T>`, and returns true if it is empty.
/// (Of course, it returns true for `None`.)
///
/// This function can be used in two ways:
/// - in a `#[is_empty(if = "is_empty::is_option_really_empty")]` attribute,
/// - in a `#[serde(skip_serializing_if = "is_empty::is_option_really_empty")]` attribute.