Struct postcard_rpc::Dispatch

source ·
pub struct Dispatch<Context, Error, const N: usize> { /* private fields */ }
Expand description

Dispatch is the primary interface for MCU “server” devices.

Dispatch is generic over three types:

  1. The Context, which will be passed as a mutable reference to each of the handlers. It typically should contain whatever resource is necessary to send replies back to the host.
  2. The Error type, which can be returned by handlers
  3. N, for the maximum number of handlers

If you plan to use COBS encoding, you can also use CobsDispatch. which will automatically handle accumulating bytes from the wire.

Note: This will be available when the cobs or cobs-serial feature is enabled.

Implementations§

source§

impl<Context, Err, const N: usize> Dispatch<Context, Err, N>

source

pub fn new(c: Context) -> Self

Create a new Dispatch

source

pub fn add_handler<E: Endpoint>( &mut self, handler: fn(_: &WireHeader, _: &mut Context, _: &[u8]) -> Result<(), Err> ) -> Result<(), &'static str>

Add a handler to the Dispatch for the given path and type

Returns an error if the given type+path have already been added, or if Dispatch is full.

source

pub fn context(&mut self) -> &mut Context

Accessor function for the Context field

source

pub fn dispatch(&mut self, bytes: &[u8]) -> Result<(), Error<Err>>

Attempt to dispatch the given message

The bytes should consist of exactly one message (including the header).

Returns an error in any of the following cases:

  • We failed to decode a header
  • No handler was found for the decoded key
  • The handler ran, but returned an error

Auto Trait Implementations§

§

impl<Context, Error, const N: usize> Freeze for Dispatch<Context, Error, N>
where Context: Freeze,

§

impl<Context, Error, const N: usize> RefUnwindSafe for Dispatch<Context, Error, N>
where Context: RefUnwindSafe,

§

impl<Context, Error, const N: usize> Send for Dispatch<Context, Error, N>
where Context: Send,

§

impl<Context, Error, const N: usize> Sync for Dispatch<Context, Error, N>
where Context: Sync,

§

impl<Context, Error, const N: usize> Unpin for Dispatch<Context, Error, N>
where Context: Unpin,

§

impl<Context, Error, const N: usize> UnwindSafe for Dispatch<Context, Error, N>
where Context: 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more