Skip to content

Commit 3d33a33

Browse files
authored
feat: simplify BoundedVec::eq (#4838)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR follows up on #4830 to simplify the implementation based on discussion in that PR. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent 5992436 commit 3d33a33

1 file changed

Lines changed: 6 additions & 31 deletions

File tree

noir_stdlib/src/collections/bounded_vec.nr

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,12 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
9898

9999
impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
100100
fn eq(self, other: BoundedVec<T, MaxLen>) -> bool {
101-
let mut ret = self.len == other.len;
102-
let mut exceeded_len = false;
103-
for i in 0..MaxLen {
104-
exceeded_len |= i == self.len;
105-
if !exceeded_len {
106-
ret &= self.storage[i] == other.storage[i];
107-
}
108-
}
109-
ret
101+
// TODO: https://github.com/noir-lang/noir/issues/4837
102+
//
103+
// We make the assumption that the user has used the proper interface for working with `BoundedVec`s
104+
// rather than directly manipulating the internal fields as this can result in an inconsistent internal state.
105+
106+
(self.len == other.len) & (self.storage == other.storage)
110107
}
111108
}
112109

@@ -131,26 +128,4 @@ mod bounded_vec_tests {
131128

132129
assert(bounded_vec1 != bounded_vec2);
133130
}
134-
135-
#[test]
136-
fn equality_respects_specified_length() {
137-
let mut bounded_vec1: BoundedVec<Field, 3> = BoundedVec::new();
138-
bounded_vec1.push(1);
139-
140-
// This BoundedVec has an extra value past the end of its specified length,
141-
// this should be ignored when checking equality so they are considered equal.
142-
let mut bounded_vec2: BoundedVec<Field, 3> = BoundedVec { storage: [1, 2, 0], len: 1 };
143-
144-
assert_eq(bounded_vec1, bounded_vec2);
145-
146-
// Pushing another entry onto `bounded_vec1` to make the underlying arrays equal should
147-
// result in the `BoundedVec`s being unequal as their lengths are different.
148-
bounded_vec1.push(2);
149-
150-
assert(bounded_vec1 != bounded_vec2);
151-
152-
bounded_vec2.push(2);
153-
154-
assert_eq(bounded_vec1, bounded_vec2);
155-
}
156131
}

0 commit comments

Comments
 (0)