Enum pharos::Filter[][src]

pub enum Filter<Event> where
    Event: Clone + 'static + Send
{ Pointer(fn(_: &Event) -> bool), Closure(Box<dyn FnMut(&Event) -> bool + Send>), }
Expand description

Predicate for filtering events.

This is an enum because closures that capture variables from their environment need to be boxed. More often than not, an event will be a simple enum and the predicate will just match on the variant, so it would be wasteful to impose boxing in those cases, hence there is a function pointer variant which does not require boxing. This should be preferred where possible.

use pharos::*;

let a = 5;

// This closure captures the a variable from it's environment.
// We can still use it as a filter by boxing it with `closure`.
//
// Note: it depends on the circumstances, but often enough, we need to
// annotate the type of the event parameter to the predicate.
//
// For this example we use bool as event type for simplicity, but it works
// just the same if that's an enum.
//
let predicate = move |_: &bool| { a; true };

let filter = Filter::Closure( Box::new(predicate) );

// This one does not capture anything, so it can be stored as a function pointer
// without boxing.
//
let predicate = move |_: &bool| { true };

let filter = Filter::Pointer( predicate );

// You can also use actual functions as filters.
//
fn predicate_function( event: &bool ) -> bool { true }

let filter = Filter::Pointer( predicate_function );

Variants

Pointer(fn(_: &Event) -> bool)

A function pointer to a predicate to filter events.

Closure(Box<dyn FnMut(&Event) -> bool + Send>)

A boxed closure to a predicate to filter events.

Trait Implementations

Formats the value using the given formatter. Read more

Create a ObserveConfig from a Filter, getting default values for other options.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.