Struct Data
pub struct Data<T>(/* private fields */)
where
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()
}
/// Alternatively, 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§
§impl<T> Apiv2Schema for Data<T>
impl<T> Apiv2Schema for Data<T>
§fn name() -> Option<String>
fn name() -> Option<String>
§fn description() -> &'static str
fn description() -> &'static str
§fn raw_schema() -> DefaultSchemaRaw
fn raw_schema() -> DefaultSchemaRaw
§fn schema_with_ref() -> DefaultSchemaRaw
fn schema_with_ref() -> DefaultSchemaRaw
§fn security_scheme() -> Option<SecurityScheme>
fn security_scheme() -> Option<SecurityScheme>
fn header_parameter_schema() -> Vec<Parameter<DefaultSchemaRaw>>
§impl<'de, T> Deserialize<'de> for Data<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Data<T>where
T: Deserialize<'de>,
§fn deserialize<D>(
deserializer: D,
) -> Result<Data<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Data<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<T> FromRequest for Data<T>where
T: 'static + ?Sized,
impl<T> FromRequest for Data<T>where
T: 'static + ?Sized,
§fn from_request(
req: &HttpRequest,
_: &mut Payload,
) -> <Data<T> as FromRequest>::Future
fn from_request( req: &HttpRequest, _: &mut Payload, ) -> <Data<T> as FromRequest>::Future
Self
from request parts asynchronously.§fn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Self
from request head asynchronously. Read more§impl<T> OperationModifier for Data<T>
impl<T> OperationModifier for Data<T>
§fn update_parameter(
op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_parameter( op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_response(
_op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_response( _op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_definitions(map: &mut BTreeMap<String, DefaultSchemaRaw>)
fn update_definitions(map: &mut BTreeMap<String, DefaultSchemaRaw>)
§fn update_security(
op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_security( op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_security_definitions(map: &mut BTreeMap<String, SecurityScheme>)
fn update_security_definitions(map: &mut BTreeMap<String, SecurityScheme>)
§impl<T> Serialize for Data<T>where
T: Serialize,
impl<T> Serialize for Data<T>where
T: Serialize,
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Auto Trait Implementations§
impl<T> Freeze for Data<T>where
T: ?Sized,
impl<T> RefUnwindSafe for Data<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for Data<T>
impl<T> Sync for Data<T>
impl<T> Unpin for Data<T>where
T: ?Sized,
impl<T> UnwindSafe for Data<T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)