mirror of
https://github.com/actix/actix-web.git
synced 2024-11-26 19:41:12 +00:00
improve Data docs
This commit is contained in:
parent
bed72d9bb7
commit
606a371ec3
1 changed files with 31 additions and 19 deletions
50
src/data.rs
50
src/data.rs
|
@ -31,41 +31,53 @@ pub(crate) type FnDataFactory =
|
||||||
/// server constructs an application instance for each thread, thus application data must be
|
/// 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
|
/// 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`
|
/// object should be used, e.g. `Send + Sync`. Application data does not need to be `Send`
|
||||||
/// or `Sync`. Internally `Data` uses `Arc`.
|
/// or `Sync`. Internally `Data` contains an `Arc`.
|
||||||
///
|
///
|
||||||
/// If route data is not set for a handler, using `Data<T>` extractor would cause *Internal
|
/// If route data is not set for a handler, using `Data<T>` extractor would cause a `500 Internal
|
||||||
/// Server Error* response.
|
/// Server Error` response.
|
||||||
///
|
///
|
||||||
// TODO: document `dyn T` functionality through converting an Arc
|
/// # Unsized Data
|
||||||
// TODO: note equivalence of req.app_data<Data<T>> and Data<T> extractor
|
/// For types that are unsized, most commonly `dyn T`, `Data` can wrap these types by first
|
||||||
// TODO: note that data must be inserted using Data<T> in order to extract it
|
/// constructing an `Arc<dyn T>` and using the `From` implementation to convert it.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use std::{fmt::Display, sync::Arc};
|
||||||
|
/// # use actix_web::web::Data;
|
||||||
|
/// let displayable_arc: Arc<dyn Display> = Arc::new(42usize);
|
||||||
|
/// let displayable_data: Data<dyn Display> = Data::from(displayable_arc);
|
||||||
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::sync::Mutex;
|
/// use std::sync::Mutex;
|
||||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
/// use actix_web::{App, HttpRequest, HttpResponse, Responder, web::{self, Data}};
|
||||||
///
|
///
|
||||||
/// struct MyData {
|
/// struct MyData {
|
||||||
/// counter: usize,
|
/// counter: usize,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// Use the `Data<T>` extractor to access data in a handler.
|
/// /// Use the `Data<T>` extractor to access data in a handler.
|
||||||
/// async fn index(data: web::Data<Mutex<MyData>>) -> impl Responder {
|
/// async fn index(data: Data<Mutex<MyData>>) -> impl Responder {
|
||||||
/// let mut data = data.lock().unwrap();
|
/// let mut my_data = data.lock().unwrap();
|
||||||
/// data.counter += 1;
|
/// my_data.counter += 1;
|
||||||
/// HttpResponse::Ok()
|
/// HttpResponse::Ok()
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// /// Alteratively, use the `HttpRequest::app_data` method to access data in a handler.
|
||||||
/// let data = web::Data::new(Mutex::new(MyData{ counter: 0 }));
|
/// async fn index_alt(req: HttpRequest) -> impl Responder {
|
||||||
///
|
/// let data = req.app_data::<Data<Mutex<MyData>>>().unwrap();
|
||||||
/// let app = App::new()
|
/// let mut my_data = data.lock().unwrap();
|
||||||
/// // Store `MyData` in application storage.
|
/// my_data.counter += 1;
|
||||||
/// .app_data(data.clone())
|
/// HttpResponse::Ok()
|
||||||
/// .service(
|
|
||||||
/// web::resource("/index.html").route(
|
|
||||||
/// web::get().to(index)));
|
|
||||||
/// }
|
/// }
|
||||||
|
///
|
||||||
|
/// 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));
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Data<T: ?Sized>(Arc<T>);
|
pub struct Data<T: ?Sized>(Arc<T>);
|
||||||
|
|
Loading…
Reference in a new issue