From 99a1e9f42e79a73e7ec3c08eb873d0ef271319d8 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 22 Nov 2021 07:37:45 +0100 Subject: support HashSet, BinaryHeap and BTreeSet --- README.md | 4 +++- src/transform.rs | 9 +++++++-- tests/tests.rs | 13 ++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cf5d0c1..f2a0de2 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,10 @@ Dynamize also understands if you wrap associated types in the following types: * `Option<_>` * `Result<_, _>` * `some::module::Result<_>` (type alias with fixed error type) -* `Vec<_>`, `VecDeque<_>`,`LinkedList<_>` * `&mut dyn Iterator` +* `Vec<_>`, `VecDeque<_>`,`LinkedList<_>` +* `HashSet<_>`, `BinaryHeap<_>`, `BTreeSet<_>` + (these only work with `Into`-bounded associated types because they require `Eq`) Note that since these are resolved recursively you can actually nest these arbitrarily so e.g. the following also just works: diff --git a/src/transform.rs b/src/transform.rs index 3f1c313..e9da115 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -24,8 +24,13 @@ pub enum TransformError { fn is_supported_collection(ident: &Ident) -> bool { // collections added here must implement IntoIterator & FromIterator - // FromIterator must not require bounds like Eq or Ord since these are Self-referential - ident == "Vec" || ident == "VecDeque" || ident == "LinkedList" + // when adding a type here don't forget to document it in the README + ident == "Vec" + || ident == "VecDeque" + || ident == "LinkedList" + || ident == "HashSet" + || ident == "BinaryHeap" + || ident == "BTreeSet" } impl AssocTypeConversions<'_> { diff --git a/tests/tests.rs b/tests/tests.rs index 7801cfc..e004d61 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -204,7 +204,7 @@ fn test3(some: T) { println!("{}", dyn_trait); } -use std::collections::{LinkedList, VecDeque}; +use std::collections::{BTreeSet, BinaryHeap, HashSet, LinkedList, VecDeque}; #[dynamize::dynamize] trait Collections { @@ -219,6 +219,17 @@ trait Collections { fn vec_opt(&self) -> Vec>; } +#[dynamize::dynamize] +trait CollectionsRequiringEq { + type A: Into; + + fn hashset(&self) -> HashSet; + + fn binary_heap(&self) -> BinaryHeap; + + fn btree_set(&self) -> BTreeSet; +} + #[dynamize::dynamize] trait ReturnIter { type A: std::error::Error; -- cgit v1.2.3