From e3f1b53efa518b33126575d41d48a8d2188b0a73 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 25 May 2023 11:54:27 -0700 Subject: [PATCH] Update 'state' to 0.6. --- core/http/Cargo.toml | 2 +- core/http/src/ext.rs | 6 +++--- core/http/src/header/media_type.rs | 2 +- core/http/src/listener.rs | 4 ++-- core/http/src/tls/listener.rs | 2 +- core/http/src/uri/absolute.rs | 4 ++-- core/http/src/uri/origin.rs | 6 +++--- core/http/src/uri/path_query.rs | 16 ++++++++-------- core/http/src/uri/reference.rs | 4 ++-- core/lib/Cargo.toml | 2 +- core/lib/src/fairing/ad_hoc.rs | 4 ++-- core/lib/src/phase.rs | 8 ++++---- core/lib/src/request/request.rs | 22 +++++++++++----------- core/lib/src/shield/shield.rs | 12 ++++++------ 14 files changed, 47 insertions(+), 47 deletions(-) diff --git a/core/http/Cargo.toml b/core/http/Cargo.toml index 3e954245..2aa700f0 100644 --- a/core/http/Cargo.toml +++ b/core/http/Cargo.toml @@ -43,7 +43,7 @@ pin-project-lite = "0.2" memchr = "2" stable-pattern = "0.1" cookie = { version = "0.17.0", features = ["percent-encode"] } -state = "0.5.3" +state = "0.6" futures = { version = "0.3", default-features = false } [dependencies.x509-parser] diff --git a/core/http/src/ext.rs b/core/http/src/ext.rs index b1b40eaa..e78012e5 100644 --- a/core/http/src/ext.rs +++ b/core/http/src/ext.rs @@ -1,7 +1,7 @@ //! Extension traits implemented by several HTTP types. use smallvec::{Array, SmallVec}; -use state::Storage; +use state::InitCell; // TODO: It would be nice if we could somehow have one trait that could give us // either SmallVec or Vec. @@ -106,10 +106,10 @@ impl IntoOwned for Vec { } } -impl IntoOwned for Storage +impl IntoOwned for InitCell where T::Owned: Send + Sync { - type Owned = Storage; + type Owned = InitCell; #[inline(always)] fn into_owned(self) -> Self::Owned { diff --git a/core/http/src/header/media_type.rs b/core/http/src/header/media_type.rs index b217dbd5..b764b7da 100644 --- a/core/http/src/header/media_type.rs +++ b/core/http/src/header/media_type.rs @@ -51,7 +51,7 @@ use smallvec::SmallVec; /// [`exact_eq()`](MediaType::exact_eq()) method can be used. #[derive(Debug, Clone)] pub struct MediaType { - /// Storage for the entire media type string. + /// InitCell for the entire media type string. pub(crate) source: Source, /// The top-level type. pub(crate) top: IndexedStr<'static>, diff --git a/core/http/src/listener.rs b/core/http/src/listener.rs index e958702e..f898a108 100644 --- a/core/http/src/listener.rs +++ b/core/http/src/listener.rs @@ -12,7 +12,7 @@ use tokio::time::Sleep; use tokio::io::{AsyncRead, AsyncWrite}; use tokio::net::TcpStream; use hyper::server::accept::Accept; -use state::Storage; +use state::InitCell; pub use tokio::net::TcpListener; @@ -29,7 +29,7 @@ pub struct CertificateData(pub Vec); /// A collection of raw certificate data. #[derive(Clone, Default)] -pub struct Certificates(Arc>>); +pub struct Certificates(Arc>>); impl From> for Certificates { fn from(value: Vec) -> Self { diff --git a/core/http/src/tls/listener.rs b/core/http/src/tls/listener.rs index f8263c6f..8c675d61 100644 --- a/core/http/src/tls/listener.rs +++ b/core/http/src/tls/listener.rs @@ -40,7 +40,7 @@ pub struct TlsListener { /// /// To work around this, we "lie" when `peer_certificates()` are requested and /// always return `Some(Certificates)`. Internally, `Certificates` is an -/// `Arc>>`, effectively a shared, thread-safe, +/// `Arc>>`, effectively a shared, thread-safe, /// `OnceCell`. The cell is initially empty and is filled as soon as the /// handshake is complete. If the certificate data were to be requested prior to /// this point, it would be empty. However, in Rocket, we only request diff --git a/core/http/src/uri/absolute.rs b/core/http/src/uri/absolute.rs index e084e63c..385f754a 100644 --- a/core/http/src/uri/absolute.rs +++ b/core/http/src/uri/absolute.rs @@ -441,12 +441,12 @@ impl<'a> Absolute<'a> { authority, path: Data { value: IndexedStr::Concrete(Cow::Borrowed(path)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }, query: match query { Some(query) => Some(Data { value: IndexedStr::Concrete(Cow::Borrowed(query)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }), None => None, }, diff --git a/core/http/src/uri/origin.rs b/core/http/src/uri/origin.rs index ebe5e444..42aa917d 100644 --- a/core/http/src/uri/origin.rs +++ b/core/http/src/uri/origin.rs @@ -168,12 +168,12 @@ impl<'a> Origin<'a> { source: None, path: Data { value: IndexedStr::Concrete(Cow::Borrowed(path)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }, query: match query { Some(query) => Some(Data { value: IndexedStr::Concrete(Cow::Borrowed(query)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }), None => None, }, @@ -534,7 +534,7 @@ impl<'a> Origin<'a> { self.path = Data { value: indexed, - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }; } else { self._normalize(false); diff --git a/core/http/src/uri/path_query.rs b/core/http/src/uri/path_query.rs index 16733bc7..5cc65a65 100644 --- a/core/http/src/uri/path_query.rs +++ b/core/http/src/uri/path_query.rs @@ -1,7 +1,7 @@ use std::hash::Hash; use std::borrow::Cow; -use state::Storage; +use state::InitCell; use crate::{RawStr, ext::IntoOwned}; use crate::uri::Segments; @@ -13,12 +13,12 @@ use crate::parse::{IndexedStr, Extent}; #[derive(Debug, Clone)] pub struct Data<'a, P: Part> { pub(crate) value: IndexedStr<'a>, - pub(crate) decoded_segments: Storage>, + pub(crate) decoded_segments: InitCell>, } impl<'a, P: Part> Data<'a, P> { pub(crate) fn raw(value: Extent<&'a [u8]>) -> Self { - Data { value: value.into(), decoded_segments: Storage::new() } + Data { value: value.into(), decoded_segments: InitCell::new() } } // INTERNAL METHOD. @@ -26,7 +26,7 @@ impl<'a, P: Part> Data<'a, P> { pub fn new>>(value: S) -> Self { Data { value: IndexedStr::from(value.into()), - decoded_segments: Storage::new(), + decoded_segments: InitCell::new(), } } } @@ -129,7 +129,7 @@ impl<'a> Path<'a> { Data { value: IndexedStr::from(Cow::Owned(path)), - decoded_segments: Storage::new(), + decoded_segments: InitCell::new(), } } @@ -215,7 +215,7 @@ impl<'a> Path<'a> { /// ``` pub fn segments(&self) -> Segments<'a, fmt::Path> { let raw = self.raw(); - let cached = self.data.decoded_segments.get_or_set(|| { + let cached = self.data.decoded_segments.get_or_init(|| { let mut segments = vec![]; let mut raw_segments = self.raw_segments().peekable(); while let Some(s) = raw_segments.next() { @@ -278,7 +278,7 @@ impl<'a> Query<'a> { Data { value: IndexedStr::from(Cow::Owned(query)), - decoded_segments: Storage::new(), + decoded_segments: InitCell::new(), } } @@ -351,7 +351,7 @@ impl<'a> Query<'a> { /// assert_eq!(query_segs, &[("a b/", "some one@gmail.com"), ("&=2", "")]); /// ``` pub fn segments(&self) -> Segments<'a, fmt::Query> { - let cached = self.data.decoded_segments.get_or_set(|| { + let cached = self.data.decoded_segments.get_or_init(|| { let (indexed, query) = (&self.data.value, self.raw()); self.raw_segments() .filter(|s| !s.is_empty()) diff --git a/core/http/src/uri/reference.rs b/core/http/src/uri/reference.rs index c3b2fa2c..a6269506 100644 --- a/core/http/src/uri/reference.rs +++ b/core/http/src/uri/reference.rs @@ -129,12 +129,12 @@ impl<'a> Reference<'a> { authority, path: Data { value: IndexedStr::Concrete(Cow::Borrowed(path)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }, query: match query { Some(query) => Some(Data { value: IndexedStr::Concrete(Cow::Borrowed(query)), - decoded_segments: state::Storage::new(), + decoded_segments: state::InitCell::new(), }), None => None, }, diff --git a/core/lib/Cargo.toml b/core/lib/Cargo.toml index e007f102..44de9448 100644 --- a/core/lib/Cargo.toml +++ b/core/lib/Cargo.toml @@ -59,7 +59,7 @@ async-trait = "0.1.43" async-stream = "0.3.2" multer = { version = "2", features = ["tokio-io"] } tokio-stream = { version = "0.1.6", features = ["signal", "time"] } -state = "0.5.1" +state = "0.6" [dependencies.rocket_codegen] version = "=0.5.0-rc.3" diff --git a/core/lib/src/fairing/ad_hoc.rs b/core/lib/src/fairing/ad_hoc.rs index 232e2f2a..b428d307 100644 --- a/core/lib/src/fairing/ad_hoc.rs +++ b/core/lib/src/fairing/ad_hoc.rs @@ -314,12 +314,12 @@ impl AdHoc { pub fn uri_normalizer() -> impl Fairing { #[derive(Default)] struct Normalizer { - routes: state::Storage>, + routes: state::InitCell>, } impl Normalizer { fn routes(&self, rocket: &Rocket) -> &[crate::Route] { - self.routes.get_or_set(|| { + self.routes.get_or_init(|| { rocket.routes() .filter(|r| r.uri.has_trailing_slash()) .cloned() diff --git a/core/lib/src/phase.rs b/core/lib/src/phase.rs index 213f1215..3b6ca870 100644 --- a/core/lib/src/phase.rs +++ b/core/lib/src/phase.rs @@ -1,4 +1,4 @@ -use state::Container; +use state::TypeMap; use figment::Figment; use crate::{Catcher, Config, Rocket, Route, Shutdown}; @@ -83,7 +83,7 @@ phases! { pub(crate) catchers: Vec, pub(crate) fairings: Fairings, pub(crate) figment: Figment, - pub(crate) state: Container![Send + Sync], + pub(crate) state: TypeMap![Send + Sync], } /// The second launch [`Phase`]: post-build but pre-orbit. See @@ -97,7 +97,7 @@ phases! { pub(crate) fairings: Fairings, pub(crate) figment: Figment, pub(crate) config: Config, - pub(crate) state: Container![Send + Sync], + pub(crate) state: TypeMap![Send + Sync], pub(crate) shutdown: Shutdown, } @@ -111,7 +111,7 @@ phases! { pub(crate) fairings: Fairings, pub(crate) figment: Figment, pub(crate) config: Config, - pub(crate) state: Container![Send + Sync], + pub(crate) state: TypeMap![Send + Sync], pub(crate) shutdown: Shutdown, } } diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index 625c4715..0b44d131 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -4,7 +4,7 @@ use std::{future::Future, borrow::Cow, sync::Arc}; use std::net::{IpAddr, SocketAddr}; use yansi::Paint; -use state::{Container, Storage}; +use state::{TypeMap, InitCell}; use futures::future::BoxFuture; use atomic::{Atomic, Ordering}; @@ -46,9 +46,9 @@ pub(crate) struct RequestState<'r> { pub rocket: &'r Rocket, pub route: Atomic>, pub cookies: CookieJar<'r>, - pub accept: Storage>, - pub content_type: Storage>, - pub cache: Arc, + pub accept: InitCell>, + pub content_type: InitCell>, + pub cache: Arc, pub host: Option>, } @@ -98,9 +98,9 @@ impl<'r> Request<'r> { rocket, route: Atomic::new(None), cookies: CookieJar::new(rocket.config()), - accept: Storage::new(), - content_type: Storage::new(), - cache: Arc::new(::new()), + accept: InitCell::new(), + content_type: InitCell::new(), + cache: Arc::new(::new()), host: None, } } @@ -547,7 +547,7 @@ impl<'r> Request<'r> { /// ``` #[inline] pub fn content_type(&self) -> Option<&ContentType> { - self.state.content_type.get_or_set(|| { + self.state.content_type.get_or_init(|| { self.headers().get_one("Content-Type").and_then(|v| v.parse().ok()) }).as_ref() } @@ -567,7 +567,7 @@ impl<'r> Request<'r> { /// ``` #[inline] pub fn accept(&self) -> Option<&Accept> { - self.state.accept.get_or_set(|| { + self.state.accept.get_or_init(|| { self.headers().get_one("Accept").and_then(|v| v.parse().ok()) }).as_ref() } @@ -928,11 +928,11 @@ impl<'r> Request<'r> { fn bust_header_cache(&mut self, name: &UncasedStr, replace: bool) { if name == "Content-Type" { if self.content_type().is_none() || replace { - self.state.content_type = Storage::new(); + self.state.content_type = InitCell::new(); } } else if name == "Accept" { if self.accept().is_none() || replace { - self.state.accept = Storage::new(); + self.state.accept = InitCell::new(); } } } diff --git a/core/lib/src/shield/shield.rs b/core/lib/src/shield/shield.rs index a614fa54..1e58175f 100644 --- a/core/lib/src/shield/shield.rs +++ b/core/lib/src/shield/shield.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::atomic::{AtomicBool, Ordering}; -use state::Storage; +use state::InitCell; use yansi::Paint; use crate::{Rocket, Request, Response, Orbit, Config}; @@ -72,7 +72,7 @@ pub struct Shield { /// Whether to enforce HSTS even though the user didn't enable it. force_hsts: AtomicBool, /// Headers pre-rendered at liftoff from the configured policies. - rendered: Storage>>, + rendered: InitCell>>, } impl Default for Shield { @@ -111,7 +111,7 @@ impl Shield { Shield { policies: HashMap::new(), force_hsts: AtomicBool::new(false), - rendered: Storage::new(), + rendered: InitCell::new(), } } @@ -129,7 +129,7 @@ impl Shield { /// let shield = Shield::new().enable(NoSniff::default()); /// ``` pub fn enable(mut self, policy: P) -> Self { - self.rendered = Storage::new(); + self.rendered = InitCell::new(); self.policies.insert(P::NAME.into(), Box::new(policy)); self } @@ -145,7 +145,7 @@ impl Shield { /// let shield = Shield::default().disable::(); /// ``` pub fn disable(mut self) -> Self { - self.rendered = Storage::new(); + self.rendered = InitCell::new(); self.policies.remove(UncasedStr::new(P::NAME)); self } @@ -174,7 +174,7 @@ impl Shield { } fn headers(&self) -> &[Header<'static>] { - self.rendered.get_or_set(|| { + self.rendered.get_or_init(|| { let mut headers: Vec<_> = self.policies.values() .map(|p| p.header()) .collect();