Showing 2 of 2 files from the diff.
Newly tracked file
src/data.rs changed.
Newly tracked file
src/app.rs changed.

@@ -18,49 +18,11 @@
Loading
18 18
pub(crate) type FnDataFactory =
19 19
    Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
20 20
21 -
/// Application data.
21 +
/// Wrapper for a piece of data.
22 22
///
23 -
/// Application level data is a piece of arbitrary data attached to the app, scope, or resource.
24 -
/// Application data is available to all routes and can be added during the application
25 -
/// configuration process via `App::data()`.
23 +
/// Internally it wraps the data in an `Arc`.
26 24
///
27 -
/// Application data can be accessed by using `Data<T>` extractor where `T` is data type.
28 -
///
29 -
/// **Note**: http server accepts an application factory rather than an application instance. HTTP
30 -
/// server constructs an application instance for each thread, thus application data must be
31 -
/// constructed multiple times. If you want to share data between different threads, a shareable
32 -
/// object should be used, e.g. `Send + Sync`. Application data does not need to be `Send`
33 -
/// or `Sync`. Internally `Data` uses `Arc`.
34 -
///
35 -
/// If route data is not set for a handler, using `Data<T>` extractor would cause *Internal
36 -
/// Server Error* response.
37 -
///
38 -
/// ```rust
39 -
/// use std::sync::Mutex;
40 -
/// use actix_web::{web, App, HttpResponse, Responder};
41 -
///
42 -
/// struct MyData {
43 -
///     counter: usize,
44 -
/// }
45 -
///
46 -
/// /// Use the `Data<T>` extractor to access data in a handler.
47 -
/// async fn index(data: web::Data<Mutex<MyData>>) -> impl Responder {
48 -
///     let mut data = data.lock().unwrap();
49 -
///     data.counter += 1;
50 -
///     HttpResponse::Ok()
51 -
/// }
52 -
///
53 -
/// fn main() {
54 -
///     let data = web::Data::new(Mutex::new(MyData{ counter: 0 }));
55 -
///
56 -
///     let app = App::new()
57 -
///         // Store `MyData` in application storage.
58 -
///         .app_data(data.clone())
59 -
///         .service(
60 -
///             web::resource("/index.html").route(
61 -
///                 web::get().to(index)));
62 -
/// }
63 -
/// ```
25 +
/// See `App::data()` and `App::app_data()` for when and how to use this.
64 26
#[derive(Debug)]
65 27
pub struct Data<T: ?Sized>(Arc<T>);
66 28

@@ -70,7 +70,8 @@
Loading
70 70
        InitError = (),
71 71
    >,
72 72
{
73 -
    /// Adds application data.
73 +
    /// Adds an arbirtraty application level data item.
74 +
    ///
74 75
    /// Application data can be accessed by using a `Data<T>` extractor where `T` is the data type.
75 76
    ///
76 77
    /// The state is managed on a per-type basis and as such there can be
@@ -82,13 +83,16 @@
Loading
82 83
    /// If your data is already wrapped in an `Arc`
83 84
    /// you can instead store it directly using the `App::app_data()` function.
84 85
    ///
85 -
    /// **Note**: `HttpServer` accepts an application factory rather than
86 +
    /// **Note**: `HttpServer` accepts an application factory (closure) rather than
86 87
    /// an application instance (`App`).
87 -
    /// `HttpServer` constructs an application instance for each thread,
88 -
    /// thus application data must be constructed multiple times.
88 +
    /// `HttpServer` constructs an application instance for each thread
89 +
    /// by calling this factory closure thus application data must be constructed multiple times.
89 90
    /// If you want to share data between different threads,
90 91
    /// a shared object should be used, e.g. `Arc`.
91 92
    ///
93 +
    /// If route data is not set for a handler, using `Data<T>` extractor would cause an *Internal
94 +
    /// Server Error* response.
95 +
    ///
92 96
    /// ```rust
93 97
    /// use std::cell::Cell;
94 98
    /// use actix_web::{web, App, HttpResponse, Responder};
@@ -144,13 +148,60 @@
Loading
144 148
        self
145 149
    }
146 150
147 -
    /// Set application level arbitrary data item.
151 +
    /// Adds an arbirtraty application level data item.
152 +
    ///
153 +
    /// This data is available to all routes and can be added during the application.
154 +
    /// There are two ways to retrieve this data:
155 +
    ///
156 +
    /// 1. At runtime via the `HttpRequest::app_data()` method
157 +
    /// 2. If data of type `T` is stored wrapped in a `Data<T>` object
158 +
    ///    it can also be retrieved using a `Data<T>` extractor
159 +
    ///
160 +
    /// The state is managed on a per-type basis and as such there can be
161 +
    /// at most one value for any given type.
162 +
    /// Later invocations overwrite earlier ones.
163 +
    /// This means that only the last invocation of this function per type will have an effect.
164 +
    ///
165 +
    /// **Note**: `HttpServer` accepts an application factory (closure) rather than
166 +
    /// an application instance (`App`).
167 +
    /// `HttpServer` constructs an application instance for each thread
168 +
    /// by calling this factory closure thus application data must be constructed multiple times.
169 +
    /// If you want to share data between different threads,
170 +
    /// a shared object should be used, e.g. `Arc`.
171 +
    ///
172 +
    /// ```rust
173 +
    /// use std::sync::Mutex;
174 +
    /// use actix_web::{web, App, HttpResponse, HttpRequest, Responder};
175 +
    ///
176 +
    /// struct MyData {
177 +
    ///     counter: usize,
178 +
    /// }
179 +
    ///
180 +
    /// /// Use the `Data<T>` extractor to access data in a handler.
181 +
    /// async fn index(data: web::Data<Mutex<MyData>>) -> impl Responder {
182 +
    ///     let mut data = data.lock().unwrap();
183 +
    ///     data.counter += 1;
184 +
    ///     HttpResponse::Ok().body(format!("{}", data.counter))
185 +
    /// }
148 186
    ///
149 -
    /// Application data stored with `App::app_data()` method is available
150 -
    /// via `HttpRequest::app_data()` method at runtime.
187 +
    /// async fn hello(req: HttpRequest) -> impl Responder {
188 +
    ///    let data = req.app_data::<web::Data<Mutex<MyData>>>();
189 +
    ///    let mut data = data.unwrap().lock().unwrap();
190 +
    ///    data.counter += 1;
191 +
    ///    HttpResponse::Ok().body(format!("{}", data.counter))
192 +
    ///}
151 193
    ///
152 -
    /// This method could be used for storing `Data<T>` as well, in that case
153 -
    /// data could be accessed by using `Data<T>` extractor.
194 +
    /// fn main() {
195 +
    ///     let data = web::Data::new(Mutex::new(MyData{ counter: 0 }));
196 +
    ///
197 +
    ///     let app = App::new()
198 +
    ///         // Store `MyData` in application storage.
199 +
    ///         .app_data(data)
200 +
    ///         .service(
201 +
    ///             web::resource("/index.html").route(
202 +
    ///                 web::get().to(index)));
203 +
    /// }
204 +
    /// ```
154 205
    pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
155 206
        self.extensions.insert(ext);
156 207
        self
Files Coverage
actix-files/src 0.00%
actix-http/src 37.77%
actix-multipart/src 0.00%
actix-web-actors/src 0.00%
awc/src 17.98%
src 87.07%
tests 100.00%
actix-web-codegen/src/route.rs 0.00%
Project Totals (126 files) 53.82%
1
ignore: # ignore codecoverage on following paths
2
  - "**/tests"
3
  - "test-server"
4
  - "**/benches"
5
  - "**/examples"
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading