diff --git a/src/into_dart.rs b/src/into_dart.rs index 352268f..fb50e6d 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -1,3 +1,4 @@ +use std::collections::{HashMap, HashSet}; use std::{ ffi::{c_void, CString}, mem::ManuallyDrop, @@ -235,6 +236,12 @@ macro_rules! dart_typed_data_type_trait_impl { } } + impl IntoDart for HashSet<$rust_type> { + fn into_dart(self) -> DartCObject { + self.into_iter().collect::>().into_dart() + } + } + #[doc(hidden)] #[no_mangle] pub(crate) unsafe extern "C" fn $free_zero_copy_buffer_func( @@ -296,6 +303,37 @@ where impl IntoDartExceptPrimitive for Vec where T: IntoDartExceptPrimitive {} +impl IntoDart for HashSet +where + T: IntoDartExceptPrimitive, +{ + fn into_dart(self) -> DartCObject { + // Treated as `Vec` and become `List` in Dart. It is unordered even though the type is a "list". + self.into_iter().collect::>().into_dart() + } +} + +impl IntoDartExceptPrimitive for HashSet where T: IntoDartExceptPrimitive {} + +impl IntoDart for HashMap +where + K: IntoDart, + V: IntoDart, + (K, V): IntoDartExceptPrimitive, +{ + fn into_dart(self) -> DartCObject { + // Treated as `Vec<(K, V)>` and thus become `List` in Dart + self.into_iter().collect::>().into_dart() + } +} + +impl IntoDartExceptPrimitive for HashMap +where + K: IntoDart, + V: IntoDart, +{ +} + impl IntoDart for ZeroCopyBuffer<[T; N]> where T: DartTypedDataTypeTrait, diff --git a/tests/containers.rs b/tests/containers.rs index ed73980..76cdb3d 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -1,4 +1,5 @@ use allo_isolate::{ffi::DartCObjectType, IntoDart, Isolate, ZeroCopyBuffer}; +use std::collections::{HashMap, HashSet}; mod vm; @@ -212,6 +213,16 @@ fn main() { assert!(isolate.post((ZeroCopyBuffer(vec![-1]), vec![-1], 1.1))); assert!(isolate.post((1, 2, 3, 4, 5, 6, 7, 8, 9, (10, 11)))); + assert!( + isolate.post(HashMap::from([("key".to_owned(), "value".to_owned())])) + ); + assert!(isolate.post(HashMap::from([(100, 200)]))); + assert!(isolate.post(HashMap::from([(100, 200u8)]))); + assert!(isolate.post(HashMap::from([(100, vec![42u8])]))); + assert!(isolate.post(HashSet::from(["value".to_owned()]))); + assert!(isolate.post(HashSet::from([200]))); + assert!(isolate.post(HashSet::from([200u8]))); + println!("all done!"); }