From caf014111ff7e541f67d3e281cad2ac06d79c61f Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Wed, 21 Sep 2022 10:38:17 -0700 Subject: [PATCH] api: impl Default for RegexSet This is justified by the fact that a RegexSet is, after all, a set. And a set has a very obvious default value: the empty set. Plus, this is exactly what you get by passing a default `Vec` or an empty iterator to the `RegexSet::new` constructor. We specifically do not add a `Default` impl for Regex because it has no obvious default value. Fixes #905, Closes #906 --- src/re_set.rs | 6 ++++++ tests/set.rs | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/re_set.rs b/src/re_set.rs index a6d886d761..92d475f7b0 100644 --- a/src/re_set.rs +++ b/src/re_set.rs @@ -289,6 +289,12 @@ impl RegexSet { } } +impl Default for RegexSet { + fn default() -> Self { + RegexSet::empty() + } +} + /// A set of matches returned by a regex set. #[derive(Clone, Debug)] pub struct SetMatches { diff --git a/tests/set.rs b/tests/set.rs index 37fcf8700c..d1144d6623 100644 --- a/tests/set.rs +++ b/tests/set.rs @@ -65,3 +65,10 @@ fn len_and_empty() { assert_eq!(not_empty.len(), 2); assert!(!not_empty.is_empty()); } + +#[test] +fn default_set_is_empty() { + let set: regex::bytes::RegexSet = Default::default(); + assert_eq!(set.len(), 0); + assert!(set.is_empty()); +}