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:
- 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. - The
Error
type, which can be returned by handlers 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>
impl<Context, Err, const N: usize> Dispatch<Context, Err, N>
sourcepub fn add_handler<E: Endpoint>(
&mut self,
handler: fn(_: &WireHeader, _: &mut Context, _: &[u8]) -> Result<(), Err>
) -> Result<(), &'static str>
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.
sourcepub fn context(&mut self) -> &mut Context
pub fn context(&mut self) -> &mut Context
Accessor function for the Context field
sourcepub fn dispatch(&mut self, bytes: &[u8]) -> Result<(), Error<Err>>
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> 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