[−][src]Struct actix_web::web::Data
Application data.
Application data is an arbitrary data attached to the app.
Application data is available to all routes and could be added
during application configuration process
with App::data()
method.
Application data could be accessed by using Data<T>
extractor where T
is data type.
Note: http server accepts an application factory rather than
an application instance. Http server constructs an application
instance for each thread, thus application data must be constructed
multiple times. If you want to share data between different
threads, a shareable object should be used, e.g. Send + Sync
. Application
data does not need to be Send
or Sync
. Internally Data
type
uses Arc
. if your data implements Send
+ Sync
traits you can
use web::Data::new()
and avoid double Arc
.
If route data is not set for a handler, using Data<T>
extractor would
cause Internal Server Error response.
use std::sync::Mutex; use actix_web::{web, App, HttpResponse, Responder}; struct MyData { counter: usize, } /// Use `Data<T>` extractor to access data in handler. async fn index(data: web::Data<Mutex<MyData>>) -> impl Responder { let mut data = data.lock().unwrap(); data.counter += 1; HttpResponse::Ok() } fn main() { let data = web::Data::new(Mutex::new(MyData{ counter: 0 })); let app = App::new() // Store `MyData` in application storage. .app_data(data.clone()) .service( web::resource("/index.html").route( web::get().to(index))); }
Implementations
impl<T> Data<T>
[src]
pub fn new(state: T) -> Data<T>
[src]
Create new Data
instance.
Internally Data
type uses Arc
. if your data implements
Send
+ Sync
traits you can use web::Data::new()
and
avoid double Arc
.
pub fn get_ref(&self) -> &T
[src]
Get reference to inner app data.
pub fn into_inner(self) -> Arc<T>
[src]
Convert to the internal Arc
Methods from Deref<Target = Arc<T>>
Trait Implementations
impl<T> Clone for Data<T>
[src]
impl<T: Debug> Debug for Data<T>
[src]
impl<T> Deref for Data<T>
[src]
impl<T> From<Arc<T>> for Data<T>
[src]
impl<T: 'static> FromRequest for Data<T>
[src]
type Config = ()
Configuration for this extractor
type Error = Error
The associated error which can be returned.
type Future = Ready<Result<Self, Error>>
Future that resolves to a Self
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
[src]
fn extract(req: &HttpRequest) -> Self::Future
[src]
fn configure<F>(f: F) -> Self::Config where
F: FnOnce(Self::Config) -> Self::Config,
[src]
F: FnOnce(Self::Config) -> Self::Config,
Auto Trait Implementations
impl<T> RefUnwindSafe for Data<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Data<T> where
T: Send + Sync,
T: Send + Sync,
impl<T> Sync for Data<T> where
T: Send + Sync,
T: Send + Sync,
impl<T> Unpin for Data<T>
impl<T> UnwindSafe for Data<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T, A, P> Access<T> for P where
A: Access<T>,
P: Deref<Target = A>,
[src]
A: Access<T>,
P: Deref<Target = A>,
type Guard = <A as Access<T>>::Guard
A guard object containing the value and keeping it alive. Read more
fn load(&self) -> <P as Access<T>>::Guard
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, A> DynAccess<T> for A where
A: Access<T>,
<A as Access<T>>::Guard: 'static,
[src]
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> Sealed<T> for T where
T: ?Sized,
T: ?Sized,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,