summaryrefslogtreecommitdiff
path: root/src/eval/dict.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-07-26 23:24:50 +0200
committerLaurenz <laurmaedje@gmail.com>2022-07-26 23:27:44 +0200
commitfc574b39454aec77cf2c33270566225917c7c823 (patch)
treeccebc217ce9f869bb0078753a7749789d77db551 /src/eval/dict.rs
parent1e9a5eda48c65096b482b396d550d139a4c2e61d (diff)
New `Str` type with methods
Diffstat (limited to 'src/eval/dict.rs')
-rw-r--r--src/eval/dict.rs34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/eval/dict.rs b/src/eval/dict.rs
index 654c90eb..837933b1 100644
--- a/src/eval/dict.rs
+++ b/src/eval/dict.rs
@@ -3,11 +3,11 @@ use std::fmt::{self, Debug, Formatter, Write};
use std::ops::{Add, AddAssign};
use std::sync::Arc;
-use super::{Args, Array, Func, Machine, Value};
+use super::{Args, Array, Func, Machine, Str, Value};
use crate::diag::{StrResult, TypResult};
use crate::parse::is_ident;
use crate::syntax::Spanned;
-use crate::util::{ArcExt, EcoString};
+use crate::util::ArcExt;
/// Create a new [`Dict`] from key-value pairs.
#[allow(unused_macros)]
@@ -20,9 +20,9 @@ macro_rules! dict {
}};
}
-/// A dictionary from strings to values with clone-on-write value semantics.
+/// A reference-counted dictionary with value semantics.
#[derive(Default, Clone, PartialEq, Hash)]
-pub struct Dict(Arc<BTreeMap<EcoString, Value>>);
+pub struct Dict(Arc<BTreeMap<Str, Value>>);
impl Dict {
/// Create a new, empty dictionary.
@@ -31,7 +31,7 @@ impl Dict {
}
/// Create a new dictionary from a mapping of strings to values.
- pub fn from_map(map: BTreeMap<EcoString, Value>) -> Self {
+ pub fn from_map(map: BTreeMap<Str, Value>) -> Self {
Self(Arc::new(map))
}
@@ -54,7 +54,7 @@ impl Dict {
///
/// This inserts the key with [`None`](Value::None) as the value if not
/// present so far.
- pub fn get_mut(&mut self, key: EcoString) -> &mut Value {
+ pub fn get_mut(&mut self, key: Str) -> &mut Value {
Arc::make_mut(&mut self.0).entry(key).or_default()
}
@@ -64,7 +64,7 @@ impl Dict {
}
/// Insert a mapping from the given `key` to the given `value`.
- pub fn insert(&mut self, key: EcoString, value: Value) {
+ pub fn insert(&mut self, key: Str, value: Value) {
Arc::make_mut(&mut self.0).insert(key, value);
}
@@ -112,7 +112,7 @@ impl Dict {
}
/// Iterate over pairs of references to the contained keys and values.
- pub fn iter(&self) -> std::collections::btree_map::Iter<EcoString, Value> {
+ pub fn iter(&self) -> std::collections::btree_map::Iter<Str, Value> {
self.0.iter()
}
}
@@ -120,7 +120,7 @@ impl Dict {
/// The missing key access error message.
#[cold]
fn missing_key(key: &str) -> String {
- format!("dictionary does not contain key {:?}", EcoString::from(key))
+ format!("dictionary does not contain key {:?}", Str::from(key))
}
impl Debug for Dict {
@@ -163,21 +163,21 @@ impl AddAssign for Dict {
}
}
-impl Extend<(EcoString, Value)> for Dict {
- fn extend<T: IntoIterator<Item = (EcoString, Value)>>(&mut self, iter: T) {
+impl Extend<(Str, Value)> for Dict {
+ fn extend<T: IntoIterator<Item = (Str, Value)>>(&mut self, iter: T) {
Arc::make_mut(&mut self.0).extend(iter);
}
}
-impl FromIterator<(EcoString, Value)> for Dict {
- fn from_iter<T: IntoIterator<Item = (EcoString, Value)>>(iter: T) -> Self {
+impl FromIterator<(Str, Value)> for Dict {
+ fn from_iter<T: IntoIterator<Item = (Str, Value)>>(iter: T) -> Self {
Self(Arc::new(iter.into_iter().collect()))
}
}
impl IntoIterator for Dict {
- type Item = (EcoString, Value);
- type IntoIter = std::collections::btree_map::IntoIter<EcoString, Value>;
+ type Item = (Str, Value);
+ type IntoIter = std::collections::btree_map::IntoIter<Str, Value>;
fn into_iter(self) -> Self::IntoIter {
Arc::take(self.0).into_iter()
@@ -185,8 +185,8 @@ impl IntoIterator for Dict {
}
impl<'a> IntoIterator for &'a Dict {
- type Item = (&'a EcoString, &'a Value);
- type IntoIter = std::collections::btree_map::Iter<'a, EcoString, Value>;
+ type Item = (&'a Str, &'a Value);
+ type IntoIter = std::collections::btree_map::Iter<'a, Str, Value>;
fn into_iter(self) -> Self::IntoIter {
self.iter()