cast hasmap

This commit is contained in:
Robin Appelman 2019-04-04 22:20:58 +02:00
commit a0af05508f
2 changed files with 8 additions and 10 deletions

View file

@ -1,5 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::fmt::Display;
use std::hash::Hash;
use std::intrinsics::transmute; use std::intrinsics::transmute;
use std::mem::size_of; use std::mem::size_of;
use std::os::raw::c_char; use std::os::raw::c_char;
@ -8,8 +10,6 @@ use std::str;
use ivory_sys::{zend_execute_data, zend_string, zval}; use ivory_sys::{zend_execute_data, zend_string, zval};
use crate::CastError; use crate::CastError;
use std::fmt::Display;
use std::hash::Hash;
#[repr(transparent)] #[repr(transparent)]
pub struct ExecuteData(zend_execute_data); pub struct ExecuteData(zend_execute_data);
@ -166,7 +166,7 @@ impl From<u8> for ZValType {
} }
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ArrayKey { pub enum ArrayKey {
String(String), String(String),
Int(u64), Int(u64),
@ -306,13 +306,11 @@ impl<K: Into<ArrayKey>, T: Into<PhpVal>> From<Vec<(K, T)>> for PhpVal {
} }
} }
impl<K: Into<ArrayKey> + Hash + Eq, T: Into<PhpVal>> From<HashMap<K, T>> for PhpVal { impl<K: Into<ArrayKey> + Hash + Eq + Ord, T: Into<PhpVal>> From<HashMap<K, T>> for PhpVal {
fn from(input: HashMap<K, T>) -> Self { fn from(input: HashMap<K, T>) -> Self {
PhpVal::Array( let mut vec: Vec<(K, T)> = input.into_iter().collect();
input // since hashmap doesn't contain any stable order we sort it to get predictable results
.into_iter() vec.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));
.map(|(key, value)| (key.into(), value.into())) vec.into()
.collect(),
)
} }
} }