From 7f9d988a3e0555123030f48f3759216ef2e14ec3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 8 Mar 2019 14:22:03 +0100 Subject: Generalise Context on its type of keys --- dhall_core/src/context.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'dhall_core/src') diff --git a/dhall_core/src/context.rs b/dhall_core/src/context.rs index b88a677..412d3f0 100644 --- a/dhall_core/src/context.rs +++ b/dhall_core/src/context.rs @@ -1,4 +1,6 @@ +use std::cmp::Eq; use std::collections::HashMap; +use std::hash::Hash; /// A `(Context a)` associates `Text` labels with values of type `a` /// @@ -13,9 +15,9 @@ use std::collections::HashMap; /// `n`th occurrence of a given key. /// #[derive(Debug, Clone)] -pub struct Context<'i, T>(HashMap<&'i str, Vec>); +pub struct Context(HashMap>); -impl<'i, T> Context<'i, T> { +impl Context { /// An empty context with no key-value pairs pub fn new() -> Self { Context(HashMap::new()) @@ -29,23 +31,23 @@ impl<'i, T> Context<'i, T> { /// lookup k n (insert k v c) = lookup k (n - 1) c -- 1 <= n /// lookup k n (insert j v c) = lookup k n c -- k /= j /// ``` - pub fn lookup<'a>(&'a self, k: &str, n: usize) -> Option<&'a T> { - self.0.get(k).and_then(|v| v.get(v.len() - 1 - n)) + pub fn lookup<'a>(&'a self, k: K, n: usize) -> Option<&'a T> { + self.0.get(&k).and_then(|v| v.get(v.len() - 1 - n)) } - pub fn map U>(&self, f: F) -> Context<'i, U> { + pub fn map U>(&self, f: F) -> Context { Context( self.0 .iter() - .map(|(k, v)| (*k, v.iter().map(&f).collect())) + .map(|(k, v)| ((*k).clone(), v.iter().map(&f).collect())) .collect(), ) } } -impl<'i, T: Clone> Context<'i, T> { +impl Context { /// Add a key-value pair to the `Context` - pub fn insert(&self, k: &'i str, v: T) -> Self { + pub fn insert(&self, k: K, v: T) -> Self { let mut ctx = (*self).clone(); { let m = ctx.0.entry(k).or_insert_with(Vec::new); -- cgit v1.2.3