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>
impl<M, W> Dispatcher<M, W>
Sourcepub const fn new(matcher: M, middleware: W) -> Self
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}Sourcepub 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>,
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}Sourcepub fn matcher_mut(&mut self) -> &mut M
pub fn matcher_mut(&mut self) -> &mut M
Returns a mutable reference to the matcher.
Sourcepub const fn middleware(&self) -> &W
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}Sourcepub fn middleware_mut(&mut self) -> &mut W
pub fn middleware_mut(&mut self) -> &mut W
Returns a mutable reference to the middleware.
Sourcepub fn into_parts(self) -> (M, W)
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>
impl<M, W> RefUnwindSafe for Dispatcher<M, W>where
M: RefUnwindSafe,
W: RefUnwindSafe,
impl<M, W> Send for Dispatcher<M, W>
impl<M, W> Sync for Dispatcher<M, W>
impl<M, W> Unpin for Dispatcher<M, W>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more