mystor / synstructure

Utilities for dealing with substructures within syn macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement something akin to `Iterator::partition` for `Structure`

pvdrz opened this issue · comments

I have this use case where you need to do different things to two disjoint subsets of the fields of a struct. This can be achieved by using filter twice:

structure.clone().filter(|f| predicate(f)).each(do_something);

structure.filter(|f| !predicate(f)).each(do_something_else);

I wonder if it would be possible to add a method like:

impl Structure {
    ...
    pub fn partition<F>(self, f: F) -> (Self, Self) whereF: FnMut(&BindingInfo<'_>) -> bool {
        ...    
    }
}

that returns two structures, one with the fields where f was true and another one with the fields where f was false.

PD: I'd be happy to implement this if it's considered relevant enough to be included.