Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-implement Deref for Table #546

Merged
merged 1 commit into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/alias_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub(crate) struct AliasResolver<'a, 'b>
where
'a: 'b,
{
aliases: &'b BTreeMap<&'a str, Alias<'a>>,
recipes: &'b BTreeMap<&'a str, Recipe<'a>>,
aliases: &'b Table<'a, Alias<'a>>,
recipes: &'b Table<'a, Recipe<'a>>,
}

impl<'a: 'b, 'b> AliasResolver<'a, 'b> {
pub(crate) fn resolve_aliases(
aliases: &BTreeMap<&'a str, Alias<'a>>,
recipes: &BTreeMap<&'a str, Recipe<'a>>,
aliases: &Table<'a, Alias<'a>>,
recipes: &Table<'a, Recipe<'a>>,
) -> CompilationResult<'a, ()> {
let resolver = AliasResolver { aliases, recipes };

Expand Down
4 changes: 2 additions & 2 deletions src/assignment_evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::*;

pub(crate) struct AssignmentEvaluator<'a: 'b, 'b> {
pub(crate) assignments: &'b BTreeMap<&'a str, Assignment<'a>>,
pub(crate) assignments: &'b Table<'a, Assignment<'a>>,
pub(crate) config: &'a Config,
pub(crate) dotenv: &'b BTreeMap<String, String>,
pub(crate) evaluated: BTreeMap<&'a str, (bool, String)>,
Expand All @@ -16,7 +16,7 @@ impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
config: &'a Config,
working_directory: &'b Path,
dotenv: &'b BTreeMap<String, String>,
assignments: &BTreeMap<&'a str, Assignment<'a>>,
assignments: &Table<'a, Assignment<'a>>,
overrides: &BTreeMap<String, String>,
settings: &'b Settings<'b>,
) -> RunResult<'a, BTreeMap<&'a str, (bool, String)>> {
Expand Down
4 changes: 2 additions & 2 deletions src/assignment_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::common::*;
use CompilationErrorKind::*;

pub(crate) struct AssignmentResolver<'a: 'b, 'b> {
assignments: &'b BTreeMap<&'a str, Assignment<'a>>,
assignments: &'b Table<'a, Assignment<'a>>,
stack: Vec<&'a str>,
seen: BTreeSet<&'a str>,
evaluated: BTreeSet<&'a str>,
}

impl<'a: 'b, 'b> AssignmentResolver<'a, 'b> {
pub(crate) fn resolve_assignments(
assignments: &BTreeMap<&'a str, Assignment<'a>>,
assignments: &Table<'a, Assignment<'a>>,
) -> CompilationResult<'a, ()> {
let mut resolver = AssignmentResolver {
stack: empty(),
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) use std::{
fs,
io::{self, Write},
iter::{self, FromIterator},
ops::{Deref, Range, RangeInclusive},
ops::{Index, Range, RangeInclusive},
path::{Path, PathBuf},
process::{self, Command},
str::{self, Chars},
Expand Down
8 changes: 4 additions & 4 deletions src/recipe_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ pub(crate) struct RecipeResolver<'a: 'b, 'b> {
stack: Vec<&'a str>,
seen: BTreeSet<&'a str>,
resolved: BTreeSet<&'a str>,
recipes: &'b BTreeMap<&'a str, Recipe<'a>>,
assignments: &'b BTreeMap<&'a str, Assignment<'a>>,
recipes: &'b Table<'a, Recipe<'a>>,
assignments: &'b Table<'a, Assignment<'a>>,
}

impl<'a, 'b> RecipeResolver<'a, 'b> {
pub(crate) fn resolve_recipes(
recipes: &BTreeMap<&'a str, Recipe<'a>>,
assignments: &BTreeMap<&'a str, Assignment<'a>>,
recipes: &Table<'a, Recipe<'a>>,
assignments: &Table<'a, Assignment<'a>>,
) -> CompilationResult<'a, ()> {
let mut resolver = RecipeResolver {
seen: empty(),
Expand Down
43 changes: 35 additions & 8 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::common::*;

use std::collections::btree_map;

#[derive(Debug, PartialEq)]
pub(crate) struct Table<'key, V: Keyed<'key>> {
map: BTreeMap<&'key str, V>,
Expand All @@ -9,6 +11,30 @@ impl<'key, V: Keyed<'key>> Table<'key, V> {
pub(crate) fn insert(&mut self, value: V) {
self.map.insert(value.key(), value);
}

pub(crate) fn len(&self) -> usize {
self.map.len()
}

pub(crate) fn get(&self, key: &str) -> Option<&V> {
self.map.get(key)
}

pub(crate) fn values(&self) -> btree_map::Values<&'key str, V> {
self.map.values()
}

pub(crate) fn contains_key(&self, key: &str) -> bool {
self.map.contains_key(key)
}

pub(crate) fn keys(&self) -> btree_map::Keys<&'key str, V> {
self.map.keys()
}

pub(crate) fn iter(&self) -> btree_map::Iter<&'key str, V> {
self.map.iter()
}
}

impl<'key, V: Keyed<'key>> FromIterator<V> for Table<'key, V> {
Expand All @@ -19,28 +45,29 @@ impl<'key, V: Keyed<'key>> FromIterator<V> for Table<'key, V> {
}
}

impl<'key, V: Keyed<'key>> Deref for Table<'key, V> {
type Target = BTreeMap<&'key str, V>;
impl<'key, V: Keyed<'key>> Index<&'key str> for Table<'key, V> {
type Output = V;

fn deref(&self) -> &Self::Target {
&self.map
#[inline]
fn index(&self, key: &str) -> &V {
self.map.get(key).expect("no entry found for key")
}
}

impl<'key, V: Keyed<'key>> IntoIterator for Table<'key, V> {
type Item = (&'key str, V);
type IntoIter = std::collections::btree_map::IntoIter<&'key str, V>;
type IntoIter = btree_map::IntoIter<&'key str, V>;

fn into_iter(self) -> std::collections::btree_map::IntoIter<&'key str, V> {
fn into_iter(self) -> btree_map::IntoIter<&'key str, V> {
self.map.into_iter()
}
}

impl<'table, V: Keyed<'table> + 'table> IntoIterator for &'table Table<'table, V> {
type Item = (&'table &'table str, &'table V);
type IntoIter = std::collections::btree_map::Iter<'table, &'table str, V>;
type IntoIter = btree_map::Iter<'table, &'table str, V>;

fn into_iter(self) -> std::collections::btree_map::Iter<'table, &'table str, V> {
fn into_iter(self) -> btree_map::Iter<'table, &'table str, V> {
self.map.iter()
}
}