Skip to main content

Dispatcher

Struct Dispatcher 

Source
pub struct Dispatcher<CallerCtx> { /* private fields */ }
Expand description

Dispatches a JmapRequest to registered method handlers.

Register handlers with Dispatcher::register, then call Dispatcher::dispatch per request. CallerCtx is cloned for each method call in the batch, so it must be Clone.

CallerCtx must also be 'static because each handler call is spawned as a tokio::task. To share non-static data (e.g. a database connection), wrap it in Arc<T>Arc is Clone + Send + 'static when T: Send + Sync.

§Thread safety

Dispatcher is both Send and Sync. Register handlers on one thread, then wrap in Arc and share across tasks — dispatch takes &self and is safe to call concurrently.

Implementations§

Source§

impl<CallerCtx: Clone + Send + 'static> Dispatcher<CallerCtx>

Source

pub fn new() -> Self

Create an empty dispatcher with no registered handlers.

Source

pub fn register( &mut self, method: impl Into<String>, handler: Arc<dyn JmapHandler<CallerCtx>>, )

Register a handler for the given method name.

Registering the same name twice replaces the earlier handler.

Using Arc rather than Box allows the same handler instance to be shared across multiple method name registrations (via Arc::clone).

Source

pub async fn dispatch( &self, request: JmapRequest, caller: CallerCtx, session_state: State, ) -> JmapResponse

Process a validated JmapRequest and return a JmapResponse.

Method calls are processed sequentially per RFC 8620 §3.3. Each handler runs in a tokio::task::spawn for panic isolation: a panicking handler returns a serverFail invocation rather than crashing the connection task.

CallerCtx must be Clone + Send + 'static; see the struct-level doc.

§Cancellation

If this future is dropped while a handler task is running (e.g., the HTTP connection closes), the spawned task runs to completion — tokio does not cancel tasks when their JoinHandle is dropped. The handler result is discarded. Callers that need strict cancellation should implement it at the handler level (e.g., tokio::select! with a shutdown signal).

Trait Implementations§

Source§

impl<CallerCtx> Debug for Dispatcher<CallerCtx>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<CallerCtx: Clone + Send + 'static> Default for Dispatcher<CallerCtx>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<CallerCtx> Freeze for Dispatcher<CallerCtx>

§

impl<CallerCtx> !RefUnwindSafe for Dispatcher<CallerCtx>

§

impl<CallerCtx> Send for Dispatcher<CallerCtx>

§

impl<CallerCtx> Sync for Dispatcher<CallerCtx>

§

impl<CallerCtx> Unpin for Dispatcher<CallerCtx>

§

impl<CallerCtx> UnsafeUnpin for Dispatcher<CallerCtx>

§

impl<CallerCtx> !UnwindSafe for Dispatcher<CallerCtx>

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.