Skip to content

Commit a425c50

Browse files
committed
Implement SetVal for Vec and HashMap.
1 parent f761a2e commit a425c50

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

phper/src/values.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
TypeError,
99
};
1010
use std::{
11-
mem::zeroed, os::raw::c_char, slice::from_raw_parts, str, str::Utf8Error,
11+
collections::HashMap, mem::zeroed, os::raw::c_char, slice::from_raw_parts, str, str::Utf8Error,
1212
sync::atomic::Ordering,
1313
};
1414

@@ -289,6 +289,38 @@ impl SetVal for String {
289289
}
290290
}
291291

292+
impl<T: SetVal> SetVal for Vec<T> {
293+
fn set_val(self, val: &mut Val) {
294+
unsafe {
295+
phper_array_init(val.as_mut_ptr());
296+
for (k, v) in self.into_iter().enumerate() {
297+
zend_hash_index_update(
298+
(*val.as_mut_ptr()).value.arr,
299+
k as u64,
300+
Val::new(v).as_mut_ptr(),
301+
);
302+
}
303+
}
304+
}
305+
}
306+
307+
impl<K: AsRef<str>, V: SetVal> SetVal for HashMap<K, V> {
308+
fn set_val(self, val: &mut Val) {
309+
unsafe {
310+
phper_array_init(val.as_mut_ptr());
311+
for (k, v) in self {
312+
let k = k.as_ref();
313+
zend_hash_str_update(
314+
(*val.as_mut_ptr()).value.arr,
315+
k.as_ptr().cast(),
316+
k.len(),
317+
Val::new(v).as_mut_ptr(),
318+
);
319+
}
320+
}
321+
}
322+
}
323+
292324
impl SetVal for Array {
293325
fn set_val(mut self, val: &mut Val) {
294326
unsafe {

tests/integration/src/values.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use phper::{arrays::Array, modules::Module, values::Val};
2+
use std::collections::HashMap;
23

34
pub fn integrate(module: &mut Module) {
45
integrate_returns(module);
@@ -50,6 +51,26 @@ fn integrate_returns(module: &mut Module) {
5051
integration_values_return_string,
5152
vec![],
5253
);
54+
module.add_function(
55+
"integration_values_return_i64_vec",
56+
integration_values_return_i64_vec,
57+
vec![],
58+
);
59+
module.add_function(
60+
"integration_values_return_string_vec",
61+
integration_values_return_string_vec,
62+
vec![],
63+
);
64+
module.add_function(
65+
"integration_values_return_i64_map",
66+
integration_values_return_i64_map,
67+
vec![],
68+
);
69+
module.add_function(
70+
"integration_values_return_string_map",
71+
integration_values_return_string_map,
72+
vec![],
73+
);
5374
module.add_function(
5475
"integration_values_return_array",
5576
integration_values_return_array,
@@ -91,6 +112,30 @@ fn integration_values_return_string(_: &mut [Val]) -> String {
91112
"foo".to_string()
92113
}
93114

115+
fn integration_values_return_i64_vec(_: &mut [Val]) -> Vec<i64> {
116+
vec![0, 1, 2]
117+
}
118+
119+
fn integration_values_return_string_vec(_: &mut [Val]) -> Vec<String> {
120+
vec!["a".to_string(), "b".to_string(), "c".to_string()]
121+
}
122+
123+
fn integration_values_return_i64_map(_: &mut [Val]) -> HashMap<&'static str, i64> {
124+
let mut map = HashMap::new();
125+
map.insert("a", 0);
126+
map.insert("b", 1);
127+
map.insert("c", 2);
128+
map
129+
}
130+
131+
fn integration_values_return_string_map(_: &mut [Val]) -> HashMap<String, String> {
132+
let mut map = HashMap::new();
133+
map.insert("a".to_string(), "x".to_string());
134+
map.insert("b".to_string(), "y".to_string());
135+
map.insert("c".to_string(), "z".to_string());
136+
map
137+
}
138+
94139
fn integration_values_return_array(_: &mut [Val]) -> Array {
95140
let mut arr = Array::new();
96141
arr.insert("a", Val::new(1));

tests/integration/tests/php/values.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
assert_eq(integration_values_return_f64(), 64.0);
1212
assert_eq(integration_values_return_str(), "foo");
1313
assert_eq(integration_values_return_string(), "foo");
14+
assert_eq(integration_values_return_i64_vec(), [0, 1, 2]);
15+
assert_eq(integration_values_return_string_vec(), ["a", "b", "c"]);
16+
assert_eq(integration_values_return_i64_map(), ["a" => 0, "b" => 1, "c" => 2]);
17+
assert_eq(integration_values_return_string_map(), ["a" => "x", "b" => "y", "c" => "z"]);
1418
assert_eq(integration_values_return_array(), ["a" => 1, "b" => "foo"]);

0 commit comments

Comments
 (0)