寫入器

Writer 用於將內容寫入 Response

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

與處理器相比:

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

兩者的主要差異在於:

  • 用途不同:Writer 代表將具體內容寫入 Response,由特定內容實現,例如字串、錯誤訊息等。而 Handler 用於處理整個請求。
  • WriterHandler 中創建,並在 write 函數調用時消耗自身,屬於一次性調用。而 Handler 是所有請求共用的。
  • Writer 可作為 Handler 返回的 Result 中的內容。
  • Writer 中沒有 FlowCtrl 參數,無法控制整個請求的執行流程。

Scribe 實現了 Writer,但功能比 Writer 更為有限:

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

Scribe 的渲染函數僅將數據寫入 Response,此過程無法從 RequestDepot 中獲取資訊。