Skip to main content

Dispatcher

Struct Dispatcher 

Source
pub struct Dispatcher<'table, Item, M> { /* private fields */ }
Expand description

A dispatcher backed by a borrowed table and a matching strategy.

The table is scanned from beginning to end. The first matching item is selected, so table order defines dispatch priority.

Implementations§

Source§

impl<'table, Item, M> Dispatcher<'table, Item, M>

Source

pub const fn new(table: &'table [Item], matcher: M) -> Self

Creates a dispatcher backed by table.

Examples found in repository?
examples/command_dispatch.rs (line 68)
57fn main() {
58    let routes = [
59        Route {
60            command: Command::Read,
61            handler: read,
62        },
63        Route {
64            command: Command::Write,
65            handler: double,
66        },
67    ];
68    let dispatcher = Dispatcher::new(&routes, CommandMatcher);
69    let mut state = AppState::default();
70    let request = Request {
71        command: Command::Write,
72        value: 21,
73    };
74
75    let response = dispatcher
76        .dispatch(&request, |route, request| {
77            (route.handler)(request, &mut state)
78        })
79        .unwrap();
80
81    assert_eq!(response, Response { value: 42 });
82    assert_eq!(state.execution_count, 1);
83    println!(
84        "value={}, execution_count={}",
85        response.value, state.execution_count
86    );
87}
Source

pub fn select<Input>(&self, input: &Input) -> Option<&'table Item>
where Input: ?Sized, M: Matcher<Item, Input>,

Returns the first item accepted by the matcher.

This method performs selection only. It does not invoke an executor or otherwise operate on the selected item.

Source

pub fn dispatch<'input, Input, Output, ExecuteError, Execute>( &self, input: &'input Input, execute: Execute, ) -> Result<Output, DispatchError<ExecuteError>>
where Input: ?Sized, M: Matcher<Item, Input>, Execute: FnOnce(&'table Item, &'input Input) -> Result<Output, ExecuteError>,

Selects an item and delegates execution to execute.

The executor receives the selected item and the original input. It can capture arbitrary per-call state and borrowed resources without making them part of the dispatcher’s type. The executor is not invoked when no item matches.

Examples found in repository?
examples/command_dispatch.rs (lines 76-78)
57fn main() {
58    let routes = [
59        Route {
60            command: Command::Read,
61            handler: read,
62        },
63        Route {
64            command: Command::Write,
65            handler: double,
66        },
67    ];
68    let dispatcher = Dispatcher::new(&routes, CommandMatcher);
69    let mut state = AppState::default();
70    let request = Request {
71        command: Command::Write,
72        value: 21,
73    };
74
75    let response = dispatcher
76        .dispatch(&request, |route, request| {
77            (route.handler)(request, &mut state)
78        })
79        .unwrap();
80
81    assert_eq!(response, Response { value: 42 });
82    assert_eq!(state.execution_count, 1);
83    println!(
84        "value={}, execution_count={}",
85        response.value, state.execution_count
86    );
87}
Source

pub const fn table(&self) -> &'table [Item]

Returns the dispatch table.

Source

pub const fn matcher(&self) -> &M

Returns a shared reference to the matcher.

Source

pub fn matcher_mut(&mut self) -> &mut M

Returns a mutable reference to the matcher.

This can be used to reconfigure matching between dispatch calls. The matcher itself remains immutable while selection is in progress.

Source

pub fn into_parts(self) -> (&'table [Item], M)

Splits the dispatcher into its table and matcher.

Auto Trait Implementations§

§

impl<'table, Item, M> Freeze for Dispatcher<'table, Item, M>
where M: Freeze,

§

impl<'table, Item, M> RefUnwindSafe for Dispatcher<'table, Item, M>
where M: RefUnwindSafe, Item: RefUnwindSafe,

§

impl<'table, Item, M> Send for Dispatcher<'table, Item, M>
where M: Send, Item: Sync,

§

impl<'table, Item, M> Sync for Dispatcher<'table, Item, M>
where M: Sync, Item: Sync,

§

impl<'table, Item, M> Unpin for Dispatcher<'table, Item, M>
where M: Unpin,

§

impl<'table, Item, M> UnsafeUnpin for Dispatcher<'table, Item, M>
where M: UnsafeUnpin,

§

impl<'table, Item, M> UnwindSafe for Dispatcher<'table, Item, M>
where M: UnwindSafe, Item: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.