# HG changeset patch # User Raphaël Gomès # Date 2019-11-29 17:33:56 # Node ID 1bb4e9b029848f6a5e05c7ce8dd1b93b175a6407 # Parent 088ba9d9407937b1430a5e2b3def0d245eb0ee2a rust-matchers: improve `Matcher` trait ergonomics `VisitChildrenSet` has no need to own the set, this will save allocations. The `file_set` return type change is motivated by both ergonomics and... being able to compile code. The `AlwaysMatcher` does not store a `file_set`, which requires it to return an owned `HashSet`, which in turn would change our return type to `Cow<&HgPath>` (lifetimes omitted). This is both un-ergonomic and troublesome for more complex lifetime issues (especially with the upcoming `FileMatcher` in the following patch). Differential Revision: https://phab.mercurial-scm.org/D7525 diff --git a/rust/hg-core/src/matchers.rs b/rust/hg-core/src/matchers.rs --- a/rust/hg-core/src/matchers.rs +++ b/rust/hg-core/src/matchers.rs @@ -10,21 +10,21 @@ use crate::utils::hg_path::{HgPath, HgPathBuf}; use std::collections::HashSet; -pub enum VisitChildrenSet { +pub enum VisitChildrenSet<'a> { /// Don't visit anything Empty, /// Only visit this directory This, /// Visit this directory and these subdirectories /// TODO Should we implement a `NonEmptyHashSet`? - Set(HashSet), + Set(HashSet<&'a HgPath>), /// Visit this directory and all subdirectories Recursive, } pub trait Matcher { /// Explicitly listed files - fn file_set(&self) -> HashSet<&HgPath>; + fn file_set(&self) -> Option<&HashSet<&HgPath>>; /// Returns whether `filename` is in `file_set` fn exact_match(&self, filename: impl AsRef) -> bool; /// Returns whether `filename` is matched by this matcher @@ -82,8 +82,8 @@ pub trait Matcher { pub struct AlwaysMatcher; impl Matcher for AlwaysMatcher { - fn file_set(&self) -> HashSet<&HgPath> { - HashSet::new() + fn file_set(&self) -> Option<&HashSet<&HgPath>> { + None } fn exact_match(&self, _filename: impl AsRef) -> bool { false