diff --git a/CHANGES.md b/CHANGES.md index 1a44caee..ab660808 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +* Add option to create `Data` from `Arc` [#1509] + ### Changed * 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 [#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 diff --git a/src/data.rs b/src/data.rs index 0c04e1d9..e657d8b7 100644 --- a/src/data.rs +++ b/src/data.rs @@ -103,6 +103,12 @@ impl Clone for Data { } } +impl From> for Data { + fn from(arc: Arc) -> Self { + Data(arc) + } +} + impl FromRequest for Data { type Config = (); type Error = Error; @@ -281,4 +287,11 @@ mod tests { 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) + } }