From 8309591862f5d49617a08b137e975534089577d1 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sun, 31 Dec 2023 21:23:13 +0800 Subject: [PATCH 1/8] map and set --- src/into_dart.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/into_dart.rs b/src/into_dart.rs index 352268f..237fc21 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -2,6 +2,7 @@ use std::{ ffi::{c_void, CString}, mem::ManuallyDrop, }; +use std::collections::{HashMap, HashSet}; use crate::{ dart_array::DartArray, @@ -296,6 +297,34 @@ where impl IntoDartExceptPrimitive for Vec where T: IntoDartExceptPrimitive {} +impl IntoDart for HashSet + where + T: IntoDart, +{ + fn into_dart(self) -> DartCObject { + let vec: Vec = self.into_iter().collect(); + vec.into_dart() + } +} + +impl IntoDartExceptPrimitive for HashSet where T: IntoDart {} + +impl IntoDart for HashMap + where + K: IntoDart, + V: IntoDart, +{ + fn into_dart(self) -> DartCObject { + let vec: Vec<(K, V)> = self.into_iter().collect(); + vec.into_dart() + } +} + +impl IntoDartExceptPrimitive for HashMap where + K: IntoDart, + V: IntoDart, +{} + impl IntoDart for ZeroCopyBuffer<[T; N]> where T: DartTypedDataTypeTrait, From f2de7b4f9deb411a08f2132ee5a33e6b27b6d26a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sun, 31 Dec 2023 21:24:27 +0800 Subject: [PATCH 2/8] test --- tests/containers.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/containers.rs b/tests/containers.rs index ed73980..f97b1c2 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -1,3 +1,4 @@ +use std::collections::{HashMap, HashSet}; use allo_isolate::{ffi::DartCObjectType, IntoDart, Isolate, ZeroCopyBuffer}; mod vm; @@ -212,6 +213,11 @@ 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(HashSet::from(["value".to_owned()]))); + assert!(isolate.post(HashSet::from([200]))); + println!("all done!"); } From 13a3c2fae6bdcc05b21129d9393e68873d0c505c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sun, 31 Dec 2023 21:26:03 +0800 Subject: [PATCH 3/8] simplify --- src/into_dart.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/into_dart.rs b/src/into_dart.rs index 237fc21..7392a51 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -302,8 +302,7 @@ impl IntoDart for HashSet T: IntoDart, { fn into_dart(self) -> DartCObject { - let vec: Vec = self.into_iter().collect(); - vec.into_dart() + self.into_iter().collect::>().into_dart() } } @@ -315,8 +314,7 @@ impl IntoDart for HashMap V: IntoDart, { fn into_dart(self) -> DartCObject { - let vec: Vec<(K, V)> = self.into_iter().collect(); - vec.into_dart() + self.into_iter().collect::>().into_dart() } } From 2be00551e1fee3437684eae2a2ba8ee40e815ce1 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sun, 31 Dec 2023 21:28:46 +0800 Subject: [PATCH 4/8] fix: err --- src/into_dart.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/into_dart.rs b/src/into_dart.rs index 7392a51..0c7e579 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -302,7 +302,7 @@ impl IntoDart for HashSet T: IntoDart, { fn into_dart(self) -> DartCObject { - self.into_iter().collect::>().into_dart() + DartArray::from(self.into_iter()).into_dart() } } @@ -314,7 +314,7 @@ impl IntoDart for HashMap V: IntoDart, { fn into_dart(self) -> DartCObject { - self.into_iter().collect::>().into_dart() + DartArray::from(self.into_iter()).into_dart() } } From e3039ce18f95828dc4ad7716b647d31df8a1f392 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sun, 31 Dec 2023 21:31:04 +0800 Subject: [PATCH 5/8] fmt --- src/into_dart.rs | 18 ++++++++++-------- tests/containers.rs | 6 ++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/into_dart.rs b/src/into_dart.rs index 0c7e579..c28a26f 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -1,8 +1,8 @@ +use std::collections::{HashMap, HashSet}; use std::{ ffi::{c_void, CString}, mem::ManuallyDrop, }; -use std::collections::{HashMap, HashSet}; use crate::{ dart_array::DartArray, @@ -298,8 +298,8 @@ where impl IntoDartExceptPrimitive for Vec where T: IntoDartExceptPrimitive {} impl IntoDart for HashSet - where - T: IntoDart, +where + T: IntoDart, { fn into_dart(self) -> DartCObject { DartArray::from(self.into_iter()).into_dart() @@ -309,19 +309,21 @@ impl IntoDart for HashSet impl IntoDartExceptPrimitive for HashSet where T: IntoDart {} impl IntoDart for HashMap - where - K: IntoDart, - V: IntoDart, +where + K: IntoDart, + V: IntoDart, { fn into_dart(self) -> DartCObject { DartArray::from(self.into_iter()).into_dart() } } -impl IntoDartExceptPrimitive for HashMap where +impl IntoDartExceptPrimitive for HashMap +where K: IntoDart, V: IntoDart, -{} +{ +} impl IntoDart for ZeroCopyBuffer<[T; N]> where diff --git a/tests/containers.rs b/tests/containers.rs index f97b1c2..81e2a54 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -1,5 +1,5 @@ -use std::collections::{HashMap, HashSet}; use allo_isolate::{ffi::DartCObjectType, IntoDart, Isolate, ZeroCopyBuffer}; +use std::collections::{HashMap, HashSet}; mod vm; @@ -213,7 +213,9 @@ 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([("key".to_owned(), "value".to_owned())])) + ); assert!(isolate.post(HashMap::from([(100, 200)]))); assert!(isolate.post(HashSet::from(["value".to_owned()]))); assert!(isolate.post(HashSet::from([200]))); From 34e978a8c6ac6740febfd811487eec966e5641cc Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 1 Jan 2024 20:32:08 +0800 Subject: [PATCH 6/8] more --- src/into_dart.rs | 17 ++++++++++++----- tests/containers.rs | 3 +++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/into_dart.rs b/src/into_dart.rs index c28a26f..05e9b2e 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -236,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( @@ -298,23 +304,24 @@ where impl IntoDartExceptPrimitive for Vec where T: IntoDartExceptPrimitive {} impl IntoDart for HashSet -where - T: IntoDart, + where + T: IntoDartExceptPrimitive, { fn into_dart(self) -> DartCObject { - DartArray::from(self.into_iter()).into_dart() + self.into_iter().collect::>().into_dart() } } -impl IntoDartExceptPrimitive for HashSet where T: IntoDart {} +impl IntoDartExceptPrimitive for HashSet where T: IntoDartExceptPrimitive {} impl IntoDart for HashMap where K: IntoDart, V: IntoDart, + (K, V): IntoDartExceptPrimitive, { fn into_dart(self) -> DartCObject { - DartArray::from(self.into_iter()).into_dart() + self.into_iter().collect::>().into_dart() } } diff --git a/tests/containers.rs b/tests/containers.rs index 81e2a54..76cdb3d 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -217,8 +217,11 @@ fn main() { 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!"); } From 384757c945d3869ed8ee259122d382436a44f6cb Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 1 Jan 2024 20:35:05 +0800 Subject: [PATCH 7/8] fmt --- src/into_dart.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/into_dart.rs b/src/into_dart.rs index 05e9b2e..336a022 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -304,8 +304,8 @@ where impl IntoDartExceptPrimitive for Vec where T: IntoDartExceptPrimitive {} impl IntoDart for HashSet - where - T: IntoDartExceptPrimitive, +where + T: IntoDartExceptPrimitive, { fn into_dart(self) -> DartCObject { self.into_iter().collect::>().into_dart() From 876ad291342b76a97a6265f81c39913df10326c6 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Tue, 2 Jan 2024 20:31:05 +0800 Subject: [PATCH 8/8] Update into_dart.rs --- src/into_dart.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/into_dart.rs b/src/into_dart.rs index 336a022..fb50e6d 100644 --- a/src/into_dart.rs +++ b/src/into_dart.rs @@ -308,6 +308,7 @@ 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() } } @@ -321,6 +322,7 @@ where (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() } }