pub struct IndexedFirstMatch<T: ?Sized, Decision> { /* private fields */ }Expand description
Ordered decisions indexed by caller-declared necessary string keys.
The key projection runs once for each call to DecisionSpecification::decide.
Evaluation considers only the matching key bucket and unkeyed rules, merging
both groups in insertion order and stopping at the first match. Bucket
lookup uses borrowed str access and index evaluation adds no allocation.
A keyed rule’s key is a necessary condition: its specification must never
match a candidate whose projected key differs from the registered key.
This cannot be checked for arbitrary specifications. Rules without such a
sound key must be registered with push_unkeyed.
The stored specifications require Send + Sync, so a completed index can
be shared by worker threads whenever its decisions are also Sync.
use specification_core::{DecisionSpecification, IndexedFirstMatch};
struct File<'a> { name: &'a str, executable: bool }
let mut routes = IndexedFirstMatch::new(|file: &File<'_>| file.name);
routes.push_keyed(
"Cargo.toml",
|file: &File<'_>| file.name == "Cargo.toml" && file.executable,
"manifest",
);
routes.push_unkeyed(|file: &File<'_>| file.name.ends_with(".lock"), "lockfile");
let manifest = File { name: "Cargo.toml", executable: true };
let lock = File { name: "Cargo.lock", executable: false };
assert_eq!(routes.decide(&manifest), Some(&"manifest"));
assert_eq!(routes.decide(&lock), Some(&"lockfile"));
assert!(routes.may_match("Cargo.lock"));Implementations§
Source§impl<T: ?Sized, Decision> IndexedFirstMatch<T, Decision>
impl<T: ?Sized, Decision> IndexedFirstMatch<T, Decision>
Sourcepub fn new<Projection>(key_of: Projection) -> Self
pub fn new<Projection>(key_of: Projection) -> Self
Creates an empty index using a borrowed string-key projection.
The projection must be stable for the candidate during one evaluation.
Sourcepub fn push_keyed<Concrete>(
&mut self,
key: impl Into<String>,
specification: Concrete,
decision: Decision,
)
pub fn push_keyed<Concrete>( &mut self, key: impl Into<String>, specification: Concrete, decision: Decision, )
Adds a rule to the bucket for its necessary key.
The specification must return true only for candidates whose
projected key equals key. Duplicate keys are supported and retain
insertion order.
Sourcepub fn push_unkeyed<Concrete>(
&mut self,
specification: Concrete,
decision: Decision,
)
pub fn push_unkeyed<Concrete>( &mut self, specification: Concrete, decision: Decision, )
Adds a rule considered for every candidate, in insertion order.