Skip to content

Commit

Permalink
Make web::Data deref to Arc<T> actix#1214
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 20, 2019
1 parent 8b8a9a9 commit e5a50f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* Allow to set `peer_addr` for TestRequest #1074

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

* Rename `App::register_data()` to `App::app_data()`

* `HttpRequest::app_data<T>()` returns `Option<&T>` instead of `Option<&Data<T>>`
Expand Down
20 changes: 11 additions & 9 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ impl<T> Data<T> {
}

impl<T> Deref for Data<T> {
type Target = T;
type Target = Arc<T>;

fn deref(&self) -> &T {
self.0.as_ref()
fn deref(&self) -> &Arc<T> {
&self.0
}
}

Expand Down Expand Up @@ -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<usize>| HttpResponse::Ok()),
))
.await;
let mut srv = init_service(App::new().data("TEST".to_string()).service(
web::resource("/").to(|data: web::Data<String>| {
assert_eq!(data.to_lowercase(), "test");
HttpResponse::Ok()
}),
))
.await;

let req = TestRequest::default().to_request();
let resp = srv.call(req).await.unwrap();
Expand Down Expand Up @@ -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<usize>| {
assert_eq!(*data, 10);
assert_eq!(**data, 10);
let _ = data.clone();
HttpResponse::Ok()
},
Expand Down
6 changes: 3 additions & 3 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,9 @@ mod tests {
|data1: web::Data<usize>,
data2: web::Data<char>,
data3: web::Data<f64>| {
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()
},
),
Expand Down
4 changes: 2 additions & 2 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ mod tests {
web::scope("app").data(10usize).route(
"/t",
web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10);
assert_eq!(**data, 10);
let _ = data.clone();
HttpResponse::Ok()
}),
Expand All @@ -1125,7 +1125,7 @@ mod tests {
web::scope("app").app_data(web::Data::new(10usize)).route(
"/t",
web::get().to(|data: web::Data<usize>| {
assert_eq!(*data, 10);
assert_eq!(**data, 10);
let _ = data.clone();
HttpResponse::Ok()
}),
Expand Down

0 comments on commit e5a50f4

Please sign in to comment.