/rust/registry/src/index.crates.io-6f17d22bba15001f/egg-0.6.0/src/subst.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::fmt; |
2 | | use std::str::FromStr; |
3 | | |
4 | | use crate::{Id, Symbol}; |
5 | | |
6 | | /// A variable for use in [`Pattern`]s or [`Subst`]s. |
7 | | /// |
8 | | /// This implements [`FromStr`], and will only parse if it has a |
9 | | /// leading `?`. |
10 | | /// |
11 | | /// [`Pattern`]: struct.Pattern.html |
12 | | /// [`Subst`]: struct.Subst.html |
13 | | /// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html |
14 | 5.52M | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] Unexecuted instantiation: <egg::subst::Var as core::clone::Clone>::clone Unexecuted instantiation: <egg::subst::Var as core::clone::Clone>::clone <egg::subst::Var as core::clone::Clone>::clone Line | Count | Source | 14 | 1.77M | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
Unexecuted instantiation: <egg::subst::Var as core::clone::Clone>::clone Unexecuted instantiation: <egg::subst::Var as core::cmp::PartialEq>::eq Unexecuted instantiation: <egg::subst::Var as core::cmp::PartialEq>::eq <egg::subst::Var as core::cmp::PartialEq>::eq Line | Count | Source | 14 | 1.67M | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
<egg::subst::Var as core::cmp::PartialEq>::eq Line | Count | Source | 14 | 3.84M | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
Unexecuted instantiation: <egg::subst::Var as core::cmp::PartialOrd>::partial_cmp Unexecuted instantiation: <egg::subst::Var as core::cmp::PartialOrd>::partial_cmp Unexecuted instantiation: <egg::subst::Var as core::cmp::Ord>::cmp Unexecuted instantiation: <egg::subst::Var as core::cmp::Ord>::cmp Unexecuted instantiation: <egg::subst::Var as core::hash::Hash>::hash::<std::collections::hash::map::DefaultHasher> Unexecuted instantiation: <egg::subst::Var as core::hash::Hash>::hash::<_> <egg::subst::Var as core::hash::Hash>::hash::<std::collections::hash::map::DefaultHasher> Line | Count | Source | 14 | 1.94M | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
Unexecuted instantiation: <egg::subst::Var as core::hash::Hash>::hash::<_> |
15 | | pub struct Var(Symbol); |
16 | | |
17 | | impl FromStr for Var { |
18 | | type Err = String; |
19 | | |
20 | 1.66M | fn from_str(s: &str) -> Result<Self, Self::Err> { |
21 | 1.66M | if s.starts_with('?') && s.len() > 1 { |
22 | 1.66M | Ok(Var(s.into())) |
23 | | } else { |
24 | 0 | Err(format!("{} doesn't start with '?'", s)) |
25 | | } |
26 | 1.66M | } Unexecuted instantiation: <egg::subst::Var as core::str::traits::FromStr>::from_str <egg::subst::Var as core::str::traits::FromStr>::from_str Line | Count | Source | 20 | 1.66M | fn from_str(s: &str) -> Result<Self, Self::Err> { | 21 | 1.66M | if s.starts_with('?') && s.len() > 1 { | 22 | 1.66M | Ok(Var(s.into())) | 23 | | } else { | 24 | 0 | Err(format!("{} doesn't start with '?'", s)) | 25 | | } | 26 | 1.66M | } |
|
27 | | } |
28 | | |
29 | | impl fmt::Display for Var { |
30 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
31 | 0 | write!(f, "{}", self.0) |
32 | 0 | } Unexecuted instantiation: <egg::subst::Var as core::fmt::Display>::fmt Unexecuted instantiation: <egg::subst::Var as core::fmt::Display>::fmt |
33 | | } |
34 | | |
35 | | impl fmt::Debug for Var { |
36 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
37 | 0 | write!(f, "{}", self.0) |
38 | 0 | } Unexecuted instantiation: <egg::subst::Var as core::fmt::Debug>::fmt Unexecuted instantiation: <egg::subst::Var as core::fmt::Debug>::fmt |
39 | | } |
40 | | |
41 | | /// A substitition mapping [`Var`]s to eclass [`Id`]s. |
42 | | /// |
43 | | /// [`Var`]: struct.Var.html |
44 | | /// [`Id`]: struct.Id.html |
45 | 1.07M | #[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] Unexecuted instantiation: <egg::subst::Subst as core::default::Default>::default Unexecuted instantiation: <egg::subst::Subst as core::default::Default>::default <egg::subst::Subst as core::default::Default>::default Line | Count | Source | 45 | 1.07M | #[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
Unexecuted instantiation: <egg::subst::Subst as core::default::Default>::default Unexecuted instantiation: <egg::subst::Subst as core::clone::Clone>::clone Unexecuted instantiation: <egg::subst::Subst as core::clone::Clone>::clone Unexecuted instantiation: <egg::subst::Subst as core::cmp::PartialEq>::eq Unexecuted instantiation: <egg::subst::Subst as core::cmp::PartialEq>::eq Unexecuted instantiation: <egg::subst::Subst as core::cmp::PartialOrd>::partial_cmp Unexecuted instantiation: <egg::subst::Subst as core::cmp::PartialOrd>::partial_cmp Unexecuted instantiation: <egg::subst::Subst as core::cmp::Ord>::cmp Unexecuted instantiation: <egg::subst::Subst as core::cmp::Ord>::cmp Unexecuted instantiation: <egg::subst::Subst as core::hash::Hash>::hash::<_> Unexecuted instantiation: <egg::subst::Subst as core::hash::Hash>::hash::<_> |
46 | | pub struct Subst { |
47 | | pub(crate) vec: smallvec::SmallVec<[(Var, Id); 3]>, |
48 | | } |
49 | | |
50 | | impl Subst { |
51 | | /// Create a `Subst` with the given initial capacity |
52 | 0 | pub fn with_capacity(capacity: usize) -> Self { |
53 | 0 | Self { |
54 | 0 | vec: smallvec::SmallVec::with_capacity(capacity), |
55 | 0 | } |
56 | 0 | } Unexecuted instantiation: <egg::subst::Subst>::with_capacity Unexecuted instantiation: <egg::subst::Subst>::with_capacity |
57 | | |
58 | | /// Insert something, returning the old `Id` if present. |
59 | 1.44M | pub fn insert(&mut self, var: Var, id: Id) -> Option<Id> { |
60 | 2.05M | for pair in &mut self.vec { |
61 | 606k | if pair.0 == var { |
62 | 0 | return Some(std::mem::replace(&mut pair.1, id)); |
63 | 606k | } |
64 | | } |
65 | 1.44M | self.vec.push((var, id)); |
66 | 1.44M | None |
67 | 1.44M | } Unexecuted instantiation: <egg::subst::Subst>::insert <egg::subst::Subst>::insert Line | Count | Source | 59 | 1.44M | pub fn insert(&mut self, var: Var, id: Id) -> Option<Id> { | 60 | 2.05M | for pair in &mut self.vec { | 61 | 606k | if pair.0 == var { | 62 | 0 | return Some(std::mem::replace(&mut pair.1, id)); | 63 | 606k | } | 64 | | } | 65 | 1.44M | self.vec.push((var, id)); | 66 | 1.44M | None | 67 | 1.44M | } |
|
68 | | |
69 | | /// Retrieve a `Var`, returning `None` if not present. |
70 | | #[inline(never)] |
71 | 3.23M | pub fn get(&self, var: Var) -> Option<&Id> { |
72 | 3.23M | self.vec |
73 | 3.23M | .iter() |
74 | 3.23M | .find_map(|(v, id)| if *v == var { Some(id) } else { None })Unexecuted instantiation: <egg::subst::Subst>::get::{closure#0}<egg::subst::Subst>::get::{closure#0}Line | Count | Source | 74 | 3.23M | .find_map(|(v, id)| if *v == var { Some(id) } else { None }) |
|
75 | 3.23M | } Unexecuted instantiation: <egg::subst::Subst>::get Line | Count | Source | 71 | 3.23M | pub fn get(&self, var: Var) -> Option<&Id> { | 72 | 3.23M | self.vec | 73 | 3.23M | .iter() | 74 | 3.23M | .find_map(|(v, id)| if *v == var { Some(id) } else { None }) | 75 | 3.23M | } |
|
76 | | } |
77 | | |
78 | | impl std::ops::Index<Var> for Subst { |
79 | | type Output = Id; |
80 | | |
81 | 3.23M | fn index(&self, var: Var) -> &Self::Output { |
82 | 3.23M | match self.get(var) { |
83 | 3.23M | Some(id) => id, |
84 | 0 | None => panic!("Var '{}={}' not found in {:?}", var.0, var, self), |
85 | | } |
86 | 3.23M | } Unexecuted instantiation: <egg::subst::Subst as core::ops::index::Index<egg::subst::Var>>::index <egg::subst::Subst as core::ops::index::Index<egg::subst::Var>>::index Line | Count | Source | 81 | 3.23M | fn index(&self, var: Var) -> &Self::Output { | 82 | 3.23M | match self.get(var) { | 83 | 3.23M | Some(id) => id, | 84 | 0 | None => panic!("Var '{}={}' not found in {:?}", var.0, var, self), | 85 | | } | 86 | 3.23M | } |
|
87 | | } |
88 | | |
89 | | impl fmt::Debug for Subst { |
90 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
91 | 0 | let len = self.vec.len(); |
92 | 0 | write!(f, "{{")?; |
93 | 0 | for i in 0..len { |
94 | 0 | let (var, id) = &self.vec[i]; |
95 | 0 | write!(f, "{}: {}", var, id)?; |
96 | 0 | if i < len - 1 { |
97 | 0 | write!(f, ", ")?; |
98 | 0 | } |
99 | | } |
100 | 0 | write!(f, "}}") |
101 | 0 | } Unexecuted instantiation: <egg::subst::Subst as core::fmt::Debug>::fmt Unexecuted instantiation: <egg::subst::Subst as core::fmt::Debug>::fmt |
102 | | } |
103 | | |
104 | | #[cfg(test)] |
105 | | mod tests { |
106 | | use super::*; |
107 | | |
108 | | #[test] |
109 | | fn var_parse() { |
110 | | assert_eq!(Var::from_str("?a").unwrap().to_string(), "?a"); |
111 | | assert_eq!(Var::from_str("?abc 123").unwrap().to_string(), "?abc 123"); |
112 | | assert!(Var::from_str("a").is_err()); |
113 | | assert!(Var::from_str("a?").is_err()); |
114 | | assert!(Var::from_str("?").is_err()); |
115 | | } |
116 | | } |