Skip to main content

Dispatcher

Struct Dispatcher 

Source
pub struct Dispatcher<'table, K, C, R, O, E, M, B = NoBefore, A = NoAfter, S = IdentityResponse> { /* private fields */ }
Expand description

A borrowed command table paired with a matching strategy.

Table order defines priority: the first matching entry is executed.

Implementations§

Source§

impl<'table, K, C, R, O, E, M> Dispatcher<'table, K, C, R, O, E, M, NoBefore, NoAfter, IdentityResponse>

Source

pub const fn new( table: &'table [CommandEntry<K, C, R, O, E>], matcher: M, ) -> Self

Creates a dispatcher over table.

Examples found in repository?
examples/command_dispatch.rs (line 135)
124fn main() {
125    let mut context = Context {
126        opcode: Opcode::SignAndSend,
127        payload: 40,
128    };
129    let mut resources = Resources {
130        io: Io::default(),
131        crypto: Crypto { key: 2 },
132        audit: Audit::default(),
133    };
134
135    let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136        .with_before(before)
137        .with_after(after)
138        .with_response(response);
139    let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141    assert_eq!(
142        response,
143        WireResponse {
144            status: 0,
145            value: 42,
146        }
147    );
148    assert_eq!(resources.io.last_value, 42);
149    assert_eq!(resources.io.writes, 1);
150    assert_eq!(resources.audit.before_calls, 1);
151    assert_eq!(resources.audit.after_calls, 1);
152    assert_eq!(resources.audit.response_calls, 1);
153    println!(
154        "status={}, value={}, writes={}",
155        response.status, response.value, resources.io.writes
156    );
157}
Source§

impl<'table, K, C, R, O, E, M, B, A, S> Dispatcher<'table, K, C, R, O, E, M, B, A, S>

Source

pub fn with_before<F, Params>( self, function: F, ) -> Dispatcher<'table, K, C, R, O, E, M, BeforeFn<F, Params>, A, S>
where F: Handler<C, R, (), E, Params>,

Installs a resource-injected before function.

This does not install an after function. Calling with_before again replaces only the previous before stage.

Examples found in repository?
examples/command_dispatch.rs (line 136)
124fn main() {
125    let mut context = Context {
126        opcode: Opcode::SignAndSend,
127        payload: 40,
128    };
129    let mut resources = Resources {
130        io: Io::default(),
131        crypto: Crypto { key: 2 },
132        audit: Audit::default(),
133    };
134
135    let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136        .with_before(before)
137        .with_after(after)
138        .with_response(response);
139    let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141    assert_eq!(
142        response,
143        WireResponse {
144            status: 0,
145            value: 42,
146        }
147    );
148    assert_eq!(resources.io.last_value, 42);
149    assert_eq!(resources.io.writes, 1);
150    assert_eq!(resources.audit.before_calls, 1);
151    assert_eq!(resources.audit.after_calls, 1);
152    assert_eq!(resources.audit.response_calls, 1);
153    println!(
154        "status={}, value={}, writes={}",
155        response.status, response.value, resources.io.writes
156    );
157}
Source

pub fn with_after<F, Params>( self, function: F, ) -> Dispatcher<'table, K, C, R, O, E, M, B, AfterFn<F, Params>, S>
where F: AfterHandler<C, R, O, E, Params>,

Installs a resource-injected after function.

The function receives mutable access to a successful command output. This does not install a before function.

Examples found in repository?
examples/command_dispatch.rs (line 137)
124fn main() {
125    let mut context = Context {
126        opcode: Opcode::SignAndSend,
127        payload: 40,
128    };
129    let mut resources = Resources {
130        io: Io::default(),
131        crypto: Crypto { key: 2 },
132        audit: Audit::default(),
133    };
134
135    let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136        .with_before(before)
137        .with_after(after)
138        .with_response(response);
139    let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141    assert_eq!(
142        response,
143        WireResponse {
144            status: 0,
145            value: 42,
146        }
147    );
148    assert_eq!(resources.io.last_value, 42);
149    assert_eq!(resources.io.writes, 1);
150    assert_eq!(resources.audit.before_calls, 1);
151    assert_eq!(resources.audit.after_calls, 1);
152    assert_eq!(resources.audit.response_calls, 1);
153    println!(
154        "status={}, value={}, writes={}",
155        response.status, response.value, resources.io.writes
156    );
157}
Source

pub fn with_response<F, Params, T>( self, function: F, ) -> ResponseDispatcher<'table, K, C, R, O, E, M, B, A, F, Params, T>
where F: ResponseHandler<C, R, O, T, E, Params>,

Installs a resource-injected successful-response function.

The function consumes the command output after the after stage and may convert it to a different final type.

Examples found in repository?
examples/command_dispatch.rs (line 138)
124fn main() {
125    let mut context = Context {
126        opcode: Opcode::SignAndSend,
127        payload: 40,
128    };
129    let mut resources = Resources {
130        io: Io::default(),
131        crypto: Crypto { key: 2 },
132        audit: Audit::default(),
133    };
134
135    let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136        .with_before(before)
137        .with_after(after)
138        .with_response(response);
139    let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141    assert_eq!(
142        response,
143        WireResponse {
144            status: 0,
145            value: 42,
146        }
147    );
148    assert_eq!(resources.io.last_value, 42);
149    assert_eq!(resources.io.writes, 1);
150    assert_eq!(resources.audit.before_calls, 1);
151    assert_eq!(resources.audit.after_calls, 1);
152    assert_eq!(resources.audit.response_calls, 1);
153    println!(
154        "status={}, value={}, writes={}",
155        response.status, response.value, resources.io.writes
156    );
157}
Source

pub fn select(&self, context: &C) -> Option<&'table CommandEntry<K, C, R, O, E>>
where M: Matcher<K, C>,

Returns the first command accepted by the matcher.

Source

pub fn dispatch( &self, context: &mut C, resources: &mut R, ) -> Result<S::Output, DispatchError<E>>
where M: Matcher<K, C>, B: Before<C, R, E>, A: After<C, R, O, E>, S: ResponseStage<C, R, O, E>,

Selects a command and runs the configured execution pipeline.

Execution stops at the first failure. The after stage runs only after a successful command, and response handling runs only after a successful after stage.

Examples found in repository?
examples/command_dispatch.rs (line 139)
124fn main() {
125    let mut context = Context {
126        opcode: Opcode::SignAndSend,
127        payload: 40,
128    };
129    let mut resources = Resources {
130        io: Io::default(),
131        crypto: Crypto { key: 2 },
132        audit: Audit::default(),
133    };
134
135    let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136        .with_before(before)
137        .with_after(after)
138        .with_response(response);
139    let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141    assert_eq!(
142        response,
143        WireResponse {
144            status: 0,
145            value: 42,
146        }
147    );
148    assert_eq!(resources.io.last_value, 42);
149    assert_eq!(resources.io.writes, 1);
150    assert_eq!(resources.audit.before_calls, 1);
151    assert_eq!(resources.audit.after_calls, 1);
152    assert_eq!(resources.audit.response_calls, 1);
153    println!(
154        "status={}, value={}, writes={}",
155        response.status, response.value, resources.io.writes
156    );
157}
Source

pub const fn table(&self) -> &'table [CommandEntry<K, C, R, O, E>]

Returns the borrowed command table.

Source

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

Returns the matcher.

Source

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

Returns a mutable matcher reference for between-dispatch configuration.

Source

pub const fn before(&self) -> &B

Returns the configured before stage.

Source

pub fn before_mut(&mut self) -> &mut B

Returns a mutable reference to the configured before stage.

Source

pub const fn after(&self) -> &A

Returns the configured after stage.

Source

pub fn after_mut(&mut self) -> &mut A

Returns a mutable reference to the configured after stage.

Source

pub const fn response(&self) -> &S

Returns the configured response stage.

Source

pub fn response_mut(&mut self) -> &mut S

Returns a mutable reference to the configured response stage.

Auto Trait Implementations§

§

impl<'table, K, C, R, O, E, M, B, A, S> Freeze for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: Freeze, M: Freeze, B: Freeze, A: Freeze, S: Freeze,

§

impl<'table, K, C, R, O, E, M, B, A, S> RefUnwindSafe for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: RefUnwindSafe, M: RefUnwindSafe, B: RefUnwindSafe, A: RefUnwindSafe, S: RefUnwindSafe,

§

impl<'table, K, C, R, O, E, M, B, A, S> Send for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: Send, M: Send, B: Send, A: Send, S: Send,

§

impl<'table, K, C, R, O, E, M, B, A, S> Sync for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: Sync, M: Sync, B: Sync, A: Sync, S: Sync,

§

impl<'table, K, C, R, O, E, M, B, A, S> Unpin for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: Unpin, M: Unpin, B: Unpin, A: Unpin, S: Unpin,

§

impl<'table, K, C, R, O, E, M, B, A, S> UnsafeUnpin for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: UnsafeUnpin, M: UnsafeUnpin, B: UnsafeUnpin, A: UnsafeUnpin, S: UnsafeUnpin,

§

impl<'table, K, C, R, O, E, M, B, A, S> UnwindSafe for Dispatcher<'table, K, C, R, O, E, M, B, A, S>
where &'table [CommandEntry<K, C, R, O, E>]: UnwindSafe, M: UnwindSafe, B: UnwindSafe, A: UnwindSafe, S: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.