From efbb5cc58430be9debfaaece961c7dee798df269 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Wed, 9 Apr 2025 23:42:49 +0800 Subject: [PATCH 1/2] feat: Introduce new_persistent method for creating persistent zend strings --- phper/src/enums.rs | 15 ++++----------- phper/src/strings.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/phper/src/enums.rs b/phper/src/enums.rs index 68dd1bf..933f814 100644 --- a/phper/src/enums.rs +++ b/phper/src/enums.rs @@ -26,7 +26,7 @@ use crate::{ errors::Throwable, functions::{Function, FunctionEntry, HandlerMap, MethodEntity}, objects::ZObj, - strings::ZString, + strings::{ZStr, ZString}, sys::*, types::Scalar, utils::ensure_end_with_zero, @@ -37,7 +37,7 @@ use std::{ cell::RefCell, ffi::{CStr, CString}, marker::PhantomData, - mem::{MaybeUninit, zeroed}, + mem::{ManuallyDrop, MaybeUninit, 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..5e33a1f 100644 --- a/phper/src/strings.rs +++ b/phper/src/strings.rs @@ -194,6 +194,20 @@ impl ZString { } } + /// Creates a new persistent zend string from a container of bytes. + /// + /// Persistent strings will remain in memory until the PHP process + /// terminates. + #[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`. /// From aee38903fc8ff5ccfc3df82643276e40ae565089 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Wed, 9 Apr 2025 23:48:06 +0800 Subject: [PATCH 2/2] refactor: Remove unnecessary comments from ZString::new_persistent method --- phper/src/enums.rs | 4 ++-- phper/src/strings.rs | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/phper/src/enums.rs b/phper/src/enums.rs index 933f814..f007ccf 100644 --- a/phper/src/enums.rs +++ b/phper/src/enums.rs @@ -26,7 +26,7 @@ use crate::{ errors::Throwable, functions::{Function, FunctionEntry, HandlerMap, MethodEntity}, objects::ZObj, - strings::{ZStr, ZString}, + strings::ZString, sys::*, types::Scalar, utils::ensure_end_with_zero, @@ -37,7 +37,7 @@ use std::{ cell::RefCell, ffi::{CStr, CString}, marker::PhantomData, - mem::{ManuallyDrop, MaybeUninit, zeroed}, + mem::{ManuallyDrop, zeroed}, ptr::{null, null_mut}, rc::Rc, }; diff --git a/phper/src/strings.rs b/phper/src/strings.rs index 5e33a1f..2a35046 100644 --- a/phper/src/strings.rs +++ b/phper/src/strings.rs @@ -195,9 +195,6 @@ impl ZString { } /// Creates a new persistent zend string from a container of bytes. - /// - /// Persistent strings will remain in memory until the PHP process - /// terminates. #[allow(clippy::useless_conversion)] pub fn new_persistent(s: impl AsRef<[u8]>) -> Self { unsafe {