From 35fa135e0978000a08c3fdf1fa7350eced6a39cb Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Sat, 14 Oct 2023 20:39:55 +0200 Subject: [PATCH 1/3] Made the TS implementation for hashmap and hashset generic over all hashers --- ts-rs/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index a624cf695..e959cb5f3 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -104,7 +104,7 @@ //! Implement `TS` for `OrderedFloat` from ordered_float //! //! - `heapless-impl` -//! +//! //! Implement `TS` for `Vec` from heapless //! //! @@ -490,7 +490,7 @@ impl TS for Vec { } } -impl TS for HashMap { +impl TS for HashMap { fn name() -> String { "Record".to_owned() } @@ -579,7 +579,7 @@ impl TS for RangeInclusive { } impl_shadow!(as T: impl<'a, T: TS + ?Sized> TS for &T); -impl_shadow!(as Vec: impl TS for HashSet); +impl_shadow!(as Vec: impl TS for HashSet); impl_shadow!(as Vec: impl TS for BTreeSet); impl_shadow!(as HashMap: impl TS for BTreeMap); impl_shadow!(as Vec: impl TS for [T; N]); From 457addb60f102f4664d8229249ae14f733b8e60a Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Sat, 14 Oct 2023 20:50:53 +0200 Subject: [PATCH 2/3] Added hashmap tests --- ts-rs/tests/hashmap.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ts-rs/tests/hashmap.rs diff --git a/ts-rs/tests/hashmap.rs b/ts-rs/tests/hashmap.rs new file mode 100644 index 000000000..f03f56519 --- /dev/null +++ b/ts-rs/tests/hashmap.rs @@ -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, + set: HashSet, + } + + assert_eq!( + Hashes::decl(), + "interface Hashes { map: Record, set: Array, }" + ) +} + +#[test] +fn hashmap_with_custom_hasher() { + struct CustomHasher {} + + type CustomHashMap = HashMap; + type CustomHashSet = HashSet; + + #[derive(TS)] + #[allow(dead_code)] + struct Hashes { + map: CustomHashMap, + set: CustomHashSet, + } + + assert_eq!( + Hashes::decl(), + "interface Hashes { map: Record, set: Array, }" + ) +} From aecb03168f95ba0eed2109f677c0b37f7890b56a Mon Sep 17 00:00:00 2001 From: escritorio-gustavo <131818645+escritorio-gustavo@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:56:00 -0300 Subject: [PATCH 3/3] Change tests to expect type instead of interface --- ts-rs/tests/hashmap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ts-rs/tests/hashmap.rs b/ts-rs/tests/hashmap.rs index f03f56519..a588e9c28 100644 --- a/ts-rs/tests/hashmap.rs +++ b/ts-rs/tests/hashmap.rs @@ -12,7 +12,7 @@ fn hashmap() { assert_eq!( Hashes::decl(), - "interface Hashes { map: Record, set: Array, }" + "type Hashes = { map: Record, set: Array, }" ) } @@ -32,6 +32,6 @@ fn hashmap_with_custom_hasher() { assert_eq!( Hashes::decl(), - "interface Hashes { map: Record, set: Array, }" + "type Hashes = { map: Record, set: Array, }" ) }