Skip to content

Commit 99a40db

Browse files
committed
feat(types): array: impl FromZval for HashSet/BTreeSet
1 parent 455b192 commit 99a40db

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/types/array/conversions/btree_set.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ where
4545
}
4646
}
4747

48+
impl<'a, V> FromZval<'a> for BTreeSet<V>
49+
where
50+
V: FromZval<'a> + Ord,
51+
{
52+
const TYPE: DataType = DataType::Array;
53+
54+
fn from_zval(zval: &'a Zval) -> Option<Self> {
55+
zval.array().and_then(|arr| arr.try_into().ok())
56+
}
57+
}
58+
4859
impl<V> IntoZval for BTreeSet<V>
4960
where
5061
V: IntoZval,
@@ -58,3 +69,25 @@ where
5869
Ok(())
5970
}
6071
}
72+
73+
#[cfg(test)]
74+
#[cfg(feature = "embed")]
75+
#[allow(clippy::unwrap_used)]
76+
mod tests {
77+
use std::collections::BTreeSet;
78+
79+
use crate::boxed::ZBox;
80+
use crate::embed::Embed;
81+
use crate::types::ZendHashTable;
82+
83+
#[test]
84+
fn test_hash_table_try_from_btree_set() {
85+
Embed::run(|| {
86+
let mut set = BTreeSet::new();
87+
set.insert("one");
88+
let ht: ZBox<ZendHashTable> = set.try_into().unwrap();
89+
assert_eq!(ht.len(), 1);
90+
assert!(ht.get(0).is_some());
91+
});
92+
}
93+
}

src/types/array/conversions/hash_set.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ where
4848
}
4949
}
5050

51+
impl<'a, V, H> FromZval<'a> for HashSet<V, H>
52+
where
53+
V: FromZval<'a> + Eq + Hash,
54+
H: BuildHasher + Default,
55+
{
56+
const TYPE: DataType = DataType::Array;
57+
58+
fn from_zval(zval: &'a Zval) -> Option<Self> {
59+
zval.array().and_then(|arr| arr.try_into().ok())
60+
}
61+
}
62+
5163
impl<V, H> IntoZval for HashSet<V, H>
5264
where
5365
V: IntoZval,
@@ -62,3 +74,25 @@ where
6274
Ok(())
6375
}
6476
}
77+
78+
#[cfg(test)]
79+
#[cfg(feature = "embed")]
80+
#[allow(clippy::unwrap_used)]
81+
mod tests {
82+
use std::collections::HashSet;
83+
84+
use crate::boxed::ZBox;
85+
use crate::embed::Embed;
86+
use crate::types::ZendHashTable;
87+
88+
#[test]
89+
fn test_hash_table_try_from_hash_set() {
90+
Embed::run(|| {
91+
let mut set = HashSet::new();
92+
set.insert("one");
93+
let ht: ZBox<ZendHashTable> = set.try_into().unwrap();
94+
assert_eq!(ht.len(), 1);
95+
assert!(ht.get(0).is_some());
96+
});
97+
}
98+
}

0 commit comments

Comments
 (0)