From bc2180d1e4fd2c174b6be32ee357d1a30abb6175 Mon Sep 17 00:00:00 2001 From: Jeff Weiss Date: Fri, 6 Jan 2023 15:30:01 -0500 Subject: [PATCH] Now compiles against rocket-0.5.0-rc.2 --- Cargo.toml | 6 +++--- examples/dog_list/main.rs | 13 ++++++------- examples/visit_counter/main.rs | 17 ++++++++--------- src/lib.rs | 23 +++++++++++++---------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96e8146..27963f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "rocket_session" -version = "0.2.2" +version = "0.3.0" authors = ["Ondřej Hruška "] -edition = "2018" +edition = "2021" license = "MIT" description = "Rocket.rs plug-in for cookie-based sessions holding arbitrary data" repository = "https://git.ondrovo.com/packages/rocket_session" @@ -17,5 +17,5 @@ categories = [ [dependencies] rand = "0.8" -rocket = "0.4" +rocket = "0.5.0-rc.2" parking_lot = "0.11" diff --git a/examples/dog_list/main.rs b/examples/dog_list/main.rs index da9e1f5..d02dc62 100644 --- a/examples/dog_list/main.rs +++ b/examples/dog_list/main.rs @@ -1,21 +1,20 @@ -#![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; -use rocket::response::content::Html; +use rocket::response::content::RawHtml; use rocket::response::Redirect; type Session<'a> = rocket_session::Session<'a, Vec>; -fn main() { - rocket::ignite() +#[launch] +fn rocket() -> _ { + rocket::build() .attach(Session::fairing()) .mount("/", routes![index, add, remove]) - .launch(); } #[get("/")] -fn index(session: Session) -> Html { +fn index(session: Session) -> RawHtml { let mut page = String::new(); page.push_str( r#" @@ -38,7 +37,7 @@ fn index(session: Session) -> Html { } }); page.push_str(""); - Html(page) + RawHtml(page) } #[post("/add", data = "")] diff --git a/examples/visit_counter/main.rs b/examples/visit_counter/main.rs index e65d053..6502a1b 100644 --- a/examples/visit_counter/main.rs +++ b/examples/visit_counter/main.rs @@ -2,11 +2,10 @@ //! //! The expiry time is set to 10 seconds to illustrate how a session is cleared if inactive. -#![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; -use rocket::response::content::Html; +use rocket::response::content::RawHtml; use std::time::Duration; #[derive(Default, Clone)] @@ -18,8 +17,9 @@ struct SessionData { // It's convenient to define a type alias: type Session<'a> = rocket_session::Session<'a, SessionData>; -fn main() { - rocket::ignite() +#[launch] +fn rocket() -> _ { + rocket::build() .attach( Session::fairing() // 10 seconds of inactivity until session expires @@ -30,11 +30,10 @@ fn main() { .with_cookie_len(20), ) .mount("/", routes![index, about]) - .launch(); } #[get("/")] -fn index(session: Session) -> Html { +fn index(session: Session) -> RawHtml { // Here we build the entire response inside the 'tap' closure. // While inside, the session is locked to parallel changes, e.g. @@ -42,7 +41,7 @@ fn index(session: Session) -> Html { session.tap(|sess| { sess.visits1 += 1; - Html(format!( + RawHtml(format!( r##"

Home

@@ -55,14 +54,14 @@ fn index(session: Session) -> Html { } #[get("/about")] -fn about(session: Session) -> Html { +fn about(session: Session) -> RawHtml { // Here we return a value from the tap function and use it below let count = session.tap(|sess| { sess.visits2 += 1; sess.visits2 }); - Html(format!( + RawHtml(format!( r##"

About

diff --git a/src/lib.rs b/src/lib.rs index 44058da..5d408a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,12 @@ use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; -use rand::{Rng, rngs::OsRng}; +use rand::{rngs::OsRng, Rng}; use rocket::{ fairing::{self, Fairing, Info}, http::{Cookie, Status}, + outcome::Outcome, request::FromRequest, - Outcome, Request, Response, Rocket, State, + Build, Request, Response, Rocket, State, }; use std::borrow::Cow; @@ -114,19 +115,20 @@ where D: 'static + Sync + Send + Default, { /// The shared state reference - store: State<'a, SessionStore>, + store: &'a State>, /// Session ID id: &'a SessionID, } -impl<'a, 'r, D> FromRequest<'a, 'r> for Session<'a, D> +#[rocket::async_trait] +impl<'r, D> FromRequest<'r> for Session<'r, D> where D: 'static + Sync + Send + Default, { type Error = (); - fn from_request(request: &'a Request<'r>) -> Outcome { - let store: State> = request.guard().unwrap(); + async fn from_request(request: &'r Request<'_>) -> Outcome { + let store = request.guard::<&State>>().await.unwrap(); Outcome::Success(Session { id: request.local_cache(|| { let store_ug = store.inner.upgradable_read(); @@ -198,7 +200,7 @@ where new_id } }), - store, + store: store, }) } } @@ -295,6 +297,7 @@ where } } +#[rocket::async_trait] impl Fairing for SessionFairing where D: 'static + Sync + Send + Default, @@ -302,11 +305,11 @@ where fn info(&self) -> Info { Info { name: "Session", - kind: fairing::Kind::Attach | fairing::Kind::Response, + kind: fairing::Kind::Ignite | fairing::Kind::Response, } } - fn on_attach(&self, rocket: Rocket) -> Result { + async fn on_ignite(&self, rocket: Rocket) -> Result, Rocket> { // install the store singleton Ok(rocket.manage(SessionStore:: { inner: Default::default(), @@ -314,7 +317,7 @@ where })) } - fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { + async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response) { // send the session cookie, if session started let session = request.local_cache(|| SessionID("".to_string()));