Skip to main content

Module rocket

Module rocket 

Source
Expand description

rocket 0.5 集成。

用 manage 注册,处理器把它写在参数里:

use rocket::{get, routes, Build, Rocket};
use encryption::guard::Guard;

#[get("/")]
fn store_phone(guard: Guard) -> String {
    guard.encrypt("13800138000".as_bytes()).unwrap().len().to_string()
}

let guard: Guard = todo!();
let rocket: Rocket<Build> = rocket::build().manage(guard).mount("/", routes![store_phone]);

rocket 的请求守卫走 Outcome,且 trait 是 #[rocket::async_trait] 的 (rocket 自己 re-export 了那个宏,所以不必额外依赖 async-trait)。

§托管状态里放的是 Guard 本身

Guard 内部已经是 Arc,克隆一次只是引用计数递增,所以这里是 manage(guard) 而不是 manage(Arc::new(guard)) —— rocket 的守卫拿到的是 借用,本来也不需要外面再套一层共享。