Skip to content

Commit

Permalink
api: impl Default for RegexSet
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sourcefrog authored and BurntSushi committed Apr 17, 2023
1 parent 6fc129b commit 73fb782
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/re_set.rs
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/set.rs
Expand Up @@ -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());
}

0 comments on commit 73fb782

Please sign in to comment.