Skip to content

Commit efbb5cc

Browse files
committed
feat: Introduce new_persistent method for creating persistent zend strings
1 parent 53bd7a1 commit efbb5cc

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

phper/src/enums.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
errors::Throwable,
2727
functions::{Function, FunctionEntry, HandlerMap, MethodEntity},
2828
objects::ZObj,
29-
strings::ZString,
29+
strings::{ZStr, ZString},
3030
sys::*,
3131
types::Scalar,
3232
utils::ensure_end_with_zero,
@@ -37,7 +37,7 @@ use std::{
3737
cell::RefCell,
3838
ffi::{CStr, CString},
3939
marker::PhantomData,
40-
mem::{MaybeUninit, zeroed},
40+
mem::{ManuallyDrop, MaybeUninit, 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ impl ZString {
194194
}
195195
}
196196

197+
/// Creates a new persistent zend string from a container of bytes.
198+
///
199+
/// Persistent strings will remain in memory until the PHP process
200+
/// terminates.
201+
#[allow(clippy::useless_conversion)]
202+
pub fn new_persistent(s: impl AsRef<[u8]>) -> Self {
203+
unsafe {
204+
let s = s.as_ref();
205+
let ptr =
206+
phper_zend_string_init(s.as_ptr().cast(), s.len().try_into().unwrap(), true.into());
207+
Self::from_raw(ptr)
208+
}
209+
}
210+
197211
/// Create owned object From raw pointer, usually used in pairs with
198212
/// `into_raw`.
199213
///

0 commit comments

Comments
 (0)