66// Common process matcher logic shared by pgrep, pkill and pidwait
77
88use std:: collections:: HashSet ;
9+ use std:: hash:: Hash ;
910
1011use clap:: { arg, Arg , ArgAction , ArgMatches } ;
1112use regex:: Regex ;
@@ -25,7 +26,7 @@ pub struct Settings {
2526 pub newest : bool ,
2627 pub oldest : bool ,
2728 pub older : Option < u64 > ,
28- pub parent : Option < Vec < u64 > > ,
29+ pub parent : Option < HashSet < u64 > > ,
2930 pub runstates : Option < String > ,
3031 pub terminal : Option < HashSet < Teletype > > ,
3132 #[ cfg( unix) ]
@@ -137,6 +138,10 @@ fn try_get_pattern_from(matches: &ArgMatches) -> UResult<String> {
137138 Ok ( pattern. to_string ( ) )
138139}
139140
141+ fn any_matches < T : Eq + Hash > ( optional_ids : & Option < HashSet < T > > , id : T ) -> bool {
142+ optional_ids. as_ref ( ) . is_none_or ( |ids| ids. contains ( & id) )
143+ }
144+
140145/// Collect pids with filter construct from command line arguments
141146fn collect_matched_pids ( settings : & Settings ) -> Vec < ProcessInformation > {
142147 // Filtration general parameters
@@ -171,18 +176,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
171176 settings. regex . is_match ( want)
172177 } ;
173178
174- let tty_matched = match & settings. terminal {
175- Some ( ttys) => ttys. contains ( & pid. tty ( ) ) ,
176- None => true ,
177- } ;
179+ let tty_matched = any_matches ( & settings. terminal , pid. tty ( ) ) ;
178180
179181 let arg_older = settings. older . unwrap_or ( 0 ) ;
180182 let older_matched = pid. start_time ( ) . unwrap ( ) >= arg_older;
181183
182- let parent_matched = match ( & settings. parent , pid. ppid ( ) ) {
183- ( Some ( parents) , Ok ( ppid) ) => parents. contains ( & ppid) ,
184- _ => true ,
185- } ;
184+ let parent_matched = any_matches ( & settings. parent , pid. ppid ( ) . unwrap ( ) ) ;
186185
187186 if ( run_state_matched
188187 && pattern_matched
0 commit comments