2020-05-06 13:45:17 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
use crate::def_package;
|
2020-10-28 15:18:44 +01:00
|
|
|
use crate::dynamic::Dynamic;
|
2020-08-16 17:41:59 +02:00
|
|
|
use crate::engine::Map;
|
2020-08-14 18:04:10 +02:00
|
|
|
use crate::plugin::*;
|
2020-10-29 04:37:51 +01:00
|
|
|
use crate::utils::ImmutableString;
|
|
|
|
use crate::INT;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-10-15 16:11:18 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
use crate::engine::Array;
|
2020-04-24 06:39:24 +02:00
|
|
|
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
2020-09-13 16:12:11 +02:00
|
|
|
combine_with_exported_module!(lib, "map", map_functions);
|
2020-08-14 18:04:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
mod map_functions {
|
|
|
|
pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
|
|
|
|
map.contains_key(&prop)
|
|
|
|
}
|
|
|
|
pub fn len(map: &mut Map) -> INT {
|
2020-08-16 17:41:59 +02:00
|
|
|
map.len() as INT
|
|
|
|
}
|
2020-08-14 18:04:10 +02:00
|
|
|
pub fn clear(map: &mut Map) {
|
2020-05-13 13:21:42 +02:00
|
|
|
map.clear();
|
2020-08-14 18:04:10 +02:00
|
|
|
}
|
|
|
|
pub fn remove(x: &mut Map, name: ImmutableString) -> Dynamic {
|
|
|
|
x.remove(&name).unwrap_or_else(|| ().into())
|
|
|
|
}
|
2020-09-08 12:01:34 +02:00
|
|
|
#[rhai_fn(name = "mixin", name = "+=")]
|
2020-08-14 18:04:10 +02:00
|
|
|
pub fn mixin(map1: &mut Map, map2: Map) {
|
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
2020-08-16 17:41:59 +02:00
|
|
|
map1.insert(key, value);
|
2020-08-14 18:04:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
#[rhai_fn(name = "+")]
|
|
|
|
pub fn merge(mut map1: Map, map2: Map) -> Map {
|
2020-08-14 18:04:10 +02:00
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
|
|
|
map1.insert(key, value);
|
|
|
|
});
|
2020-08-16 17:41:59 +02:00
|
|
|
map1
|
2020-08-14 18:04:10 +02:00
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
pub fn fill_with(map1: &mut Map, map2: Map) {
|
2020-08-14 18:04:10 +02:00
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
2020-09-20 20:07:43 +02:00
|
|
|
map1.entry(key).or_insert(value);
|
2020-08-14 18:04:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-08-22 16:26:49 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
pub mod indexing {
|
2020-10-15 16:11:18 +02:00
|
|
|
pub fn keys(map: &mut Map) -> Array {
|
2020-08-22 16:26:49 +02:00
|
|
|
map.iter().map(|(k, _)| k.clone().into()).collect()
|
|
|
|
}
|
2020-10-15 16:11:18 +02:00
|
|
|
pub fn values(map: &mut Map) -> Array {
|
2020-08-22 16:26:49 +02:00
|
|
|
map.iter().map(|(_, v)| v.clone()).collect()
|
|
|
|
}
|
2020-08-14 18:04:10 +02:00
|
|
|
}
|
|
|
|
}
|