Skip to content

feat: Introduce new_persistent method for ZString #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions phper/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<zval>::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 => {
Expand Down
11 changes: 11 additions & 0 deletions phper/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down
Loading