1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

Provide impl<T> From<Arc<T>> for Data<T> (#1509)

This commit is contained in:
Sven Allers 2020-05-16 01:27:03 +02:00 committed by GitHub
parent 4fc99d4a6f
commit 201090d7a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -2,6 +2,10 @@
## [Unreleased] ## [Unreleased]
### Added
* Add option to create `Data<T>` from `Arc<T>` [#1509]
### Changed ### Changed
* Resources and Scopes can now access non-overridden data types set on App (or containing scopes) when setting their own data. [#1486] * Resources and Scopes can now access non-overridden data types set on App (or containing scopes) when setting their own data. [#1486]
@ -11,7 +15,7 @@
* Bump minimum supported Rust version to 1.40 * Bump minimum supported Rust version to 1.40
[#1485]: https://github.com/actix/actix-web/pull/1485 [#1485]: https://github.com/actix/actix-web/pull/1485
[#1509]: https://github.com/actix/actix-web/pull/1509
## [3.0.0-alpha.2] - 2020-05-08 ## [3.0.0-alpha.2] - 2020-05-08

View File

@ -103,6 +103,12 @@ impl<T> Clone for Data<T> {
} }
} }
impl<T> From<Arc<T>> for Data<T> {
fn from(arc: Arc<T>) -> Self {
Data(arc)
}
}
impl<T: 'static> FromRequest for Data<T> { impl<T: 'static> FromRequest for Data<T> {
type Config = (); type Config = ();
type Error = Error; type Error = Error;
@ -281,4 +287,11 @@ mod tests {
assert_eq!(num.load(Ordering::SeqCst), 0); assert_eq!(num.load(Ordering::SeqCst), 0);
} }
#[actix_rt::test]
async fn test_data_from_arc() {
let data_new = Data::new(String::from("test-123"));
let data_from_arc = Data::from(Arc::new(String::from("test-123")));
assert_eq!(data_new.0, data_from_arc.0)
}
} }