diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-26 09:54:23 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-26 11:45:53 +0100 |
commit | 43950edc4f26a07055fb917fa8bbb262276e2a08 (patch) | |
tree | a790fdea9ab5929e6985772950999557b0f37c50 | |
parent | 74bcf35fc83c57fd7e9a31639d7dae31d4d9a050 (diff) |
doc: suggest splitting traits in two
-rw-r--r-- | README.md | 22 |
1 files changed, 17 insertions, 5 deletions
@@ -114,25 +114,37 @@ fn example(&self) -> Result<Vec<Self::Item>, Self::Error>; ## How does dynamize deal with method generics? In order to be object-safe methods must not have generics, so dynamize simply -moves them to the trait definition: +moves them to the trait definition. For the following source code: ```rust +#[dynamize::dynamize] trait Gen { - fn foobar<A>(&self, a: A) -> A; + type Result: std::fmt::Display; + + fn foo<A>(&self, a: A) -> Self::Result; + fn bar<A, B>(&self, a: A, b: B) -> Self::Result; + fn buz(&self) -> Self::Result; } ``` -becomes +dynamize generates the following trait: ```rust -trait DynGen<A> { - fn foobar(&self, a: A) -> A; +trait DynGen<A, B> { + fn foo(&self, a: A) -> Box<dyn std::fmt::Display + '_>; + fn bar(&self, a: A, b: B) -> Box<dyn std::fmt::Display + '_>; + fn buz(&self) -> Box<dyn std::fmt::Display + '_>; } ``` If two method type parameters have the same name, dynamize enforces that they also have the same bounds and only adds the parameter once to the trait. +Note that in the dynamized trait calling the `buz` method now requires you to +specify both generic types, even though they aren't actually required by the +method. You can avoid this by splitting the original trait in two, i.e. moving +the `buz` method to a separate trait, which can be dynamized separately. + ## Dynamize supports async Dynamize supports async out of the box. Since Rust however does not yet support |