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>
impl<'table, Item, M> Dispatcher<'table, Item, M>
Sourcepub const fn new(table: &'table [Item], matcher: M) -> Self
pub const fn new(table: &'table [Item], matcher: M) -> Self
Creates a dispatcher backed by table.
Examples found in repository?
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}Sourcepub fn select<Input>(&self, input: &Input) -> Option<&'table Item>
pub fn select<Input>(&self, input: &Input) -> Option<&'table Item>
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.
Sourcepub 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>,
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?
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}Sourcepub fn matcher_mut(&mut self) -> &mut M
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.
Sourcepub fn into_parts(self) -> (&'table [Item], M)
pub fn into_parts(self) -> (&'table [Item], M)
Splits the dispatcher into its table and matcher.