1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Make web::Data deref to Arc<T> #1214

This commit is contained in:
Nikolay Kim 2019-12-20 17:45:35 +06:00
parent 8b8a9a995d
commit e5a50f423d
4 changed files with 18 additions and 14 deletions

View File

@ -8,6 +8,8 @@
* Allow to set `peer_addr` for TestRequest #1074 * Allow to set `peer_addr` for TestRequest #1074
* Make web::Data deref to Arc<T> #1214
* Rename `App::register_data()` to `App::app_data()` * Rename `App::register_data()` to `App::app_data()`
* `HttpRequest::app_data<T>()` returns `Option<&T>` instead of `Option<&Data<T>>` * `HttpRequest::app_data<T>()` returns `Option<&T>` instead of `Option<&Data<T>>`

View File

@ -87,10 +87,10 @@ impl<T> Data<T> {
} }
impl<T> Deref for Data<T> { impl<T> Deref for Data<T> {
type Target = T; type Target = Arc<T>;
fn deref(&self) -> &T { fn deref(&self) -> &Arc<T> {
self.0.as_ref() &self.0
} }
} }
@ -144,9 +144,11 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_data_extractor() { async fn test_data_extractor() {
let mut srv = let mut srv = init_service(App::new().data("TEST".to_string()).service(
init_service(App::new().data(10usize).service( web::resource("/").to(|data: web::Data<String>| {
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()), assert_eq!(data.to_lowercase(), "test");
HttpResponse::Ok()
}),
)) ))
.await; .await;
@ -220,7 +222,7 @@ mod tests {
let mut srv = init_service(App::new().data(1usize).service( let mut srv = init_service(App::new().data(1usize).service(
web::resource("/").data(10usize).route(web::get().to( web::resource("/").data(10usize).route(web::get().to(
|data: web::Data<usize>| { |data: web::Data<usize>| {
assert_eq!(*data, 10); assert_eq!(**data, 10);
let _ = data.clone(); let _ = data.clone();
HttpResponse::Ok() HttpResponse::Ok()
}, },

View File

@ -761,9 +761,9 @@ mod tests {
|data1: web::Data<usize>, |data1: web::Data<usize>,
data2: web::Data<char>, data2: web::Data<char>,
data3: web::Data<f64>| { data3: web::Data<f64>| {
assert_eq!(*data1, 10); assert_eq!(**data1, 10);
assert_eq!(*data2, '*'); assert_eq!(**data2, '*');
assert_eq!(*data3, 1.0); assert_eq!(**data3, 1.0);
HttpResponse::Ok() HttpResponse::Ok()
}, },
), ),

View File

@ -1106,7 +1106,7 @@ mod tests {
web::scope("app").data(10usize).route( web::scope("app").data(10usize).route(
"/t", "/t",
web::get().to(|data: web::Data<usize>| { web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10); assert_eq!(**data, 10);
let _ = data.clone(); let _ = data.clone();
HttpResponse::Ok() HttpResponse::Ok()
}), }),
@ -1125,7 +1125,7 @@ mod tests {
web::scope("app").app_data(web::Data::new(10usize)).route( web::scope("app").app_data(web::Data::new(10usize)).route(
"/t", "/t",
web::get().to(|data: web::Data<usize>| { web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10); assert_eq!(**data, 10);
let _ = data.clone(); let _ = data.clone();
HttpResponse::Ok() HttpResponse::Ok()
}), }),