diff --git a/CHANGES.md b/CHANGES.md index 28187968..66b214bd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ * Allow to set `peer_addr` for TestRequest #1074 +* Make web::Data deref to Arc #1214 + * Rename `App::register_data()` to `App::app_data()` * `HttpRequest::app_data()` returns `Option<&T>` instead of `Option<&Data>` diff --git a/src/data.rs b/src/data.rs index eaa2db38..2867d68f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -87,10 +87,10 @@ impl Data { } impl Deref for Data { - type Target = T; + type Target = Arc; - fn deref(&self) -> &T { - self.0.as_ref() + fn deref(&self) -> &Arc { + &self.0 } } @@ -144,11 +144,13 @@ mod tests { #[actix_rt::test] async fn test_data_extractor() { - let mut srv = - init_service(App::new().data(10usize).service( - web::resource("/").to(|_: web::Data| HttpResponse::Ok()), - )) - .await; + let mut srv = init_service(App::new().data("TEST".to_string()).service( + web::resource("/").to(|data: web::Data| { + assert_eq!(data.to_lowercase(), "test"); + HttpResponse::Ok() + }), + )) + .await; let req = TestRequest::default().to_request(); let resp = srv.call(req).await.unwrap(); @@ -220,7 +222,7 @@ mod tests { let mut srv = init_service(App::new().data(1usize).service( web::resource("/").data(10usize).route(web::get().to( |data: web::Data| { - assert_eq!(*data, 10); + assert_eq!(**data, 10); let _ = data.clone(); HttpResponse::Ok() }, diff --git a/src/resource.rs b/src/resource.rs index 8fc5973e..2ee08441 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -761,9 +761,9 @@ mod tests { |data1: web::Data, data2: web::Data, data3: web::Data| { - assert_eq!(*data1, 10); - assert_eq!(*data2, '*'); - assert_eq!(*data3, 1.0); + assert_eq!(**data1, 10); + assert_eq!(**data2, '*'); + assert_eq!(**data3, 1.0); HttpResponse::Ok() }, ), diff --git a/src/scope.rs b/src/scope.rs index 5a965292..18e775e6 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1106,7 +1106,7 @@ mod tests { web::scope("app").data(10usize).route( "/t", web::get().to(|data: web::Data| { - assert_eq!(*data, 10); + assert_eq!(**data, 10); let _ = data.clone(); HttpResponse::Ok() }), @@ -1125,7 +1125,7 @@ mod tests { web::scope("app").app_data(web::Data::new(10usize)).route( "/t", web::get().to(|data: web::Data| { - assert_eq!(*data, 10); + assert_eq!(**data, 10); let _ = data.clone(); HttpResponse::Ok() }),