mirror of
https://github.com/actix/actix-web.git
synced 2024-11-26 11:31:09 +00:00
web: implement into_inner
for Data<T: ?Sized>
(#2407)
This commit is contained in:
parent
a6707fb7ee
commit
99985fc4ec
2 changed files with 37 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
||||||
### Changed
|
### Changed
|
||||||
* Associated type `FromRequest::Config` was removed. [#2233]
|
* Associated type `FromRequest::Config` was removed. [#2233]
|
||||||
* Inner field made private on `web::Payload`. [#2384]
|
* Inner field made private on `web::Payload`. [#2384]
|
||||||
|
* `Data::into_inner` and `Data::get_ref` no longer require T: Sized. [#2403]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `ServiceResponse::checked_expr` was a legacy and just removed. [#2401]
|
* `ServiceResponse::checked_expr` was a legacy and just removed. [#2401]
|
||||||
|
|
36
src/data.rs
36
src/data.rs
|
@ -75,7 +75,9 @@ impl<T> Data<T> {
|
||||||
pub fn new(state: T) -> Data<T> {
|
pub fn new(state: T) -> Data<T> {
|
||||||
Data(Arc::new(state))
|
Data(Arc::new(state))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> Data<T> {
|
||||||
/// Get reference to inner app data.
|
/// Get reference to inner app data.
|
||||||
pub fn get_ref(&self) -> &T {
|
pub fn get_ref(&self) -> &T {
|
||||||
self.0.as_ref()
|
self.0.as_ref()
|
||||||
|
@ -304,4 +306,38 @@ mod tests {
|
||||||
let data_arc = Data::from(dyn_arc);
|
let data_arc = Data::from(dyn_arc);
|
||||||
assert_eq!(data_arc_box.get_num(), data_arc.get_num())
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue