Writer

Writer is used to write content into Response:

#[async_trait]
pub trait Writer {
    async fn write(mut self, req: &mut Request, depot: &mut Depot, res: &mut Response);
}

Compared to Handler:

#[async_trait]
pub trait Handler: Send + Sync + 'static {
    async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl);
}

The main differences between them are:

  • Different purposes: Writer represents writing specific content into Response, implemented by concrete content such as strings, error messages, etc. In contrast, Handler is used to process the entire request.
  • Writer is created within a Handler and consumes itself when the write function is called, making it a one-time call. On the other hand, Handler is shared across all requests.
  • Writer can be returned as the content in the Result of a Handler.
  • Writer does not include a FlowCtrl parameter, so it cannot control the execution flow of the entire request.

Scribe implements Writer but offers fewer capabilities compared to Writer:

pub trait Scribe {
    fn render(self, res: &mut Response);
}

The rendering function of Scribe only writes data into Response and cannot retrieve information from Request or Depot during this process.