Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
//! Implement `TS` for `OrderedFloat` from ordered_float
//!
//! - `heapless-impl`
//!
//!
//! Implement `TS` for `Vec` from heapless
//!
//!
Expand Down Expand Up @@ -490,7 +490,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 @@ -579,7 +579,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>, }"
)
}
Loading