Skip to main content

Dispatcher

Struct Dispatcher 

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

A dispatcher that owns the components used to select and wrap a handler.

The dispatch table is supplied for each call, so the dispatcher itself does not own application data or state. This makes it suitable as a small core for higher-level abstractions such as a state machine.

Implementations§

Source§

impl<M, W> Dispatcher<M, W>

Source

pub const fn new(matcher: M, middleware: W) -> Self

Creates a dispatcher from a matcher and middleware.

Examples found in repository?
examples/command_dispatch.rs (line 117)
101fn main() {
102    let routes = [
103        Route {
104            command: Command::Read,
105            operation: Operation::Read,
106        },
107        Route {
108            command: Command::Write,
109            operation: Operation::Double,
110        },
111    ];
112
113    let middleware = SecurityAndAudit {
114        before_count: 0,
115        after_count: 0,
116    };
117    let mut dispatcher = Dispatcher::new(CommandMatcher, middleware);
118
119    let request = Request {
120        command: Command::Write,
121        authenticated: true,
122        value: 21,
123    };
124
125    // 也可以写成 dispatch_core::dispatch(&mut dispatcher, &routes, &request)。
126    let response = dispatcher.dispatch(&routes, &request).unwrap();
127
128    assert_eq!(
129        response,
130        Response {
131            value: 42,
132            audited: true,
133        }
134    );
135    assert_eq!(dispatcher.middleware().before_count, 1);
136    assert_eq!(dispatcher.middleware().after_count, 1);
137
138    println!("value={}, audited={}", response.value, response.audited);
139}
Source

pub fn dispatch<Item, Input, Output, Error>( &mut self, table: &[Item], input: &Input, ) -> Result<Output, DispatchError<Error>>
where M: Matcher<Item, Input>, W: Middleware<Item, Input, Output, Error>, Item: Handler<Input = Input, Output = Output, Error = Error>,

Dispatches an input to the first matching item in table.

Examples found in repository?
examples/command_dispatch.rs (line 126)
101fn main() {
102    let routes = [
103        Route {
104            command: Command::Read,
105            operation: Operation::Read,
106        },
107        Route {
108            command: Command::Write,
109            operation: Operation::Double,
110        },
111    ];
112
113    let middleware = SecurityAndAudit {
114        before_count: 0,
115        after_count: 0,
116    };
117    let mut dispatcher = Dispatcher::new(CommandMatcher, middleware);
118
119    let request = Request {
120        command: Command::Write,
121        authenticated: true,
122        value: 21,
123    };
124
125    // 也可以写成 dispatch_core::dispatch(&mut dispatcher, &routes, &request)。
126    let response = dispatcher.dispatch(&routes, &request).unwrap();
127
128    assert_eq!(
129        response,
130        Response {
131            value: 42,
132            audited: true,
133        }
134    );
135    assert_eq!(dispatcher.middleware().before_count, 1);
136    assert_eq!(dispatcher.middleware().after_count, 1);
137
138    println!("value={}, audited={}", response.value, response.audited);
139}
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.

Source

pub const fn middleware(&self) -> &W

Returns a shared reference to the middleware.

Examples found in repository?
examples/command_dispatch.rs (line 135)
101fn main() {
102    let routes = [
103        Route {
104            command: Command::Read,
105            operation: Operation::Read,
106        },
107        Route {
108            command: Command::Write,
109            operation: Operation::Double,
110        },
111    ];
112
113    let middleware = SecurityAndAudit {
114        before_count: 0,
115        after_count: 0,
116    };
117    let mut dispatcher = Dispatcher::new(CommandMatcher, middleware);
118
119    let request = Request {
120        command: Command::Write,
121        authenticated: true,
122        value: 21,
123    };
124
125    // 也可以写成 dispatch_core::dispatch(&mut dispatcher, &routes, &request)。
126    let response = dispatcher.dispatch(&routes, &request).unwrap();
127
128    assert_eq!(
129        response,
130        Response {
131            value: 42,
132            audited: true,
133        }
134    );
135    assert_eq!(dispatcher.middleware().before_count, 1);
136    assert_eq!(dispatcher.middleware().after_count, 1);
137
138    println!("value={}, audited={}", response.value, response.audited);
139}
Source

pub fn middleware_mut(&mut self) -> &mut W

Returns a mutable reference to the middleware.

Source

pub fn into_parts(self) -> (M, W)

Splits the dispatcher into its matcher and middleware.

Auto Trait Implementations§

§

impl<M, W> Freeze for Dispatcher<M, W>
where M: Freeze, W: Freeze,

§

impl<M, W> RefUnwindSafe for Dispatcher<M, W>

§

impl<M, W> Send for Dispatcher<M, W>
where M: Send, W: Send,

§

impl<M, W> Sync for Dispatcher<M, W>
where M: Sync, W: Sync,

§

impl<M, W> Unpin for Dispatcher<M, W>
where M: Unpin, W: Unpin,

§

impl<M, W> UnsafeUnpin for Dispatcher<M, W>
where M: UnsafeUnpin, W: UnsafeUnpin,

§

impl<M, W> UnwindSafe for Dispatcher<M, W>
where M: UnwindSafe, W: UnwindSafe,

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.