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"
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]

View File

@ -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<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
{
type Owned = Storage<T::Owned>;
type Owned = InitCell<T::Owned>;
#[inline(always)]
fn into_owned(self) -> Self::Owned {

View File

@ -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>,

View File

@ -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<u8>);
/// A collection of raw certificate data.
#[derive(Clone, Default)]
pub struct Certificates(Arc<Storage<Vec<CertificateData>>>);
pub struct Certificates(Arc<InitCell<Vec<CertificateData>>>);
impl From<Vec<CertificateData>> for Certificates {
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
/// 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
/// 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

View File

@ -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,
},

View File

@ -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);

View File

@ -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<Vec<P::Raw>>,
pub(crate) decoded_segments: InitCell<Vec<P::Raw>>,
}
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<S: Into<Cow<'a, str>>>(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())

View File

@ -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,
},

View File

@ -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"

View File

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

View File

@ -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<Catcher>,
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,
}
}

View File

@ -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<Orbit>,
pub route: Atomic<Option<&'r Route>>,
pub cookies: CookieJar<'r>,
pub accept: Storage<Option<Accept>>,
pub content_type: Storage<Option<ContentType>>,
pub cache: Arc<Container![Send + Sync]>,
pub accept: InitCell<Option<Accept>>,
pub content_type: InitCell<Option<ContentType>>,
pub cache: Arc<TypeMap![Send + Sync]>,
pub host: Option<Host<'r>>,
}
@ -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(<Container![Send + Sync]>::new()),
accept: InitCell::new(),
content_type: InitCell::new(),
cache: Arc::new(<TypeMap![Send + Sync]>::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();
}
}
}

View File

@ -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<Vec<Header<'static>>>,
rendered: InitCell<Vec<Header<'static>>>,
}
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<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
}
@ -145,7 +145,7 @@ impl Shield {
/// let shield = Shield::default().disable::<NoSniff>();
/// ```
pub fn disable<P: Policy>(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();