Skip to content

Bump to 0.6.0. #70

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 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Revert and fix clippy.
  • Loading branch information
jmjoy committed Nov 28, 2022
commit e52f1e3c21e1441eeebff741d955abead1f050d7
4 changes: 3 additions & 1 deletion phper-alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod macros;
use phper_sys::*;
use std::{
borrow::Borrow,
convert::TryInto,
mem::{size_of, ManuallyDrop},
ops::{Deref, DerefMut},
};
Expand All @@ -35,10 +36,11 @@ impl<T> EBox<T> {
/// # Panic
///
/// Panic if `size_of::<T>()` equals zero.
#[allow(clippy::useless_conversion)]
pub fn new(x: T) -> Self {
unsafe {
assert_ne!(size_of::<T>(), 0);
let ptr: *mut T = phper_emalloc(size_of::<T>()).cast();
let ptr: *mut T = phper_emalloc(size_of::<T>().try_into().unwrap()).cast();
// TODO Deal with ptr is zero, when memory limit is reached.
ptr.write(x);
Self { ptr }
Expand Down
70 changes: 50 additions & 20 deletions phper/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl ZArr {
}

/// Add or update item by key.
#[allow(clippy::useless_conversion)]
pub fn insert<'a>(&mut self, key: impl Into<InsertKey<'a>>, mut value: ZVal) {
let key = key.into();
let val = value.as_mut_ptr();
Expand All @@ -126,23 +127,23 @@ impl ZArr {
phper_zend_symtable_str_update(
self.as_mut_ptr(),
s.as_ptr().cast(),
s.len(),
s.len().try_into().unwrap(),
val,
);
}
InsertKey::Bytes(b) => {
phper_zend_symtable_str_update(
self.as_mut_ptr(),
b.as_ptr().cast(),
b.len(),
b.len().try_into().unwrap(),
val,
);
}
InsertKey::ZStr(s) => {
phper_zend_symtable_str_update(
self.as_mut_ptr(),
s.as_c_str_ptr().cast(),
s.len(),
s.len().try_into().unwrap(),
val,
);
}
Expand All @@ -162,15 +163,26 @@ impl ZArr {
self.inner_get(key)
}

#[allow(clippy::useless_conversion)]
fn inner_get<'a>(&self, key: impl Into<Key<'a>>) -> Option<&'a mut ZVal> {
let key = key.into();
let ptr = self.as_ptr() as *mut _;
unsafe {
let value = match key {
Key::Index(i) => phper_zend_hash_index_find(ptr, i),
Key::Str(s) => phper_zend_symtable_str_find(ptr, s.as_ptr().cast(), s.len()),
Key::Bytes(b) => phper_zend_symtable_str_find(ptr, b.as_ptr().cast(), b.len()),
Key::ZStr(s) => phper_zend_symtable_str_find(ptr, s.as_c_str_ptr(), s.len()),
Key::Str(s) => phper_zend_symtable_str_find(
ptr,
s.as_ptr().cast(),
s.len().try_into().unwrap(),
),
Key::Bytes(b) => phper_zend_symtable_str_find(
ptr,
b.as_ptr().cast(),
b.len().try_into().unwrap(),
),
Key::ZStr(s) => {
phper_zend_symtable_str_find(ptr, s.as_c_str_ptr(), s.len().try_into().unwrap())
}
};
if value.is_null() {
None
Expand All @@ -180,35 +192,53 @@ impl ZArr {
}
}

#[allow(clippy::useless_conversion)]
pub fn exists<'a>(&self, key: impl Into<Key<'a>>) -> bool {
let key = key.into();
let ptr = self.as_ptr() as *mut _;
unsafe {
match key {
Key::Index(i) => phper_zend_hash_index_exists(ptr, i),
Key::Str(s) => phper_zend_symtable_str_exists(ptr, s.as_ptr().cast(), s.len()),
Key::Bytes(b) => phper_zend_symtable_str_exists(ptr, b.as_ptr().cast(), b.len()),
Key::ZStr(s) => {
phper_zend_symtable_str_exists(ptr, s.to_bytes().as_ptr().cast(), s.len())
}
Key::Str(s) => phper_zend_symtable_str_exists(
ptr,
s.as_ptr().cast(),
s.len().try_into().unwrap(),
),
Key::Bytes(b) => phper_zend_symtable_str_exists(
ptr,
b.as_ptr().cast(),
b.len().try_into().unwrap(),
),
Key::ZStr(s) => phper_zend_symtable_str_exists(
ptr,
s.to_bytes().as_ptr().cast(),
s.len().try_into().unwrap(),
),
}
}
}

#[allow(clippy::useless_conversion)]
pub fn remove<'a>(&mut self, key: impl Into<Key<'a>>) -> bool {
let key = key.into();
unsafe {
match key {
Key::Index(i) => phper_zend_hash_index_del(&mut self.inner, i),
Key::Str(s) => {
phper_zend_symtable_str_del(&mut self.inner, s.as_ptr().cast(), s.len())
}
Key::Bytes(b) => {
phper_zend_symtable_str_del(&mut self.inner, b.as_ptr().cast(), b.len())
}
Key::ZStr(s) => {
phper_zend_symtable_str_del(&mut self.inner, s.as_c_str_ptr().cast(), s.len())
}
Key::Str(s) => phper_zend_symtable_str_del(
&mut self.inner,
s.as_ptr().cast(),
s.len().try_into().unwrap(),
),
Key::Bytes(b) => phper_zend_symtable_str_del(
&mut self.inner,
b.as_ptr().cast(),
b.len().try_into().unwrap(),
),
Key::ZStr(s) => phper_zend_symtable_str_del(
&mut self.inner,
s.as_c_str_ptr().cast(),
s.len().try_into().unwrap(),
),
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions phper/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use once_cell::sync::OnceCell;
use phper_alloc::ToRefOwned;
use std::{
any::{Any, TypeId},
convert::TryInto,
marker::PhantomData,
mem::{size_of, zeroed, ManuallyDrop},
os::raw::c_int,
Expand Down Expand Up @@ -252,14 +253,15 @@ impl ClassEntry {
}
}

#[allow(clippy::useless_conversion)]
fn find_global_class_entry_ptr(name: impl AsRef<str>) -> *mut zend_class_entry {
let name = name.as_ref();
let name = name.to_lowercase();
unsafe {
phper_zend_hash_str_find_ptr(
compiler_globals.class_table,
name.as_ptr().cast(),
name.len(),
name.len().try_into().unwrap(),
)
.cast()
}
Expand All @@ -282,10 +284,11 @@ impl ClassEntity {
}
}

#[allow(clippy::useless_conversion)]
pub(crate) unsafe fn init(&mut self) {
let mut class_ce = phper_init_class_entry_ex(
self.name.as_ptr().cast(),
self.name.len(),
self.name.len().try_into().unwrap(),
self.function_entries().load(Ordering::SeqCst).cast(),
);

Expand Down Expand Up @@ -358,9 +361,10 @@ impl PropertyEntity {
}
}

#[allow(clippy::useless_conversion)]
pub(crate) fn declare(&self, ce: *mut zend_class_entry) {
let name = self.name.as_ptr().cast();
let name_length = self.name.len();
let name_length = self.name.len().try_into().unwrap();
let access_type = self.visibility as u32 as i32;

unsafe {
Expand All @@ -385,7 +389,7 @@ impl PropertyEntity {
name,
name_length,
s.as_ptr().cast(),
s.len(),
s.len().try_into().unwrap(),
access_type,
);
}
Expand All @@ -395,7 +399,7 @@ impl PropertyEntity {
name,
name_length,
b.as_ptr().cast(),
b.len(),
b.len().try_into().unwrap(),
access_type,
);
}
Expand Down Expand Up @@ -427,10 +431,11 @@ fn get_object_handlers() -> &'static zend_object_handlers {
})
}

#[allow(clippy::useless_conversion)]
unsafe extern "C" fn create_object(ce: *mut zend_class_entry) -> *mut zend_object {
// Alloc more memory size to store state data.
let extend_object: *mut ExtendObject =
phper_zend_object_alloc(size_of::<ExtendObject>(), ce).cast();
phper_zend_object_alloc(size_of::<ExtendObject>().try_into().unwrap(), ce).cast();

// Common initialize process.
let object = ExtendObject::as_mut_object(extend_object);
Expand Down
4 changes: 3 additions & 1 deletion phper/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
};
use phper_alloc::ToRefOwned;
use std::{
convert::TryInto,
marker::PhantomData,
mem::{size_of, transmute, zeroed},
os::raw::c_char,
Expand Down Expand Up @@ -259,6 +260,7 @@ impl ZendFunction {
}
}

#[allow(clippy::useless_conversion)]
pub(crate) fn call(
&mut self, mut object: Option<&mut ZObj>, mut arguments: impl AsMut<[ZVal]>,
) -> crate::Result<ZVal> {
Expand All @@ -284,7 +286,7 @@ impl ZendFunction {
call_raw_common(
|ret| unsafe {
let mut fci = zend_fcall_info {
size: size_of::<zend_fcall_info>(),
size: size_of::<zend_fcall_info>().try_into().unwrap(),
function_name: ZVal::from(()).into_inner(),
retval: ret.as_mut_ptr(),
params: arguments.as_mut_ptr().cast(),
Expand Down
12 changes: 8 additions & 4 deletions phper/src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,41 @@ pub trait FromIniValue {
}

impl FromIniValue for bool {
#[allow(clippy::useless_conversion)]
fn from_ini_value(name: &str) -> Self {
unsafe {
let name_ptr = name.as_ptr() as *mut u8 as *mut c_char;
zend_ini_long(name_ptr, name.len(), 0) != 0
zend_ini_long(name_ptr, name.len().try_into().unwrap(), 0) != 0
}
}
}

impl FromIniValue for i64 {
#[allow(clippy::useless_conversion)]
fn from_ini_value(name: &str) -> Self {
unsafe {
let name_ptr = name.as_ptr() as *mut u8 as *mut c_char;
zend_ini_long(name_ptr, name.len(), 0)
zend_ini_long(name_ptr, name.len().try_into().unwrap(), 0)
}
}
}

impl FromIniValue for f64 {
#[allow(clippy::useless_conversion)]
fn from_ini_value(name: &str) -> Self {
unsafe {
let name_ptr = name.as_ptr() as *mut u8 as *mut c_char;
zend_ini_double(name_ptr, name.len(), 0)
zend_ini_double(name_ptr, name.len().try_into().unwrap(), 0)
}
}
}

impl FromIniValue for Option<&CStr> {
#[allow(clippy::useless_conversion)]
fn from_ini_value(name: &str) -> Self {
unsafe {
let name_ptr = name.as_ptr() as *mut u8 as *mut c_char;
let ptr = zend_ini_string_ex(name_ptr, name.len(), 0, null_mut());
let ptr = zend_ini_string_ex(name_ptr, name.len().try_into().unwrap(), 0, null_mut());
(!ptr.is_null()).then(|| CStr::from_ptr(ptr))
}
}
Expand Down
3 changes: 2 additions & 1 deletion phper/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl ZObj {
unsafe { ZVal::from_mut_ptr(prop) }
}

#[allow(clippy::useless_conversion)]
pub fn set_property(&mut self, name: impl AsRef<str>, val: impl Into<ZVal>) {
let name = name.as_ref();
let val = EBox::new(val.into());
Expand All @@ -182,7 +183,7 @@ impl ZObj {
self.inner.ce,
&mut zv,
name.as_ptr().cast(),
name.len(),
name.len().try_into().unwrap(),
EBox::into_raw(val).cast(),
)
}
Expand Down
5 changes: 3 additions & 2 deletions phper/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! Logs and echo facilities.

use crate::{sys::*, utils::ensure_end_with_zero};
use std::ptr::null;
use std::{convert::TryInto, ptr::null};

#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq)]
Expand All @@ -34,12 +34,13 @@ pub fn log(level: LogLevel, message: impl Into<String>) {
}
}

#[allow(clippy::useless_conversion)]
pub fn echo(message: impl Into<String>) {
let message = ensure_end_with_zero(message);
unsafe {
zend_write.expect("function zend_write can't be null")(
message.as_ptr().cast(),
message.len() - 1,
(message.len() - 1).try_into().unwrap(),
);
}
}
7 changes: 6 additions & 1 deletion phper/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,15 @@ pub struct ZString {
}

impl ZString {
#[allow(clippy::useless_conversion)]
pub fn new(s: impl AsRef<[u8]>) -> Self {
unsafe {
let s = s.as_ref();
let ptr = phper_zend_string_init(s.as_ptr().cast(), s.len(), false.into());
let ptr = phper_zend_string_init(
s.as_ptr().cast(),
s.len().try_into().unwrap(),
false.into(),
);
Self::from_raw(ptr)
}
}
Expand Down
7 changes: 6 additions & 1 deletion phper/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,15 @@ impl From<f64> for ZVal {
}

impl From<&[u8]> for ZVal {
#[allow(clippy::useless_conversion)]
fn from(b: &[u8]) -> Self {
unsafe {
let mut val = MaybeUninit::<ZVal>::uninit();
phper_zval_stringl(val.as_mut_ptr().cast(), b.as_ptr().cast(), b.len());
phper_zval_stringl(
val.as_mut_ptr().cast(),
b.as_ptr().cast(),
b.len().try_into().unwrap(),
);
val.assume_init()
}
}
Expand Down