Skip to content

Commit a74a744

Browse files
committed
Modify Val from api.
1 parent 6918134 commit a74a744

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

examples/hello/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn module_init(_args: ModuleArgs) -> bool {
1919
true
2020
}
2121

22-
fn say_hello(arguments: &mut [Val]) -> String {
22+
fn say_hello(arguments: &mut [Val]) -> impl SetVal {
2323
let name = arguments[0].as_string();
2424
format!("Hello, {}!\n", name)
2525
}
@@ -52,15 +52,13 @@ pub extern "C" fn get_module(module: &mut Module) {
5252
module.add_function("hello_throw_exception", throw_exception, vec![]);
5353
module.add_function(
5454
"hello_get_all_ini",
55-
|_: &mut [Val]| -> Array {
55+
|_: &mut [Val]| {
5656
let mut arr = Array::new();
5757

58-
let mut hello_enable = Val::null();
59-
Module::get_bool_ini("hello.enable").set_val(&mut hello_enable);
58+
let mut hello_enable = Val::new(Module::get_bool_ini("hello.enable"));
6059
arr.insert("hello.enable", &mut hello_enable);
6160

62-
let mut hello_description = Val::null();
63-
Module::get_str_ini("hello.description").set_val(&mut hello_description);
61+
let mut hello_description = Val::new(Module::get_str_ini("hello.description"));
6462
arr.insert("hello.description", &mut hello_description);
6563

6664
arr

examples/hello/tests/integration.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ use std::{env, path::Path};
33

44
#[test]
55
fn test_php() {
6-
test_php_scripts(
7-
Path::new(env!("CARGO_MANIFEST_DIR"))
8-
.join("..")
9-
.join("..")
10-
.join("target"),
11-
env!("CARGO_PKG_NAME"),
12-
&[Path::new(env!("CARGO_MANIFEST_DIR"))
13-
.join("tests")
14-
.join("php")
15-
.join("test.php")],
16-
);
6+
dbg!(env!("CARGO_BIN_EXE_hello"));
7+
dbg!(env::current_exe());
8+
// test_php_scripts(
9+
// Path::new(env!("CARGO_MANIFEST_DIR"))
10+
// .join("..")
11+
// .join("..")
12+
// .join("target"),
13+
// env!("CARGO_PKG_NAME"),
14+
// &[Path::new(env!("CARGO_MANIFEST_DIR"))
15+
// .join("tests")
16+
// .join("php")
17+
// .join("test.php")],
18+
// );
1719
}

phper/src/values.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl ExecuteData {
6767
let num_args = self.num_args();
6868
let mut arguments = vec![zeroed::<zval>(); num_args as usize];
6969
_zend_get_parameters_array_ex(num_args as c_int, arguments.as_mut_ptr());
70-
arguments.into_iter().map(Val::new).collect()
70+
arguments.into_iter().map(Val::from_inner).collect()
7171
}
7272
}
7373

@@ -77,8 +77,14 @@ pub struct Val {
7777
}
7878

7979
impl Val {
80+
pub fn new<T: SetVal>(t: T) -> Self {
81+
let mut val = Self::empty();
82+
val.set(t);
83+
val
84+
}
85+
8086
#[inline]
81-
pub const fn new(inner: zval) -> Self {
87+
pub const fn from_inner(inner: zval) -> Self {
8288
Self { inner }
8389
}
8490

@@ -100,6 +106,12 @@ impl Val {
100106
val
101107
}
102108

109+
pub fn from_bool(b: bool) -> Self {
110+
let mut val = Self::empty();
111+
val.set(b);
112+
val
113+
}
114+
103115
pub fn from_val(other: &Val) -> Self {
104116
let mut val = Self::empty();
105117
val.set(other);
@@ -255,6 +267,7 @@ impl<T: SetVal + ?Sized> SetVal for &T {
255267
T::set_val(self, val)
256268
}
257269
}
270+
258271
impl<T: SetVal + ?Sized> SetVal for &mut T {
259272
fn set_val(&self, val: &mut Val) {
260273
T::set_val(self, val)

0 commit comments

Comments
 (0)