Skip to content

Commit 245cfef

Browse files
committed
Improve Val.
1 parent 9398f5e commit 245cfef

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

phper-macros/tests/integration.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ fn test_c_str() {
66
assert_eq!(c_str!("foo"), unsafe {
77
CStr::from_ptr("foo\0".as_ptr().cast())
88
});
9-
assert_eq!(unsafe { c_str!("bar") }, unsafe {
10-
CStr::from_ptr("bar\0".as_ptr().cast())
11-
});
9+
10+
assert_eq!(
11+
{
12+
#[allow(unused_unsafe)]
13+
unsafe {
14+
c_str!("bar")
15+
}
16+
},
17+
unsafe { CStr::from_ptr("bar\0".as_ptr().cast()) }
18+
);
1219
}
1320

1421
#[test]

phper-sys/php_wrapper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void phper_zval_zval(zval *return_value, zval *zv, int copy, int dtor) {
4646
ZVAL_ZVAL(return_value, zv, copy, dtor);
4747
}
4848

49-
void phper_zval_dup(zval *return_value, zval *zv) {
49+
void phper_zval_dup(zval *return_value, const zval *zv) {
5050
ZVAL_DUP(return_value, zv);
5151
}
5252

phper-sys/php_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void phper_zval_stringl(zval *return_value, const char *s, size_t len);
2222
char *phper_z_strval_p(const zval *v);
2323
zval *phper_get_this(zend_execute_data *execute_data);
2424
void phper_zval_zval(zval *return_value, zval *zv, int copy, int dtor);
25-
void phper_zval_dup(zval *return_value, zval *zv);
25+
void phper_zval_dup(zval *return_value, const zval *zv);
2626
void phper_zval_copy(zval *return_value, zval *zv);
2727
void phper_zval_copy_value(zval *return_value, zval *zv);
2828

phper/src/values.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl Val {
164164
t.into()
165165
}
166166

167+
#[inline]
167168
pub fn into_inner(self) -> zval {
168169
self.inner
169170
}
@@ -263,6 +264,17 @@ impl Val {
263264
}
264265
}
265266

267+
pub fn as_mut_array(&mut self) -> crate::Result<&mut Array> {
268+
if self.get_type().is_array() {
269+
unsafe {
270+
let ptr = self.inner.value.arr;
271+
Ok(Array::from_mut_ptr(ptr).unwrap())
272+
}
273+
} else {
274+
Err(self.must_be_type_error("array"))
275+
}
276+
}
277+
266278
pub fn as_object(&self) -> crate::Result<&Object<()>> {
267279
if self.get_type().is_object() {
268280
unsafe {
@@ -336,6 +348,16 @@ impl Val {
336348
}
337349
}
338350

351+
impl Clone for Val {
352+
fn clone(&self) -> Self {
353+
let mut val = Val::undef();
354+
unsafe {
355+
phper_zval_dup(val.as_mut_ptr(), self.as_ptr());
356+
}
357+
val
358+
}
359+
}
360+
339361
impl EAllocatable for Val {
340362
unsafe fn free(ptr: *mut Self) {
341363
ptr.as_mut().unwrap().drop_value();

0 commit comments

Comments
 (0)