Writer
Writer
is used to write content to 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);
}
Their differences are:
- Different uses,
Writer
represents writing specific content toResponse
, which is implemented by specific content, such as strings, error messages, etc.Handler
is used to process the entire request . Writer
is created inHandler
, it will consume itself when thewrite
function is called, it is a one-time call. AndHandler
is common to all requests;Writer
can be used as the content ofResult
returned byHandler
;- The
FlowCtrl
parameter does not exist inWriter
, and cannot control the execution flow of the entire request.
Scribe
implements Writer
, but with less functionality than Writer
:
pub trait Scribe {
fn render(self, res: &mut Response);
}
Scribe
's rendering function just writes data to Response
, this process cannot get information from Request
or Depot
.