diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-21 21:36:15 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-21 21:36:28 +0100 |
commit | 8345637cb9f21fdcfcaa13a48329137e5527a0d3 (patch) | |
tree | f9b2c4c74d301223a992015dcc4cd3b329fb4cd3 | |
parent | 29d0acb48a03a08eff63d0c40c2e6961f250cb89 (diff) |
auto-box all trait bounds, bump version to 0.3.2v0.3.2
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | src/parse_assoc_type.rs | 4 | ||||
-rw-r--r-- | tests/tests.rs | 2 |
4 files changed, 10 insertions, 9 deletions
@@ -1,6 +1,6 @@ [package] name = "dynamize" -version = "0.3.1" +version = "0.3.2" description = "proc macro to make traits with associated types object-safe" authors = ["Martin Fischer <martin@push-f.com>"] license = "MIT" @@ -72,12 +72,13 @@ automatically also implements `DynClient`! ## How does this actually work? The destination type of an associated type is -determined by looking at its first trait bound: +determined by looking at its trait bounds: -* `Into<T>` is mapped to `T` +* if the first trait bound is `Into<T>` the destination type is `T` -* `SomeTrait` is mapped to `Box<dyn SomeTrait>` - (for this `SomeTrait` of course needs to be object-safe) +* otherwise the destination type is the boxed trait object of all trait bounds + e.g. `Error + Send` becomes `Box<dyn Error + Send>` + (for this the first trait bound needs to be object-safe) Dynamize can convert associated types in: @@ -132,7 +133,7 @@ async functions in traits, you'll have to additionally use another library like #[blanket_impl_attr(async_trait)] #[async_trait] trait Client: Sync { - type Error: std::error::Error; + type Error: std::error::Error + Send; async fn get(&self, url: String) -> Result<Vec<u8>, Self::Error>; } diff --git a/src/parse_assoc_type.rs b/src/parse_assoc_type.rs index 048e58c..856735d 100644 --- a/src/parse_assoc_type.rs +++ b/src/parse_assoc_type.rs @@ -79,11 +79,11 @@ pub fn parse_assoc_type( } } } - let path = &bound.path; + let bounds = &assoc_type.bounds; return Ok(( &assoc_type.ident, DestType::Box(BoxType { - inner: quote! {dyn #path}, + inner: quote! {dyn #bounds}, placeholder_lifetime: !lifetime_bounds(&assoc_type.bounds) .any(|l| l.ident == "static"), }), diff --git a/tests/tests.rs b/tests/tests.rs index b326b4b..bae52bd 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -213,7 +213,7 @@ trait ReturnIter { #[dynamize::dynamize] trait FunIter { - type A: std::error::Error; + type A: std::error::Error + Send; fn foobar<F: Fn(&mut dyn Iterator<Item = Self::A>)>(&mut self, f: F); |