pub struct Data<T: ?Sized>(_);Expand description
Application data wrapper and extractor.
Setting Data
Data is set using the app_data methods on App, Scope, and Resource. If data is wrapped
in this Data type for those calls, it can be used as an extractor.
Note that Data should be constructed outside the HttpServer::new closure if shared,
potentially mutable state is desired. Data is cheap to clone; internally, it uses an Arc.
See also App::app_data, Scope::app_data,
and Resource::app_data.
Extracting Data
Since the Actix Web router layers application data, the returned object will reference the
“closest” instance of the type. For example, if an App stores a u32, a nested Scope
also stores a u32, and the delegated request handler falls within that Scope, then
extracting a web::<Data<u32>> for that handler will return the Scope’s instance.
However, using the same router set up and a request that does not get captured by the Scope,
web::<Data<u32>> would return the App’s instance.
If route data is not set for a handler, using Data<T> extractor would cause a 500 Internal Server Error response.
See also HttpRequest::app_data
and ServiceRequest::app_data.
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 · sourcepub fn borrow_mut(&mut self) -> &mut T
pub 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> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
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
