Update 'state' to 0.6.

This commit is contained in:
Sergio Benitez 2023-05-25 11:54:27 -07:00
parent afb5374157
commit e3f1b53efa
14 changed files with 47 additions and 47 deletions

View File

@ -43,7 +43,7 @@ pin-project-lite = "0.2"
memchr = "2" memchr = "2"
stable-pattern = "0.1" stable-pattern = "0.1"
cookie = { version = "0.17.0", features = ["percent-encode"] } cookie = { version = "0.17.0", features = ["percent-encode"] }
state = "0.5.3" state = "0.6"
futures = { version = "0.3", default-features = false } futures = { version = "0.3", default-features = false }
[dependencies.x509-parser] [dependencies.x509-parser]

View File

@ -1,7 +1,7 @@
//! Extension traits implemented by several HTTP types. //! Extension traits implemented by several HTTP types.
use smallvec::{Array, SmallVec}; 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 // TODO: It would be nice if we could somehow have one trait that could give us
// either SmallVec or Vec. // either SmallVec or Vec.
@ -106,10 +106,10 @@ impl<T: IntoOwned> IntoOwned for Vec<T> {
} }
} }
impl<T: IntoOwned + Send + Sync> IntoOwned for Storage<T> impl<T: IntoOwned + Send + Sync> IntoOwned for InitCell<T>
where T::Owned: Send + Sync where T::Owned: Send + Sync
{ {
type Owned = Storage<T::Owned>; type Owned = InitCell<T::Owned>;
#[inline(always)] #[inline(always)]
fn into_owned(self) -> Self::Owned { fn into_owned(self) -> Self::Owned {

View File

@ -51,7 +51,7 @@ use smallvec::SmallVec;
/// [`exact_eq()`](MediaType::exact_eq()) method can be used. /// [`exact_eq()`](MediaType::exact_eq()) method can be used.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MediaType { pub struct MediaType {
/// Storage for the entire media type string. /// InitCell for the entire media type string.
pub(crate) source: Source, pub(crate) source: Source,
/// The top-level type. /// The top-level type.
pub(crate) top: IndexedStr<'static>, pub(crate) top: IndexedStr<'static>,

View File

@ -12,7 +12,7 @@ use tokio::time::Sleep;
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpStream; use tokio::net::TcpStream;
use hyper::server::accept::Accept; use hyper::server::accept::Accept;
use state::Storage; use state::InitCell;
pub use tokio::net::TcpListener; pub use tokio::net::TcpListener;
@ -29,7 +29,7 @@ pub struct CertificateData(pub Vec<u8>);
/// A collection of raw certificate data. /// A collection of raw certificate data.
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Certificates(Arc<Storage<Vec<CertificateData>>>); pub struct Certificates(Arc<InitCell<Vec<CertificateData>>>);
impl From<Vec<CertificateData>> for Certificates { impl From<Vec<CertificateData>> for Certificates {
fn from(value: Vec<CertificateData>) -> Self { fn from(value: Vec<CertificateData>) -> Self {

View File

@ -40,7 +40,7 @@ pub struct TlsListener {
/// ///
/// To work around this, we "lie" when `peer_certificates()` are requested and /// To work around this, we "lie" when `peer_certificates()` are requested and
/// always return `Some(Certificates)`. Internally, `Certificates` is an /// always return `Some(Certificates)`. Internally, `Certificates` is an
/// `Arc<Storage<Vec<CertificateData>>>`, effectively a shared, thread-safe, /// `Arc<InitCell<Vec<CertificateData>>>`, effectively a shared, thread-safe,
/// `OnceCell`. The cell is initially empty and is filled as soon as the /// `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 /// 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 /// this point, it would be empty. However, in Rocket, we only request

View File

@ -441,12 +441,12 @@ impl<'a> Absolute<'a> {
authority, authority,
path: Data { path: Data {
value: IndexedStr::Concrete(Cow::Borrowed(path)), value: IndexedStr::Concrete(Cow::Borrowed(path)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}, },
query: match query { query: match query {
Some(query) => Some(Data { Some(query) => Some(Data {
value: IndexedStr::Concrete(Cow::Borrowed(query)), value: IndexedStr::Concrete(Cow::Borrowed(query)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}), }),
None => None, None => None,
}, },

View File

@ -168,12 +168,12 @@ impl<'a> Origin<'a> {
source: None, source: None,
path: Data { path: Data {
value: IndexedStr::Concrete(Cow::Borrowed(path)), value: IndexedStr::Concrete(Cow::Borrowed(path)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}, },
query: match query { query: match query {
Some(query) => Some(Data { Some(query) => Some(Data {
value: IndexedStr::Concrete(Cow::Borrowed(query)), value: IndexedStr::Concrete(Cow::Borrowed(query)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}), }),
None => None, None => None,
}, },
@ -534,7 +534,7 @@ impl<'a> Origin<'a> {
self.path = Data { self.path = Data {
value: indexed, value: indexed,
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}; };
} else { } else {
self._normalize(false); self._normalize(false);

View File

@ -1,7 +1,7 @@
use std::hash::Hash; use std::hash::Hash;
use std::borrow::Cow; use std::borrow::Cow;
use state::Storage; use state::InitCell;
use crate::{RawStr, ext::IntoOwned}; use crate::{RawStr, ext::IntoOwned};
use crate::uri::Segments; use crate::uri::Segments;
@ -13,12 +13,12 @@ use crate::parse::{IndexedStr, Extent};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Data<'a, P: Part> { pub struct Data<'a, P: Part> {
pub(crate) value: IndexedStr<'a>, pub(crate) value: IndexedStr<'a>,
pub(crate) decoded_segments: Storage<Vec<P::Raw>>, pub(crate) decoded_segments: InitCell<Vec<P::Raw>>,
} }
impl<'a, P: Part> Data<'a, P> { impl<'a, P: Part> Data<'a, P> {
pub(crate) fn raw(value: Extent<&'a [u8]>) -> Self { 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. // INTERNAL METHOD.
@ -26,7 +26,7 @@ impl<'a, P: Part> Data<'a, P> {
pub fn new<S: Into<Cow<'a, str>>>(value: S) -> Self { pub fn new<S: Into<Cow<'a, str>>>(value: S) -> Self {
Data { Data {
value: IndexedStr::from(value.into()), value: IndexedStr::from(value.into()),
decoded_segments: Storage::new(), decoded_segments: InitCell::new(),
} }
} }
} }
@ -129,7 +129,7 @@ impl<'a> Path<'a> {
Data { Data {
value: IndexedStr::from(Cow::Owned(path)), 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> { pub fn segments(&self) -> Segments<'a, fmt::Path> {
let raw = self.raw(); 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 segments = vec![];
let mut raw_segments = self.raw_segments().peekable(); let mut raw_segments = self.raw_segments().peekable();
while let Some(s) = raw_segments.next() { while let Some(s) = raw_segments.next() {
@ -278,7 +278,7 @@ impl<'a> Query<'a> {
Data { Data {
value: IndexedStr::from(Cow::Owned(query)), 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", "")]); /// assert_eq!(query_segs, &[("a b/", "some one@gmail.com"), ("&=2", "")]);
/// ``` /// ```
pub fn segments(&self) -> Segments<'a, fmt::Query> { 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()); let (indexed, query) = (&self.data.value, self.raw());
self.raw_segments() self.raw_segments()
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())

View File

@ -129,12 +129,12 @@ impl<'a> Reference<'a> {
authority, authority,
path: Data { path: Data {
value: IndexedStr::Concrete(Cow::Borrowed(path)), value: IndexedStr::Concrete(Cow::Borrowed(path)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}, },
query: match query { query: match query {
Some(query) => Some(Data { Some(query) => Some(Data {
value: IndexedStr::Concrete(Cow::Borrowed(query)), value: IndexedStr::Concrete(Cow::Borrowed(query)),
decoded_segments: state::Storage::new(), decoded_segments: state::InitCell::new(),
}), }),
None => None, None => None,
}, },

View File

@ -59,7 +59,7 @@ async-trait = "0.1.43"
async-stream = "0.3.2" async-stream = "0.3.2"
multer = { version = "2", features = ["tokio-io"] } multer = { version = "2", features = ["tokio-io"] }
tokio-stream = { version = "0.1.6", features = ["signal", "time"] } tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
state = "0.5.1" state = "0.6"
[dependencies.rocket_codegen] [dependencies.rocket_codegen]
version = "=0.5.0-rc.3" version = "=0.5.0-rc.3"

View File

@ -314,12 +314,12 @@ impl AdHoc {
pub fn uri_normalizer() -> impl Fairing { pub fn uri_normalizer() -> impl Fairing {
#[derive(Default)] #[derive(Default)]
struct Normalizer { struct Normalizer {
routes: state::Storage<Vec<crate::Route>>, routes: state::InitCell<Vec<crate::Route>>,
} }
impl Normalizer { impl Normalizer {
fn routes(&self, rocket: &Rocket<Orbit>) -> &[crate::Route] { fn routes(&self, rocket: &Rocket<Orbit>) -> &[crate::Route] {
self.routes.get_or_set(|| { self.routes.get_or_init(|| {
rocket.routes() rocket.routes()
.filter(|r| r.uri.has_trailing_slash()) .filter(|r| r.uri.has_trailing_slash())
.cloned() .cloned()

View File

@ -1,4 +1,4 @@
use state::Container; use state::TypeMap;
use figment::Figment; use figment::Figment;
use crate::{Catcher, Config, Rocket, Route, Shutdown}; use crate::{Catcher, Config, Rocket, Route, Shutdown};
@ -83,7 +83,7 @@ phases! {
pub(crate) catchers: Vec<Catcher>, pub(crate) catchers: Vec<Catcher>,
pub(crate) fairings: Fairings, pub(crate) fairings: Fairings,
pub(crate) figment: Figment, 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 /// The second launch [`Phase`]: post-build but pre-orbit. See
@ -97,7 +97,7 @@ phases! {
pub(crate) fairings: Fairings, pub(crate) fairings: Fairings,
pub(crate) figment: Figment, pub(crate) figment: Figment,
pub(crate) config: Config, pub(crate) config: Config,
pub(crate) state: Container![Send + Sync], pub(crate) state: TypeMap![Send + Sync],
pub(crate) shutdown: Shutdown, pub(crate) shutdown: Shutdown,
} }
@ -111,7 +111,7 @@ phases! {
pub(crate) fairings: Fairings, pub(crate) fairings: Fairings,
pub(crate) figment: Figment, pub(crate) figment: Figment,
pub(crate) config: Config, pub(crate) config: Config,
pub(crate) state: Container![Send + Sync], pub(crate) state: TypeMap![Send + Sync],
pub(crate) shutdown: Shutdown, pub(crate) shutdown: Shutdown,
} }
} }

View File

@ -4,7 +4,7 @@ use std::{future::Future, borrow::Cow, sync::Arc};
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use yansi::Paint; use yansi::Paint;
use state::{Container, Storage}; use state::{TypeMap, InitCell};
use futures::future::BoxFuture; use futures::future::BoxFuture;
use atomic::{Atomic, Ordering}; use atomic::{Atomic, Ordering};
@ -46,9 +46,9 @@ pub(crate) struct RequestState<'r> {
pub rocket: &'r Rocket<Orbit>, pub rocket: &'r Rocket<Orbit>,
pub route: Atomic<Option<&'r Route>>, pub route: Atomic<Option<&'r Route>>,
pub cookies: CookieJar<'r>, pub cookies: CookieJar<'r>,
pub accept: Storage<Option<Accept>>, pub accept: InitCell<Option<Accept>>,
pub content_type: Storage<Option<ContentType>>, pub content_type: InitCell<Option<ContentType>>,
pub cache: Arc<Container![Send + Sync]>, pub cache: Arc<TypeMap![Send + Sync]>,
pub host: Option<Host<'r>>, pub host: Option<Host<'r>>,
} }
@ -98,9 +98,9 @@ impl<'r> Request<'r> {
rocket, rocket,
route: Atomic::new(None), route: Atomic::new(None),
cookies: CookieJar::new(rocket.config()), cookies: CookieJar::new(rocket.config()),
accept: Storage::new(), accept: InitCell::new(),
content_type: Storage::new(), content_type: InitCell::new(),
cache: Arc::new(<Container![Send + Sync]>::new()), cache: Arc::new(<TypeMap![Send + Sync]>::new()),
host: None, host: None,
} }
} }
@ -547,7 +547,7 @@ impl<'r> Request<'r> {
/// ``` /// ```
#[inline] #[inline]
pub fn content_type(&self) -> Option<&ContentType> { 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()) self.headers().get_one("Content-Type").and_then(|v| v.parse().ok())
}).as_ref() }).as_ref()
} }
@ -567,7 +567,7 @@ impl<'r> Request<'r> {
/// ``` /// ```
#[inline] #[inline]
pub fn accept(&self) -> Option<&Accept> { 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()) self.headers().get_one("Accept").and_then(|v| v.parse().ok())
}).as_ref() }).as_ref()
} }
@ -928,11 +928,11 @@ impl<'r> Request<'r> {
fn bust_header_cache(&mut self, name: &UncasedStr, replace: bool) { fn bust_header_cache(&mut self, name: &UncasedStr, replace: bool) {
if name == "Content-Type" { if name == "Content-Type" {
if self.content_type().is_none() || replace { if self.content_type().is_none() || replace {
self.state.content_type = Storage::new(); self.state.content_type = InitCell::new();
} }
} else if name == "Accept" { } else if name == "Accept" {
if self.accept().is_none() || replace { if self.accept().is_none() || replace {
self.state.accept = Storage::new(); self.state.accept = InitCell::new();
} }
} }
} }

View File

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use state::Storage; use state::InitCell;
use yansi::Paint; use yansi::Paint;
use crate::{Rocket, Request, Response, Orbit, Config}; 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. /// Whether to enforce HSTS even though the user didn't enable it.
force_hsts: AtomicBool, force_hsts: AtomicBool,
/// Headers pre-rendered at liftoff from the configured policies. /// Headers pre-rendered at liftoff from the configured policies.
rendered: Storage<Vec<Header<'static>>>, rendered: InitCell<Vec<Header<'static>>>,
} }
impl Default for Shield { impl Default for Shield {
@ -111,7 +111,7 @@ impl Shield {
Shield { Shield {
policies: HashMap::new(), policies: HashMap::new(),
force_hsts: AtomicBool::new(false), force_hsts: AtomicBool::new(false),
rendered: Storage::new(), rendered: InitCell::new(),
} }
} }
@ -129,7 +129,7 @@ impl Shield {
/// let shield = Shield::new().enable(NoSniff::default()); /// let shield = Shield::new().enable(NoSniff::default());
/// ``` /// ```
pub fn enable<P: Policy>(mut self, policy: P) -> Self { pub fn enable<P: Policy>(mut self, policy: P) -> Self {
self.rendered = Storage::new(); self.rendered = InitCell::new();
self.policies.insert(P::NAME.into(), Box::new(policy)); self.policies.insert(P::NAME.into(), Box::new(policy));
self self
} }
@ -145,7 +145,7 @@ impl Shield {
/// let shield = Shield::default().disable::<NoSniff>(); /// let shield = Shield::default().disable::<NoSniff>();
/// ``` /// ```
pub fn disable<P: Policy>(mut self) -> Self { pub fn disable<P: Policy>(mut self) -> Self {
self.rendered = Storage::new(); self.rendered = InitCell::new();
self.policies.remove(UncasedStr::new(P::NAME)); self.policies.remove(UncasedStr::new(P::NAME));
self self
} }
@ -174,7 +174,7 @@ impl Shield {
} }
fn headers(&self) -> &[Header<'static>] { fn headers(&self) -> &[Header<'static>] {
self.rendered.get_or_set(|| { self.rendered.get_or_init(|| {
let mut headers: Vec<_> = self.policies.values() let mut headers: Vec<_> = self.policies.values()
.map(|p| p.header()) .map(|p| p.header())
.collect(); .collect();