1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-22 17:41:11 +00:00

web: implement into_inner for Data<T: ?Sized> (#2407)

This commit is contained in:
James Rhodes 2021-10-12 18:35:33 +01:00 committed by GitHub
parent a6707fb7ee
commit 99985fc4ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View file

@ -7,6 +7,7 @@
### Changed
* Associated type `FromRequest::Config` was removed. [#2233]
* Inner field made private on `web::Payload`. [#2384]
* `Data::into_inner` and `Data::get_ref` no longer require T: Sized. [#2403]
### Removed
* `ServiceResponse::checked_expr` was a legacy and just removed. [#2401]

View file

@ -75,7 +75,9 @@ impl<T> Data<T> {
pub fn new(state: T) -> Data<T> {
Data(Arc::new(state))
}
}
impl<T: ?Sized> Data<T> {
/// Get reference to inner app data.
pub fn get_ref(&self) -> &T {
self.0.as_ref()
@ -304,4 +306,38 @@ mod tests {
let data_arc = Data::from(dyn_arc);
assert_eq!(data_arc_box.get_num(), data_arc.get_num())
}
#[actix_rt::test]
async fn test_dyn_data_into_arc() {
trait TestTrait {
fn get_num(&self) -> i32;
}
struct A {}
impl TestTrait for A {
fn get_num(&self) -> i32 {
42
}
}
let dyn_arc: Arc<dyn TestTrait> = Arc::new(A {});
let data_arc = Data::from(dyn_arc);
let arc_from_data = data_arc.clone().into_inner();
assert_eq!(data_arc.get_num(), arc_from_data.get_num())
}
#[actix_rt::test]
async fn test_get_ref_from_dyn_data() {
trait TestTrait {
fn get_num(&self) -> i32;
}
struct A {}
impl TestTrait for A {
fn get_num(&self) -> i32 {
42
}
}
let dyn_arc: Arc<dyn TestTrait> = Arc::new(A {});
let data_arc = Data::from(dyn_arc);
let ref_data = data_arc.get_ref();
assert_eq!(data_arc.get_num(), ref_data.get_num())
}
}