Skip to content

Commit 051d5e9

Browse files
authored
Merge pull request #115 from KodrAus/chore/re-usable-filter-builder
Prevent filter::Builder from being reused
2 parents 299713a + 0002fdd commit 051d5e9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/filter/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub struct Filter {
116116
pub struct Builder {
117117
directives: Vec<Directive>,
118118
filter: Option<inner::Filter>,
119+
built: bool,
119120
}
120121

121122
#[derive(Debug)]
@@ -183,6 +184,7 @@ impl Builder {
183184
Builder {
184185
directives: Vec::new(),
185186
filter: None,
187+
built: false,
186188
}
187189
}
188190

@@ -239,6 +241,9 @@ impl Builder {
239241

240242
/// Build a log filter.
241243
pub fn build(&mut self) -> Filter {
244+
assert!(!self.built, "attempt to re-use consumed builder");
245+
self.built = true;
246+
242247
if self.directives.is_empty() {
243248
// Adds the default filter if none exist
244249
self.directives.push(Directive {
@@ -279,10 +284,16 @@ impl fmt::Debug for Filter {
279284

280285
impl fmt::Debug for Builder {
281286
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
282-
f.debug_struct("Filter")
287+
if self.built {
288+
f.debug_struct("Filter")
289+
.field("built", &true)
290+
.finish()
291+
} else {
292+
f.debug_struct("Filter")
283293
.field("filter", &self.filter)
284294
.field("directives", &self.directives)
285295
.finish()
296+
}
286297
}
287298
}
288299

0 commit comments

Comments
 (0)