Skip to content

Commit

Permalink
Made the TS implementation for hashmap and hashset generic over all h…
Browse files Browse the repository at this point in the history
…ashers (#173)

* Made the TS implementation for hashmap and hashset generic over all hashers

* Added hashmap tests

* Change tests to expect type instead of interface

---------

Co-authored-by: Victor Koenders <git@trang.ar>
Co-authored-by: escritorio-gustavo <131818645+escritorio-gustavo@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 25, 2024
1 parent ac00146 commit d6b862f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl<T: TS> TS for Vec<T> {
}
}

impl<K: TS, V: TS> TS for HashMap<K, V> {
impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
fn name() -> String {
"Record".to_owned()
}
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<I: TS> TS for RangeInclusive<I> {
}

impl_shadow!(as T: impl<'a, T: TS + ?Sized> TS for &T);
impl_shadow!(as Vec<T>: impl<T: TS> TS for HashSet<T>);
impl_shadow!(as Vec<T>: impl<T: TS, H> TS for HashSet<T, H>);
impl_shadow!(as Vec<T>: impl<T: TS> TS for BTreeSet<T>);
impl_shadow!(as HashMap<K, V>: impl<K: TS, V: TS> TS for BTreeMap<K, V>);
impl_shadow!(as Vec<T>: impl<T: TS, const N: usize> TS for [T; N]);
Expand Down
37 changes: 37 additions & 0 deletions ts-rs/tests/hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::collections::{HashMap, HashSet};
use ts_rs::TS;

#[test]
fn hashmap() {
#[derive(TS)]
#[allow(dead_code)]
struct Hashes {
map: HashMap<String, String>,
set: HashSet<String>,
}

assert_eq!(
Hashes::decl(),
"type Hashes = { map: Record<string, string>, set: Array<string>, }"
)
}

#[test]
fn hashmap_with_custom_hasher() {
struct CustomHasher {}

type CustomHashMap<K, V> = HashMap<K, V, CustomHasher>;
type CustomHashSet<K> = HashSet<K, CustomHasher>;

#[derive(TS)]
#[allow(dead_code)]
struct Hashes {
map: CustomHashMap<String, String>,
set: CustomHashSet<String>,
}

assert_eq!(
Hashes::decl(),
"type Hashes = { map: Record<string, string>, set: Array<string>, }"
)
}

0 comments on commit d6b862f

Please sign in to comment.