Struct ntex::web::types::Data[][src]

pub struct Data<T>(_);
Expand description

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::types::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 ntex::web::{self, App, HttpResponse};

struct MyData {
    counter: usize,
}

/// Use `Data<T>` extractor to access data in handler.
async fn index(data: web::types::Data<Mutex<MyData>>) -> HttpResponse {
    let mut data = data.lock().unwrap();
    data.counter += 1;
    HttpResponse::Ok().into()
}

fn main() {
    let data = web::types::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::types::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]

fn clone(&self) -> Data<T>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for Data<T>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T> Deref for Data<T>[src]

type Target = Arc<T>

The resulting type after dereferencing.

fn deref(&self) -> &Arc<T>[src]

Dereferences the value.

impl<T: 'static, E: ErrorRenderer> FromRequest<E> for Data<T>[src]

type Error = DataExtractorError

The associated error which can be returned.

type Future = Ready<Self, Self::Error>

Future that resolves to a Self

fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future[src]

Convert request to a Self

fn extract(req: &HttpRequest) -> Self::Future[src]

Convert request to a Self Read more

Auto Trait Implementations

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

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

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

impl<T> Unpin for Data<T>

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

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

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

pub fn into(self) -> U[src]

Performs the conversion.

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.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

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.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.