Skip to content

Array btreemap conversion #520

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

Closed
Closed
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
89 changes: 79 additions & 10 deletions src/types/array.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
//! Represents an array in PHP. As all arrays in PHP are associative arrays,
//! they are represented by hash tables.

use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
ffi::CString,
fmt::{Debug, Display},
iter::FromIterator,
ptr,
str::FromStr,
};

use crate::{
boxed::{ZBox, ZBoxable},
convert::{FromZval, IntoZval},
Expand All @@ -27,6 +17,15 @@ use crate::{
flags::DataType,
types::Zval,
};
use std::{
collections::{BTreeMap, HashMap},
convert::{TryFrom, TryInto},
ffi::CString,
fmt::{Debug, Display},
iter::FromIterator,
ptr,
str::FromStr,
};

/// A PHP hashtable.
///
Expand Down Expand Up @@ -1288,6 +1287,76 @@ where
}
}

///////////////////////////////////////////
// BTreeMap
///////////////////////////////////////////

impl<'a, V> TryFrom<&'a ZendHashTable> for BTreeMap<String, V>
where
V: FromZval<'a>,
{
Comment on lines +1294 to +1297
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this here? Same for the others.

Suggested change
impl<'a, V> TryFrom<&'a ZendHashTable> for BTreeMap<String, V>
where
V: FromZval<'a>,
{
impl<'a, K, V> TryFrom<&'a ZendHashTable> for BTreeMap<K, V>
where
K: TryFrom<ArrayKey<'a>, Error = Error>,
V: FromZval<'a>,
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Xenira Not sure this is a good idea.

type Error = Error;

fn try_from(value: &'a ZendHashTable) -> Result<Self> {
let mut hm = BTreeMap::new();

for (key, val) in value {
hm.insert(
key.to_string(),
V::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?,
);
}

Ok(hm)
}
}

impl<K, V> TryFrom<BTreeMap<K, V>> for ZBox<ZendHashTable>
where
K: AsRef<str>,
V: IntoZval,
{
type Error = Error;

fn try_from(value: BTreeMap<K, V>) -> Result<Self> {
let mut ht = ZendHashTable::with_capacity(
value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
);

for (k, v) in value {
ht.insert(k.as_ref(), v)?;
}

Ok(ht)
}
}

impl<K, V> IntoZval for BTreeMap<K, V>
where
K: AsRef<str>,
V: IntoZval,
{
const TYPE: DataType = DataType::Array;
const NULLABLE: bool = false;

fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
let arr = self.try_into()?;
zv.set_hashtable(arr);
Ok(())
}
}

impl<'a, T> FromZval<'a> for BTreeMap<String, T>
where
T: FromZval<'a>,
{
const TYPE: DataType = DataType::Array;

fn from_zval(zval: &'a Zval) -> Option<Self> {
zval.array().and_then(|arr| arr.try_into().ok())
}
}

///////////////////////////////////////////
// Vec
///////////////////////////////////////////
Expand Down
Loading