mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Make web::Data deref to Arc<T> #1214
This commit is contained in:
parent
8b8a9a995d
commit
e5a50f423d
@ -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>>`
|
||||
|
16
src/data.rs
16
src/data.rs
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,9 +144,11 @@ 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()),
|
||||
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;
|
||||
|
||||
@ -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()
|
||||
},
|
||||
|
@ -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()
|
||||
},
|
||||
),
|
||||
|
@ -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()
|
||||
}),
|
||||
@ -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()
|
||||
}),
|
||||
|
Loading…
Reference in New Issue
Block a user