pub fn filter_async<'a, Pred, Input, Output, FnArgs, Descr>(
    pred: Pred
) -> Handler<'a, Input, Output, Descr> where
    Pred: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,
    Input: Send + 'a,
    Output: 'a,
    Descr: HandlerDescription
Expand description

The asynchronous version of filter.

Examples found in repository?
examples/simple_dispatcher.rs (line 86)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
fn ping_handler() -> CommandHandler {
    dptree::filter_async(|event: Event| async move { matches!(event, Event::Ping) })
        .endpoint(|| async { "Pong".to_string() })
}

fn set_value_handler() -> CommandHandler {
    dptree::filter_map_async(|event: Event| async move {
        match event {
            Event::SetValue(value) => Some(value),
            _ => None,
        }
    })
    .endpoint(move |value: i32, store: Arc<AtomicI32>| async move {
        store.store(value, Ordering::SeqCst);
        format!("{} stored", value)
    })
}

fn print_value_handler() -> CommandHandler {
    dptree::filter_async(|event: Event| async move { matches!(event, Event::PrintValue) }).endpoint(
        move |store: Arc<AtomicI32>| async move {
            let value = store.load(Ordering::SeqCst);
            format!("{}", value)
        },
    )
}
More examples
Hide additional examples
examples/descr_locations.rs (line 101)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
fn main() {
    #[rustfmt::skip]
    let some_tree: Handler<DependencyMap, _, _> = dptree::entry()
        .branch(
            dptree::filter(|| true)
                .endpoint(|| async {})
        )
        .branch(
            dptree::filter_async(|| async { true })
                .chain(dptree::filter_map(|| Some(1) ))
                .endpoint(|| async {})
        );

    get_locations(&some_tree).iter().for_each(|(name, loc)| println!("{name: <12} @ {loc}"));
}