1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00
actix-web/src/data.rs

121 lines
2.7 KiB
Rust
Raw Normal View History

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