Hi there,
I'm trying to define a function that is able to apply a filter to a list of objects. The filter could be either a simple String
, a more complex structure or a closure. I tent to design this as an enum lie this:
pub enum ServiceFilter<F: Fn(&Value) -> bool> {
Name(String),
Object(Map<String, Value>),
Function(F),
}
However, when using this approach the generic to store the closure need to be added to the whole enum which makes the actual filter function requiring the generic as well like so:
pub fn filter_services<F>(filter: ServiceFilter<F>) -> Result<Vec<Value>>
where F: Fn(&Value) -> bool
{}
Which is kind of ok, but when using an filter enum variant w/o the closure I would still need to provide some arbitrary type to be passed for the generic even though the actual enum variant does not make any use of it:
let services = filter_services::<fn(&Value) -> bool>(ServiceFilter::Name("myName".to_string()));
Is there an alternative approach using the enum and without boxing the closure? I'd like to prevent creating 3 functions to cover the different filter possibilities if possible.
Thanks in advance for any hint.