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

Implement HashMap and HashSet #54

Merged
merged 8 commits into from
Jan 2, 2024
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
38 changes: 38 additions & 0 deletions src/into_dart.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::{
ffi::{c_void, CString},
mem::ManuallyDrop,
Expand Down Expand Up @@ -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::<Vec<_>>().into_dart()
}
}

#[doc(hidden)]
#[no_mangle]
pub(crate) unsafe extern "C" fn $free_zero_copy_buffer_func(
Expand Down Expand Up @@ -296,6 +303,37 @@ where

impl<T> IntoDartExceptPrimitive for Vec<T> where T: IntoDartExceptPrimitive {}

impl<T> IntoDart for HashSet<T>
where
T: IntoDartExceptPrimitive,
{
fn into_dart(self) -> DartCObject {
// Treated as `Vec<T>` and become `List` in Dart. It is unordered even though the type is a "list".
self.into_iter().collect::<Vec<_>>().into_dart()
}
}

impl<T> IntoDartExceptPrimitive for HashSet<T> where T: IntoDartExceptPrimitive {}

impl<K, V> IntoDart for HashMap<K, V>
where
K: IntoDart,
V: IntoDart,
(K, V): IntoDartExceptPrimitive,
{
fn into_dart(self) -> DartCObject {
// Treated as `Vec<(K, V)>` and thus become `List<dynamic>` in Dart
self.into_iter().collect::<Vec<_>>().into_dart()
}
}

impl<K, V> IntoDartExceptPrimitive for HashMap<K, V>
where
K: IntoDart,
V: IntoDart,
{
}

impl<T, const N: usize> IntoDart for ZeroCopyBuffer<[T; N]>
where
T: DartTypedDataTypeTrait,
Expand Down
11 changes: 11 additions & 0 deletions tests/containers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use allo_isolate::{ffi::DartCObjectType, IntoDart, Isolate, ZeroCopyBuffer};
use std::collections::{HashMap, HashSet};

mod vm;

Expand Down Expand Up @@ -212,6 +213,16 @@
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!");
}

Expand All @@ -227,7 +238,7 @@

#[cfg(feature = "chrono")]
fn return_chrono_naive_date_time() -> chrono::NaiveDateTime {
chrono::NaiveDate::from_ymd(2016, 7, 8).and_hms_micro(9, 10, 11, 123_456)

Check warning on line 241 in tests/containers.rs

View workflow job for this annotation

GitHub Actions / Memory Check

use of deprecated associated function `chrono::NaiveDate::from_ymd`: use `from_ymd_opt()` instead

Check warning on line 241 in tests/containers.rs

View workflow job for this annotation

GitHub Actions / Memory Check

use of deprecated method `chrono::NaiveDate::and_hms_micro`: use `and_hms_micro_opt()` instead
}
#[cfg(feature = "chrono")]
fn return_chrono_date_time_utc() -> chrono::DateTime<chrono::Utc> {
Expand Down
Loading