Skip to main content

Dispatcher

Struct Dispatcher 

Source
pub struct Dispatcher<'a, 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<'a, Item, M> Dispatcher<'a, Item, M>

Source

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

Creates a dispatcher backed by table.

Examples found in repository?
examples/command_dispatch.rs (line 83)
72fn main() {
73    let routes = [
74        Route {
75            command: Command::Read,
76            handler: read,
77        },
78        Route {
79            command: Command::Write,
80            handler: double,
81        },
82    ];
83    let dispatcher = Dispatcher::new(&routes, CommandMatcher);
84    let mut context = AppContext::default();
85    let request = Request {
86        command: Command::Write,
87        value: 21,
88    };
89
90    let response = dispatcher.dispatch(&request, &mut context).unwrap();
91
92    assert_eq!(response, Response { value: 42 });
93    assert_eq!(context.execution_count, 1);
94    println!(
95        "value={}, execution_count={}",
96        response.value, context.execution_count
97    );
98}
Source

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

Returns the first item accepted by the matcher.

This method performs selection only and does not invoke the item’s Handler.

Source

pub fn dispatch( &self, input: &Item::Input, context: &mut Item::Context, ) -> Result<Item::Output, DispatchError<Item::Error>>
where Item: Handler, M: Matcher<Item, Item::Input>,

Selects and executes the first item accepted by the matcher.

input is immutable for the entire operation. Runtime state and other mutable resources should be carried by context instead.

Examples found in repository?
examples/command_dispatch.rs (line 90)
72fn main() {
73    let routes = [
74        Route {
75            command: Command::Read,
76            handler: read,
77        },
78        Route {
79            command: Command::Write,
80            handler: double,
81        },
82    ];
83    let dispatcher = Dispatcher::new(&routes, CommandMatcher);
84    let mut context = AppContext::default();
85    let request = Request {
86        command: Command::Write,
87        value: 21,
88    };
89
90    let response = dispatcher.dispatch(&request, &mut context).unwrap();
91
92    assert_eq!(response, Response { value: 42 });
93    assert_eq!(context.execution_count, 1);
94    println!(
95        "value={}, execution_count={}",
96        response.value, context.execution_count
97    );
98}
Source

pub const fn table(&self) -> &'a [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) -> (&'a [Item], M)

Splits the dispatcher into its table and matcher.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<'a, Item, M> UnwindSafe for Dispatcher<'a, 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.