Skip to content

Commit

Permalink
Provide impl<T> From<Arc<T>> for Data<T> (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenallers authored May 15, 2020
1 parent 4fc99d4 commit 201090d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

* Add option to create `Data<T>` from `Arc<T>` [#1509]

### Changed

* Resources and Scopes can now access non-overridden data types set on App (or containing scopes) when setting their own data. [#1486]
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
type Config = ();
type Error = Error;
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 201090d

Please sign in to comment.