mirror of https://github.com/fafhrd91/actix-web
Improve docs for Data extractor
This commit is contained in:
parent
98243db9f1
commit
cb60d026c7
19
src/data.rs
19
src/data.rs
|
@ -20,12 +20,11 @@ pub(crate) type FnDataFactory =
|
||||||
|
|
||||||
/// Application data.
|
/// Application data.
|
||||||
///
|
///
|
||||||
/// Application data is an arbitrary data attached to the app.
|
/// Application data is a piece of arbitrary data attached to the app.
|
||||||
/// Application data is available to all routes and could be added
|
/// Application data is available to all routes and can be added
|
||||||
/// during application configuration process
|
/// during the application configuration process via `App::data()`.
|
||||||
/// with `App::data()` method.
|
|
||||||
///
|
///
|
||||||
/// Application data could be accessed by using `Data<T>`
|
/// Application data can be accessed by using `Data<T>`
|
||||||
/// extractor where `T` is data type.
|
/// extractor where `T` is data type.
|
||||||
///
|
///
|
||||||
/// **Note**: http server accepts an application factory rather than
|
/// **Note**: http server accepts an application factory rather than
|
||||||
|
@ -33,9 +32,7 @@ pub(crate) type FnDataFactory =
|
||||||
/// instance for each thread, thus application data must be constructed
|
/// instance for each thread, thus application data must be constructed
|
||||||
/// multiple times. If you want to share data between different
|
/// multiple times. If you want to share data between different
|
||||||
/// threads, a shareable object should be used, e.g. `Send + Sync`. Application
|
/// threads, a shareable object should be used, e.g. `Send + Sync`. Application
|
||||||
/// data does not need to be `Send` or `Sync`. Internally `Data` type
|
/// data does not need to be `Send` or `Sync`. Internally `Data` uses `Arc`.
|
||||||
/// uses `Arc`. if your data implements `Send` + `Sync` traits you can
|
|
||||||
/// use `web::Data::new()` and avoid double `Arc`.
|
|
||||||
///
|
///
|
||||||
/// If route data is not set for a handler, using `Data<T>` extractor would
|
/// If route data is not set for a handler, using `Data<T>` extractor would
|
||||||
/// cause *Internal Server Error* response.
|
/// cause *Internal Server Error* response.
|
||||||
|
@ -48,7 +45,7 @@ pub(crate) type FnDataFactory =
|
||||||
/// counter: usize,
|
/// counter: usize,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// Use `Data<T>` extractor to access data in 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: web::Data<Mutex<MyData>>) -> impl Responder {
|
||||||
/// let mut data = data.lock().unwrap();
|
/// let mut data = data.lock().unwrap();
|
||||||
/// data.counter += 1;
|
/// data.counter += 1;
|
||||||
|
@ -71,10 +68,6 @@ pub struct Data<T: ?Sized>(Arc<T>);
|
||||||
|
|
||||||
impl<T> Data<T> {
|
impl<T> Data<T> {
|
||||||
/// Create new `Data` instance.
|
/// Create new `Data` instance.
|
||||||
///
|
|
||||||
/// Internally `Data` type uses `Arc`. if your data implements
|
|
||||||
/// `Send` + `Sync` traits you can use `web::Data::new()` and
|
|
||||||
/// avoid double `Arc`.
|
|
||||||
pub fn new(state: T) -> Data<T> {
|
pub fn new(state: T) -> Data<T> {
|
||||||
Data(Arc::new(state))
|
Data(Arc::new(state))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue