use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
ffi::CString,
fmt::Debug,
iter::FromIterator,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr::NonNull,
u64,
};
use crate::{
bindings::{
_Bucket, _zend_new_array, zend_array_destroy, zend_array_dup, zend_hash_clean,
zend_hash_index_del, zend_hash_index_find, zend_hash_index_update,
zend_hash_next_index_insert, zend_hash_str_del, zend_hash_str_find, zend_hash_str_update,
HT_MIN_SIZE,
},
errors::{Error, Result},
php::enums::DataType,
};
use super::zval::{FromZval, IntoZval, Zval};
pub use crate::bindings::HashTable;
impl HashTable {
pub fn len(&self) -> usize {
self.nNumOfElements as usize
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn clear(&mut self) {
unsafe { zend_hash_clean(self) }
}
pub fn get(&self, key: &'_ str) -> Option<&Zval> {
let str = CString::new(key).ok()?;
unsafe { zend_hash_str_find(self, str.as_ptr(), key.len() as _).as_ref() }
}
pub fn get_index(&self, key: u64) -> Option<&Zval> {
unsafe { zend_hash_index_find(self, key).as_ref() }
}
pub fn remove<K>(&mut self, key: &str) -> Option<()> {
let result =
unsafe { zend_hash_str_del(self, CString::new(key).ok()?.as_ptr(), key.len() as _) };
if result < 0 {
None
} else {
Some(())
}
}
pub fn remove_index(&mut self, key: u64) -> Option<()> {
let result = unsafe { zend_hash_index_del(self, key) };
if result < 0 {
None
} else {
Some(())
}
}
pub fn insert<V>(&mut self, key: &str, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe {
zend_hash_str_update(
self,
CString::new(key)?.as_ptr(),
key.len() as u64,
&mut val,
)
};
val.release();
Ok(())
}
pub fn insert_at_index<V>(&mut self, key: u64, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe { zend_hash_index_update(self, key, &mut val) };
val.release();
Ok(())
}
pub fn push<V>(&mut self, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe { zend_hash_next_index_insert(self, &mut val) };
val.release();
Ok(())
}
#[inline]
pub fn iter(&self) -> Iter {
Iter::new(self)
}
#[inline]
pub fn values(&self) -> Values {
Values::new(self)
}
pub fn to_owned(&self) -> OwnedHashTable {
let ptr = unsafe { zend_array_dup(self as *const HashTable as *mut HashTable) };
unsafe { OwnedHashTable::from_ptr(ptr) }
}
}
impl Debug for HashTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map()
.entries(
self.iter()
.map(|(k, k2, v)| (k2.unwrap_or_else(|| k.to_string()), v)),
)
.finish()
}
}
pub struct Iter<'a> {
ht: &'a HashTable,
pos: Option<NonNull<_Bucket>>,
end: Option<NonNull<_Bucket>>,
}
impl<'a> Iter<'a> {
pub fn new(ht: &'a HashTable) -> Self {
Self {
ht,
pos: NonNull::new(ht.arData),
end: NonNull::new(unsafe { ht.arData.offset(ht.nNumUsed as isize) }),
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = (u64, Option<String>, &'a Zval);
fn next(&mut self) -> Option<Self::Item> {
let pos = self.pos?;
if pos == self.end? {
return None;
}
let bucket = unsafe { pos.as_ref() };
let key = unsafe { bucket.key.as_ref() }.and_then(|s| s.try_into().ok());
self.pos = NonNull::new(unsafe { pos.as_ptr().offset(1) });
Some((bucket.h, key, &bucket.val))
}
fn count(self) -> usize
where
Self: Sized,
{
self.ht.len()
}
}
impl<'a> ExactSizeIterator for Iter<'a> {
fn len(&self) -> usize {
self.ht.len()
}
}
impl<'a> DoubleEndedIterator for Iter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
let end = self.end?;
if end == self.pos? {
return None;
}
let new_end = NonNull::new(unsafe { end.as_ptr().offset(-1) })?;
let bucket = unsafe { new_end.as_ref() };
let key = unsafe { bucket.key.as_ref() }.and_then(|s| s.try_into().ok());
self.end = Some(new_end);
Some((bucket.h, key, &bucket.val))
}
}
pub struct Values<'a>(Iter<'a>);
impl<'a> Values<'a> {
pub fn new(ht: &'a HashTable) -> Self {
Self(Iter::new(ht))
}
}
impl<'a> Iterator for Values<'a> {
type Item = &'a Zval;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(_, _, zval)| zval)
}
fn count(self) -> usize
where
Self: Sized,
{
self.0.count()
}
}
impl<'a> ExactSizeIterator for Values<'a> {
fn len(&self) -> usize {
self.0.len()
}
}
impl<'a> DoubleEndedIterator for Values<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|(_, _, zval)| zval)
}
}
pub struct OwnedHashTable {
ptr: NonNull<HashTable>,
}
impl OwnedHashTable {
pub fn new() -> Self {
Self::with_capacity(HT_MIN_SIZE)
}
pub fn with_capacity(size: u32) -> Self {
unsafe {
let ptr = _zend_new_array(size);
Self::from_ptr(ptr)
}
}
pub unsafe fn from_ptr(ptr: *mut HashTable) -> Self {
Self {
ptr: NonNull::new(ptr).expect("Invalid hashtable pointer given"),
}
}
pub fn into_inner(self) -> *mut HashTable {
let this = ManuallyDrop::new(self);
this.ptr.as_ptr()
}
}
impl Deref for OwnedHashTable {
type Target = HashTable;
fn deref(&self) -> &Self::Target {
unsafe { self.ptr.as_ref() }
}
}
impl DerefMut for OwnedHashTable {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.ptr.as_mut() }
}
}
impl Debug for OwnedHashTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.deref().fmt(f)
}
}
impl Default for OwnedHashTable {
fn default() -> Self {
Self::new()
}
}
impl Clone for OwnedHashTable {
fn clone(&self) -> Self {
self.deref().to_owned()
}
}
impl Drop for OwnedHashTable {
fn drop(&mut self) {
unsafe { zend_array_destroy(self.ptr.as_mut()) };
}
}
impl IntoZval for OwnedHashTable {
const TYPE: DataType = DataType::Array;
fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
zv.set_hashtable(self);
Ok(())
}
}
impl<'a> FromZval<'a> for &'a HashTable {
const TYPE: DataType = DataType::Array;
fn from_zval(zval: &'a Zval) -> Option<Self> {
zval.array()
}
}
impl<V> TryFrom<&HashTable> for HashMap<String, V>
where
for<'a> V: FromZval<'a>,
{
type Error = Error;
fn try_from(value: &HashTable) -> Result<Self> {
let mut hm = HashMap::with_capacity(value.len());
for (idx, key, val) in value.iter() {
hm.insert(
key.unwrap_or_else(|| idx.to_string()),
V::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?,
);
}
Ok(hm)
}
}
impl<K, V> TryFrom<HashMap<K, V>> for OwnedHashTable
where
K: AsRef<str>,
V: IntoZval,
{
type Error = Error;
fn try_from(value: HashMap<K, V>) -> Result<Self> {
let mut ht = OwnedHashTable::with_capacity(
value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
);
for (k, v) in value.into_iter() {
ht.insert(k.as_ref(), v)?;
}
Ok(ht)
}
}
impl<K, V> IntoZval for HashMap<K, V>
where
K: AsRef<str>,
V: IntoZval,
{
const TYPE: DataType = DataType::Array;
fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
let arr = self.try_into()?;
zv.set_hashtable(arr);
Ok(())
}
}
impl<T> FromZval<'_> for HashMap<String, T>
where
for<'a> T: FromZval<'a>,
{
const TYPE: DataType = DataType::Array;
fn from_zval(zval: &Zval) -> Option<Self> {
zval.array().and_then(|arr| arr.try_into().ok())
}
}
impl<T> TryFrom<&HashTable> for Vec<T>
where
for<'a> T: FromZval<'a>,
{
type Error = Error;
fn try_from(value: &HashTable) -> Result<Self> {
let mut vec = Vec::with_capacity(value.len());
for (_, _, val) in value.iter() {
vec.push(T::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?);
}
Ok(vec)
}
}
impl<T> TryFrom<Vec<T>> for OwnedHashTable
where
T: IntoZval,
{
type Error = Error;
fn try_from(value: Vec<T>) -> Result<Self> {
let mut ht = OwnedHashTable::with_capacity(
value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
);
for val in value.into_iter() {
ht.push(val)?;
}
Ok(ht)
}
}
impl<T> IntoZval for Vec<T>
where
T: IntoZval,
{
const TYPE: DataType = DataType::Array;
fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
let arr = self.try_into()?;
zv.set_hashtable(arr);
Ok(())
}
}
impl<T> FromZval<'_> for Vec<T>
where
for<'a> T: FromZval<'a>,
{
const TYPE: DataType = DataType::Array;
fn from_zval(zval: &Zval) -> Option<Self> {
zval.array().and_then(|arr| arr.try_into().ok())
}
}
impl FromIterator<Zval> for OwnedHashTable {
fn from_iter<T: IntoIterator<Item = Zval>>(iter: T) -> Self {
let mut ht = OwnedHashTable::new();
for item in iter.into_iter() {
let _ = ht.push(item);
}
ht
}
}
impl FromIterator<(u64, Zval)> for OwnedHashTable {
fn from_iter<T: IntoIterator<Item = (u64, Zval)>>(iter: T) -> Self {
let mut ht = OwnedHashTable::new();
for (key, val) in iter.into_iter() {
let _ = ht.insert_at_index(key, val);
}
ht
}
}
impl<'a> FromIterator<(&'a str, Zval)> for OwnedHashTable {
fn from_iter<T: IntoIterator<Item = (&'a str, Zval)>>(iter: T) -> Self {
let mut ht = OwnedHashTable::new();
for (key, val) in iter.into_iter() {
let _ = ht.insert(key, val);
}
ht
}
}