Skip to content

Commit 6d4aacd

Browse files
committed
improve SetExt to operate on &Set and Set
and do some mild renaming
1 parent 54c8544 commit 6d4aacd

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

crates/formality-core/src/collections.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,37 @@ impl<T: Ord + Clone> VecExt<T> for Vec<T> {
7070
}
7171

7272
pub trait SetExt<T>: Sized {
73-
fn split_first(self) -> Option<(T, Self)>;
73+
fn split_first(self) -> Option<(T, Set<T>)>;
7474

75-
fn union_with(self, other: Self) -> Self;
75+
fn union_with(self, other: impl Upcast<Set<T>>) -> Set<T>;
7676

77-
fn plus(self, other: T) -> Self;
77+
fn with_element(self, other: impl Upcast<T>) -> Set<T>;
7878

79-
fn split_nth(&self, i: usize) -> Option<(T, Self)>;
79+
fn without_element(self, other: impl Upcast<T>) -> Set<T>;
80+
81+
fn split_nth(&self, i: usize) -> Option<(T, Set<T>)>;
82+
}
83+
84+
impl<T: Ord + Clone> SetExt<T> for &Set<T> {
85+
fn split_first(self) -> Option<(T, Set<T>)> {
86+
self.clone().split_first()
87+
}
88+
89+
fn union_with(self, other: impl Upcast<Set<T>>) -> Set<T> {
90+
self.clone().union_with(other)
91+
}
92+
93+
fn with_element(self, other: impl Upcast<T>) -> Set<T> {
94+
self.clone().with_element(other)
95+
}
96+
97+
fn without_element(self, other: impl Upcast<T>) -> Set<T> {
98+
self.clone().without_element(other)
99+
}
100+
101+
fn split_nth(&self, i: usize) -> Option<(T, Set<T>)> {
102+
<Set<T>>::split_nth(self, i)
103+
}
80104
}
81105

82106
impl<T: Ord + Clone> SetExt<T> for Set<T> {
@@ -87,22 +111,28 @@ impl<T: Ord + Clone> SetExt<T> for Set<T> {
87111
Some((e, c))
88112
}
89113

90-
fn split_nth(&self, i: usize) -> Option<(T, Self)> {
114+
fn split_nth(&self, i: usize) -> Option<(T, Set<T>)> {
91115
let mut s = self.clone();
92116
let item = self.iter().skip(i).next()?;
93117
let item = s.take(item).unwrap();
94118
Some((item, s))
95119
}
96120

97-
fn union_with(mut self, other: Self) -> Self {
121+
fn union_with(mut self, other: impl Upcast<Set<T>>) -> Set<T> {
122+
let other: Set<T> = other.upcast();
98123
for item in other {
99124
self.insert(item);
100125
}
101126
self
102127
}
103128

104-
fn plus(mut self, other: T) -> Self {
105-
self.insert(other);
129+
fn with_element(mut self, other: impl Upcast<T>) -> Set<T> {
130+
self.insert(other.upcast());
131+
self
132+
}
133+
134+
fn without_element(mut self, other: impl Upcast<T>) -> Set<T> {
135+
self.remove(&other.upcast());
106136
self
107137
}
108138
}

crates/formality-core/src/judgment/proven_set.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,12 @@ impl FailedJudgment {
316316
// This will return a boolean indicating if all the failed rules
317317
// ultimately failed because of a cycle.
318318

319+
let mut stack1 = stack.clone();
320+
stack1.insert(&judgment.judgment);
321+
319322
let judgment_has_non_cycle;
320-
(judgment.failed_rules, judgment_has_non_cycle) = Self::strip_cycles(
321-
stack.clone().plus(&judgment.judgment),
322-
judgment.failed_rules,
323-
);
323+
(judgment.failed_rules, judgment_has_non_cycle) =
324+
Self::strip_cycles(stack1, judgment.failed_rules);
324325
failed_rule.cause = RuleFailureCause::FailedJudgment(judgment);
325326

326327
if judgment_has_non_cycle.0 {

0 commit comments

Comments
 (0)