Skip to content

Commit 57cfa79

Browse files
authored
feat: Introduce new_persistent method for ZString (#204)
1 parent 53bd7a1 commit 57cfa79

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

phper/src/enums.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::{
3737
cell::RefCell,
3838
ffi::{CStr, CString},
3939
marker::PhantomData,
40-
mem::{MaybeUninit, zeroed},
40+
mem::{ManuallyDrop, zeroed},
4141
ptr::{null, null_mut},
4242
rc::Rc,
4343
};
@@ -528,15 +528,8 @@ unsafe fn register_enum_case(
528528
);
529529
}
530530
Scalar::String(value) => {
531-
#[allow(clippy::useless_conversion)]
532-
let value_ptr = phper_zend_string_init(
533-
value.as_ptr().cast(),
534-
value.len().try_into().unwrap(),
535-
true.into(),
536-
);
537-
let mut value = MaybeUninit::<zval>::uninit();
538-
phper_zval_str(value.as_mut_ptr(), value_ptr);
539-
531+
let value = ZString::new_persistent(value);
532+
let mut value = ManuallyDrop::new(ZVal::from(value));
540533
zend_enum_add_case_cstr(class_ce, case_name.as_ptr(), value.as_mut_ptr());
541534
}
542535
Scalar::Null => {

phper/src/strings.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ impl ZString {
194194
}
195195
}
196196

197+
/// Creates a new persistent zend string from a container of bytes.
198+
#[allow(clippy::useless_conversion)]
199+
pub fn new_persistent(s: impl AsRef<[u8]>) -> Self {
200+
unsafe {
201+
let s = s.as_ref();
202+
let ptr =
203+
phper_zend_string_init(s.as_ptr().cast(), s.len().try_into().unwrap(), true.into());
204+
Self::from_raw(ptr)
205+
}
206+
}
207+
197208
/// Create owned object From raw pointer, usually used in pairs with
198209
/// `into_raw`.
199210
///

0 commit comments

Comments
 (0)