1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-12 02:09:36 +00:00
actix-web/src/state.rs

121 lines
2.8 KiB
Rust
Raw Normal View History

2019-03-02 06:51:32 +00:00
use std::ops::Deref;
2019-03-06 18:03:18 +00:00
use std::sync::Arc;
2019-03-02 06:51:32 +00:00
use actix_http::error::{Error, ErrorInternalServerError};
use actix_http::Extensions;
use futures::{Async, Future, IntoFuture, Poll};
use crate::extract::FromRequest;
use crate::service::ServiceFromRequest;
2019-03-02 06:51:32 +00:00
/// Application state factory
pub(crate) trait StateFactory {
fn construct(&self) -> Box<StateFactoryResult>;
}
pub(crate) trait StateFactoryResult {
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()>;
}
/// Application state
2019-03-06 18:03:18 +00:00
pub struct State<T>(Arc<T>);
2019-03-02 06:51:32 +00:00
2019-03-03 22:45:56 +00:00
impl<T> State<T> {
pub(crate) fn new(state: T) -> State<T> {
2019-03-06 18:03:18 +00:00
State(Arc::new(state))
2019-03-02 06:51:32 +00:00
}
2019-03-03 22:45:56 +00:00
/// Get referecnce to inner state type.
pub fn get_ref(&self) -> &T {
2019-03-02 06:51:32 +00:00
self.0.as_ref()
}
}
2019-03-03 22:45:56 +00:00
impl<T> Deref for State<T> {
type Target = T;
2019-03-02 06:51:32 +00:00
2019-03-03 22:45:56 +00:00
fn deref(&self) -> &T {
2019-03-02 06:51:32 +00:00
self.0.as_ref()
}
}
2019-03-03 22:45:56 +00:00
impl<T> Clone for State<T> {
fn clone(&self) -> State<T> {
2019-03-02 06:51:32 +00:00
State(self.0.clone())
}
}
2019-03-03 22:45:56 +00:00
impl<T: 'static, P> FromRequest<P> for State<T> {
2019-03-02 06:51:32 +00:00
type Error = Error;
2019-03-03 22:45:56 +00:00
type Future = Result<Self, Error>;
2019-03-02 06:51:32 +00:00
#[inline]
fn from_request(req: &mut ServiceFromRequest<P>) -> Self::Future {
if let Some(st) = req.config().extensions().get::<State<T>>() {
2019-03-03 22:45:56 +00:00
Ok(st.clone())
2019-03-02 06:51:32 +00:00
} else {
2019-03-03 22:45:56 +00:00
Err(ErrorInternalServerError(
"State is not configured, to configure use App::state()",
2019-03-02 06:51:32 +00:00
))
}
}
}
2019-03-03 22:45:56 +00:00
impl<T: 'static> StateFactory for State<T> {
2019-03-02 06:51:32 +00:00
fn construct(&self) -> Box<StateFactoryResult> {
Box::new(StateFut { st: self.clone() })
}
}
2019-03-03 22:45:56 +00:00
struct StateFut<T> {
st: State<T>,
2019-03-02 06:51:32 +00:00
}
2019-03-03 22:45:56 +00:00
impl<T: 'static> StateFactoryResult for StateFut<T> {
2019-03-02 06:51:32 +00:00
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()> {
extensions.insert(self.st.clone());
Ok(Async::Ready(()))
}
}
impl<F, Out> StateFactory for F
where
F: Fn() -> Out + 'static,
Out: IntoFuture + 'static,
Out::Error: std::fmt::Debug,
{
fn construct(&self) -> Box<StateFactoryResult> {
Box::new(StateFactoryFut {
fut: (*self)().into_future(),
})
}
}
2019-03-03 22:45:56 +00:00
struct StateFactoryFut<T, F>
2019-03-02 06:51:32 +00:00
where
2019-03-03 22:45:56 +00:00
F: Future<Item = T>,
2019-03-02 06:51:32 +00:00
F::Error: std::fmt::Debug,
{
fut: F,
}
2019-03-03 22:45:56 +00:00
impl<T: 'static, F> StateFactoryResult for StateFactoryFut<T, F>
2019-03-02 06:51:32 +00:00
where
2019-03-03 22:45:56 +00:00
F: Future<Item = T>,
2019-03-02 06:51:32 +00:00
F::Error: std::fmt::Debug,
{
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()> {
match self.fut.poll() {
Ok(Async::Ready(s)) => {
extensions.insert(State::new(s));
Ok(Async::Ready(()))
}
Ok(Async::NotReady) => Ok(Async::NotReady),
Err(e) => {
log::error!("Can not construct application state: {:?}", e);
Err(())
}
}
}
}