Make maps multi-threaded again
This commit is contained in:
parent
924b388f2c
commit
b7e0828ced
@ -102,10 +102,9 @@ impl AbstractTree for For {
|
|||||||
return Ok(Value::none());
|
return Ok(Value::none());
|
||||||
}
|
}
|
||||||
|
|
||||||
let values = expression_run.as_list()?.items();
|
if let Value::List(list) = expression_run {
|
||||||
|
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
values.par_iter().try_for_each(|value| {
|
list.items().par_iter().try_for_each(|value| {
|
||||||
let iter_context = Context::with_variables_from(context)?;
|
let iter_context = Context::with_variables_from(context)?;
|
||||||
|
|
||||||
iter_context.set_value(key.clone(), value.clone())?;
|
iter_context.set_value(key.clone(), value.clone())?;
|
||||||
@ -115,12 +114,13 @@ impl AbstractTree for For {
|
|||||||
} else {
|
} else {
|
||||||
let loop_context = Context::with_variables_from(context)?;
|
let loop_context = Context::with_variables_from(context)?;
|
||||||
|
|
||||||
for value in values.iter() {
|
for value in list.items().iter() {
|
||||||
loop_context.set_value(key.clone(), value.clone())?;
|
loop_context.set_value(key.clone(), value.clone())?;
|
||||||
|
|
||||||
self.block.run(source, &loop_context)?;
|
self.block.run(source, &loop_context)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Value::none())
|
Ok(Value::none())
|
||||||
}
|
}
|
||||||
|
@ -84,6 +84,8 @@ impl AbstractTree for Index {
|
|||||||
Ok(item)
|
Ok(item)
|
||||||
}
|
}
|
||||||
Value::Map(map) => {
|
Value::Map(map) => {
|
||||||
|
let map = map.inner()?;
|
||||||
|
|
||||||
let (key, value) = if let IndexExpression::Identifier(identifier) = &self.index {
|
let (key, value) = if let IndexExpression::Identifier(identifier) = &self.index {
|
||||||
let key = identifier.inner();
|
let key = identifier.inner();
|
||||||
let value = map.get(key).unwrap_or_default();
|
let value = map.get(key).unwrap_or_default();
|
||||||
|
@ -294,17 +294,17 @@ impl AbstractTree for ValueNode {
|
|||||||
Value::Option(option_value)
|
Value::Option(option_value)
|
||||||
}
|
}
|
||||||
ValueNode::Map(key_statement_pairs, _) => {
|
ValueNode::Map(key_statement_pairs, _) => {
|
||||||
let mut map = Map::new();
|
let mut map = BTreeMap::new();
|
||||||
|
|
||||||
{
|
{
|
||||||
for (key, (statement, _)) in key_statement_pairs {
|
for (key, (statement, _)) in key_statement_pairs {
|
||||||
let value = statement.run(source, context)?;
|
let value = statement.run(source, context)?;
|
||||||
|
|
||||||
map.set(key.clone(), value);
|
map.insert(key.clone(), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Map(map)
|
Value::Map(Map::with_values(map))
|
||||||
}
|
}
|
||||||
ValueNode::BuiltInValue(built_in_value) => built_in_value.run(source, context)?,
|
ValueNode::BuiltInValue(built_in_value) => built_in_value.run(source, context)?,
|
||||||
ValueNode::Structure(node_map) => {
|
ValueNode::Structure(node_map) => {
|
||||||
|
@ -111,7 +111,7 @@ impl Callable for BuiltInFunction {
|
|||||||
let length = if let Ok(list) = value.as_list() {
|
let length = if let Ok(list) = value.as_list() {
|
||||||
list.items().len()
|
list.items().len()
|
||||||
} else if let Ok(map) = value.as_map() {
|
} else if let Ok(map) = value.as_map() {
|
||||||
map.len()
|
map.inner()?.len()
|
||||||
} else if let Ok(str) = value.as_string() {
|
} else if let Ok(str) = value.as_string() {
|
||||||
str.chars().count()
|
str.chars().count()
|
||||||
} else {
|
} else {
|
||||||
|
@ -293,7 +293,7 @@ impl Completer for DustCompleter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Value::Map(map) = built_in_value.get() {
|
if let Value::Map(map) = built_in_value.get() {
|
||||||
for (key, value) in map.iter() {
|
for (key, value) in map.inner().unwrap().iter() {
|
||||||
if key.contains(last_word) {
|
if key.contains(last_word) {
|
||||||
suggestions.push(Suggestion {
|
suggestions.push(Suggestion {
|
||||||
value: format!("{name}:{key}"),
|
value: format!("{name}:{key}"),
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use stanza::{
|
use stanza::{
|
||||||
renderer::{console::Console, Renderer},
|
renderer::{console::Console, Renderer},
|
||||||
style::{HAlign, Styles},
|
style::{HAlign, Styles},
|
||||||
@ -7,51 +6,52 @@ use stanza::{
|
|||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
|
sync::{Arc, RwLock, RwLockReadGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::value::Value;
|
use crate::{error::rw_lock_error::RwLockError, value::Value};
|
||||||
|
|
||||||
/// A collection dust variables comprised of key-value pairs.
|
/// A collection dust variables comprised of key-value pairs.
|
||||||
///
|
///
|
||||||
/// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared
|
/// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared
|
||||||
/// to one another.
|
/// to one another.
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
inner: BTreeMap<String, Value>,
|
inner: Arc<RwLock<BTreeMap<String, Value>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
/// Creates a new instace.
|
/// Creates a new instace.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Map {
|
Map {
|
||||||
inner: BTreeMap::new(),
|
inner: Arc::new(RwLock::new(BTreeMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_values(variables: BTreeMap<String, Value>) -> Self {
|
pub fn with_values(variables: BTreeMap<String, Value>) -> Self {
|
||||||
Map { inner: variables }
|
Map {
|
||||||
|
inner: Arc::new(RwLock::new(variables)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn inner(&self) -> Result<RwLockReadGuard<BTreeMap<String, Value>>, RwLockError> {
|
||||||
self.inner.len()
|
Ok(self.inner.read()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
|
pub fn get(&self, key: &str) -> Result<Option<Value>, RwLockError> {
|
||||||
self.inner.iter()
|
Ok(self.inner()?.get(key).cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, key: &str) -> Option<&Value> {
|
pub fn set(&self, key: String, value: Value) -> Result<(), RwLockError> {
|
||||||
self.inner.get(key)
|
self.inner.write()?.insert(key, value);
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set(&mut self, key: String, value: Value) {
|
Ok(())
|
||||||
self.inner.insert(key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_text_table(&self) -> Table {
|
pub fn as_text_table(&self) -> Table {
|
||||||
let mut table = Table::with_styles(Styles::default().with(HAlign::Centred));
|
let mut table = Table::with_styles(Styles::default().with(HAlign::Centred));
|
||||||
|
|
||||||
for (key, value) in &self.inner {
|
for (key, value) in self.inner().unwrap().iter() {
|
||||||
if let Value::Map(map) = value {
|
if let Value::Map(map) = value {
|
||||||
table.push_row(Row::new(
|
table.push_row(Row::new(
|
||||||
Styles::default(),
|
Styles::default(),
|
||||||
@ -92,3 +92,34 @@ impl Display for Map {
|
|||||||
f.write_str(&renderer.render(&self.as_text_table()))
|
f.write_str(&renderer.render(&self.as_text_table()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Eq for Map {}
|
||||||
|
|
||||||
|
impl PartialEq for Map {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
let left = self.inner().unwrap();
|
||||||
|
let right = other.inner().unwrap();
|
||||||
|
|
||||||
|
if left.len() != right.len() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
left.iter()
|
||||||
|
.zip(right.iter())
|
||||||
|
.all(|((left_key, left_value), (right_key, right_value))| {
|
||||||
|
left_key == right_key && left_value == right_value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for Map {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for Map {
|
||||||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
|
self.inner().unwrap().cmp(&other.inner().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,12 +3,13 @@ use crate::{error::RuntimeError, Identifier, Type, TypeSpecification};
|
|||||||
|
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{MapAccess, SeqAccess, Visitor},
|
de::{MapAccess, SeqAccess, Visitor},
|
||||||
ser::SerializeTuple,
|
ser::{SerializeMap, SerializeTuple},
|
||||||
Deserialize, Serialize, Serializer,
|
Deserialize, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
|
collections::BTreeMap,
|
||||||
convert::TryFrom,
|
convert::TryFrom,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
@ -82,7 +83,7 @@ impl Value {
|
|||||||
Value::Map(map) => {
|
Value::Map(map) => {
|
||||||
let mut identifier_types = Vec::new();
|
let mut identifier_types = Vec::new();
|
||||||
|
|
||||||
for (key, value) in map.iter() {
|
for (key, value) in map.inner().unwrap().iter() {
|
||||||
identifier_types.push((
|
identifier_types.push((
|
||||||
Identifier::new(key.clone()),
|
Identifier::new(key.clone()),
|
||||||
TypeSpecification::new(value.r#type()),
|
TypeSpecification::new(value.r#type()),
|
||||||
@ -513,7 +514,16 @@ impl Serialize for Value {
|
|||||||
list.end()
|
list.end()
|
||||||
}
|
}
|
||||||
Value::Option(inner) => inner.serialize(serializer),
|
Value::Option(inner) => inner.serialize(serializer),
|
||||||
Value::Map(inner) => inner.serialize(serializer),
|
Value::Map(map) => {
|
||||||
|
let entries = map.inner().unwrap();
|
||||||
|
let mut map = serializer.serialize_map(Some(entries.len()))?;
|
||||||
|
|
||||||
|
for (key, value) in entries.iter() {
|
||||||
|
map.serialize_entry(key, value)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
Value::Function(inner) => inner.serialize(serializer),
|
Value::Function(inner) => inner.serialize(serializer),
|
||||||
Value::Structure(inner) => inner.serialize(serializer),
|
Value::Structure(inner) => inner.serialize(serializer),
|
||||||
Value::Range(range) => range.serialize(serializer),
|
Value::Range(range) => range.serialize(serializer),
|
||||||
@ -858,13 +868,13 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
M: MapAccess<'de>,
|
M: MapAccess<'de>,
|
||||||
{
|
{
|
||||||
let mut map = Map::new();
|
let mut map = BTreeMap::new();
|
||||||
|
|
||||||
while let Some((key, value)) = access.next_entry::<String, Value>()? {
|
while let Some((key, value)) = access.next_entry::<String, Value>()? {
|
||||||
map.set(key, value);
|
map.insert(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::Map(map))
|
Ok(Value::Map(Map::with_values(map)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_enum<A>(self, data: A) -> std::result::Result<Self::Value, A::Error>
|
fn visit_enum<A>(self, data: A) -> std::result::Result<Self::Value, A::Error>
|
||||||
|
@ -92,10 +92,10 @@ fn modify_map() {
|
|||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut map = Map::new();
|
let map = Map::new();
|
||||||
|
|
||||||
map.set("x".to_string(), Value::Integer(1));
|
map.set("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
map.set("y".to_string(), Value::Integer(2));
|
map.set("y".to_string(), Value::Integer(2)).unwrap();
|
||||||
|
|
||||||
assert_eq!(Ok(Value::Map(map)), result);
|
assert_eq!(Ok(Value::Map(map)), result);
|
||||||
}
|
}
|
||||||
@ -137,10 +137,10 @@ fn modify_map_values() {
|
|||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut map = Map::new();
|
let map = Map::new();
|
||||||
|
|
||||||
map.set("x".to_string(), Value::Integer(1));
|
map.set("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
map.set("y".to_string(), Value::Integer(2));
|
map.set("y".to_string(), Value::Integer(2)).unwrap();
|
||||||
|
|
||||||
assert_eq!(Ok(Value::Map(map)), result);
|
assert_eq!(Ok(Value::Map(map)), result);
|
||||||
}
|
}
|
||||||
|
@ -89,9 +89,9 @@ fn function_context_captures_functions() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn function_context_captures_structure_definitions() {
|
fn function_context_captures_structure_definitions() {
|
||||||
let mut map = Map::new();
|
let map = Map::new();
|
||||||
|
|
||||||
map.set("name".to_string(), Value::string("bob"));
|
map.set("name".to_string(), Value::string("bob")).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpret(
|
interpret(
|
||||||
|
@ -69,10 +69,11 @@ fn empty_list() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn map() {
|
fn map() {
|
||||||
let mut map = Map::new();
|
let map = Map::new();
|
||||||
|
|
||||||
map.set("x".to_string(), Value::Integer(1));
|
map.set("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
map.set("foo".to_string(), Value::string("bar".to_string()));
|
map.set("foo".to_string(), Value::string("bar".to_string()))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(interpret("{ x = 1, foo = 'bar' }"), Ok(Value::Map(map)));
|
assert_eq!(interpret("{ x = 1, foo = 'bar' }"), Ok(Value::Map(map)));
|
||||||
}
|
}
|
||||||
@ -84,10 +85,11 @@ fn empty_map() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn map_types() {
|
fn map_types() {
|
||||||
let mut map = Map::new();
|
let map = Map::new();
|
||||||
|
|
||||||
map.set("x".to_string(), Value::Integer(1));
|
map.set("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
map.set("foo".to_string(), Value::string("bar".to_string()));
|
map.set("foo".to_string(), Value::string("bar".to_string()))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpret("{ x <int> = 1, foo <str> = 'bar' }"),
|
interpret("{ x <int> = 1, foo <str> = 'bar' }"),
|
||||||
|
Loading…
Reference in New Issue
Block a user