pub struct Data<T: ?Sized>(_);
Expand description
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
contains an Arc
.
If route data is not set for a handler, using Data<T>
extractor would cause a 500 Internal Server Error
response.
Unsized Data
For types that are unsized, most commonly dyn T
, Data
can wrap these types by first
constructing an Arc<dyn T>
and using the From
implementation to convert it.
let displayable_arc: Arc<dyn Display> = Arc::new(42usize);
let displayable_data: Data<dyn Display> = Data::from(displayable_arc);
Examples
use std::sync::Mutex;
use actix_web::{App, HttpRequest, HttpResponse, Responder, web::{self, Data}};
struct MyData {
counter: usize,
}
/// Use the `Data<T>` extractor to access data in a handler.
async fn index(data: Data<Mutex<MyData>>) -> impl Responder {
let mut my_data = data.lock().unwrap();
my_data.counter += 1;
HttpResponse::Ok()
}
/// Alteratively, use the `HttpRequest::app_data` method to access data in a handler.
async fn index_alt(req: HttpRequest) -> impl Responder {
let data = req.app_data::<Data<Mutex<MyData>>>().unwrap();
let mut my_data = data.lock().unwrap();
my_data.counter += 1;
HttpResponse::Ok()
}
let data = Data::new(Mutex::new(MyData { counter: 0 }));
let app = App::new()
// Store `MyData` in application storage.
.app_data(Data::clone(&data))
.route("/index.html", web::get().to(index))
.route("/index-alt.html", web::get().to(index_alt));
Implementations
Trait Implementations
sourceimpl<T: ?Sized + 'static> FromRequest for Data<T>
impl<T: ?Sized + 'static> FromRequest for Data<T>
sourcefn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a Self from request parts asynchronously.
sourcefn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Create a Self from request head asynchronously. Read more
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more