diff --git a/phper/src/enums.rs b/phper/src/enums.rs index 68dd1bf..f007ccf 100644 --- a/phper/src/enums.rs +++ b/phper/src/enums.rs @@ -37,7 +37,7 @@ use std::{ cell::RefCell, ffi::{CStr, CString}, marker::PhantomData, - mem::{MaybeUninit, zeroed}, + mem::{ManuallyDrop, zeroed}, ptr::{null, null_mut}, rc::Rc, }; @@ -528,15 +528,8 @@ unsafe fn register_enum_case( ); } Scalar::String(value) => { - #[allow(clippy::useless_conversion)] - let value_ptr = phper_zend_string_init( - value.as_ptr().cast(), - value.len().try_into().unwrap(), - true.into(), - ); - let mut value = MaybeUninit::::uninit(); - phper_zval_str(value.as_mut_ptr(), value_ptr); - + let value = ZString::new_persistent(value); + let mut value = ManuallyDrop::new(ZVal::from(value)); zend_enum_add_case_cstr(class_ce, case_name.as_ptr(), value.as_mut_ptr()); } Scalar::Null => { diff --git a/phper/src/strings.rs b/phper/src/strings.rs index 6405154..2a35046 100644 --- a/phper/src/strings.rs +++ b/phper/src/strings.rs @@ -194,6 +194,17 @@ impl ZString { } } + /// Creates a new persistent zend string from a container of bytes. + #[allow(clippy::useless_conversion)] + pub fn new_persistent(s: impl AsRef<[u8]>) -> Self { + unsafe { + let s = s.as_ref(); + let ptr = + phper_zend_string_init(s.as_ptr().cast(), s.len().try_into().unwrap(), true.into()); + Self::from_raw(ptr) + } + } + /// Create owned object From raw pointer, usually used in pairs with /// `into_raw`. ///