rustfmt util.rs

This commit is contained in:
Greg Shuflin 2021-10-27 00:40:21 -07:00
parent 0f40a7de5d
commit 4dd39fe085

View File

@ -1,31 +1,31 @@
use std::collections::HashMap; use std::{cmp::Eq, collections::HashMap, hash::Hash, ops::Deref};
use std::hash::Hash;
use std::cmp::Eq;
use std::ops::Deref;
pub fn deref_optional_box<T>(x: &Option<Box<T>>) -> Option<&T> { pub fn deref_optional_box<T>(x: &Option<Box<T>>) -> Option<&T> {
x.as_ref().map(Deref::deref) x.as_ref().map(Deref::deref)
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ScopeStack<'a, T: 'a, V: 'a, N=String> where T: Hash + Eq { pub struct ScopeStack<'a, T: 'a, V: 'a, N = String>
where T: Hash + Eq
{
parent: Option<&'a ScopeStack<'a, T, V, N>>, parent: Option<&'a ScopeStack<'a, T, V, N>>,
values: HashMap<T, V>, values: HashMap<T, V>,
scope_name: Option<N> scope_name: Option<N>,
} }
impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq { impl<'a, T, V, N> ScopeStack<'a, T, V, N>
pub fn new(scope_name: Option<N>) -> Self where T: Hash + Eq { where T: Hash + Eq
ScopeStack { {
parent: None, pub fn new(scope_name: Option<N>) -> Self
values: HashMap::new(), where T: Hash + Eq {
scope_name, ScopeStack { parent: None, values: HashMap::new(), scope_name }
} }
} pub fn insert(&mut self, key: T, value: V)
pub fn insert(&mut self, key: T, value: V) where T: Hash + Eq { where T: Hash + Eq {
self.values.insert(key, value); self.values.insert(key, value);
} }
pub fn lookup(&self, key: &T) -> Option<&V> where T: Hash + Eq { pub fn lookup(&self, key: &T) -> Option<&V>
where T: Hash + Eq {
match (self.values.get(key), self.parent) { match (self.values.get(key), self.parent) {
(None, None) => None, (None, None) => None,
(None, Some(parent)) => parent.lookup(key), (None, Some(parent)) => parent.lookup(key),
@ -33,20 +33,18 @@ impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq {
} }
} }
pub fn new_scope(&'a self, scope_name: Option<N>) -> Self where T: Hash + Eq { pub fn new_scope(&'a self, scope_name: Option<N>) -> Self
ScopeStack { where T: Hash + Eq {
parent: Some(self), ScopeStack { parent: Some(self), values: HashMap::default(), scope_name }
values: HashMap::default(),
scope_name,
}
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)> where T: Hash + Eq { pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)>
where T: Hash + Eq {
match (self.values.get(key), self.parent) { match (self.values.get(key), self.parent) {
(None, None) => None, (None, None) => None,
(None, Some(parent)) => parent.lookup_with_scope(key), (None, Some(parent)) => parent.lookup_with_scope(key),
(Some(value), _) => Some((value, self.scope_name.as_ref())) (Some(value), _) => Some((value, self.scope_name.as_ref())),
} }
} }
@ -67,5 +65,7 @@ pub fn quick_ast(input: &str) -> crate::ast::AST {
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! rc { macro_rules! rc {
($string:tt) => { Rc::new(stringify!($string).to_string()) } ($string:tt) => {
Rc::new(stringify!($string).to_string())
};
} }