[][src]Struct actix_web::web::Data

pub struct Data<T: ?Sized>(_);

Application data.

Application level data is a piece of arbitrary data attached to the app, scope, or resource. Application data is available to all routes and can be added during the application configuration process via App::data().

Application data can 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 uses 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 the `Data<T>` extractor to access data in a 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.

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: ?Sized> Clone for Data<T>[src]

impl<T: Debug + ?Sized> Debug for Data<T>[src]

impl<T: ?Sized> Deref for Data<T>[src]

type Target = Arc<T>

The resulting type after dereferencing.

impl<T: ?Sized> From<Arc<T>> for Data<T>[src]

impl<T: ?Sized + '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

Auto Trait Implementations

impl<T: ?Sized> RefUnwindSafe for Data<T> where
    T: RefUnwindSafe

impl<T: ?Sized> Send for Data<T> where
    T: Send + Sync

impl<T: ?Sized> Sync for Data<T> where
    T: Send + Sync

impl<T: ?Sized> Unpin for Data<T>

impl<T: ?Sized> UnwindSafe for Data<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,