Skip to content

Commit

Permalink
[move] fixed find_index (MystenLabs#18842)
Browse files Browse the repository at this point in the history
## Description 

- Fixed find_index not being by-ref

## Test plan 

- new tests that force no `copy` possible

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
tnowacki authored Jul 29, 2024
1 parent 9edb722 commit 0512a87
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ module std::vector {

/// Finds the index of first element in the vector `v` that satisfies the predicate `f`.
/// Returns `some(index)` if such an element is found, otherwise `none()`.
public macro fun find_index<$T>($v: vector<$T>, $f: |&$T| -> bool): Option<u64> {
public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): Option<u64> {
let v = $v;
'find_index: {
v.length().do!(|i| if ($f(&v[i])) return 'find_index option::some(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ module std::vector_tests {
let r = vector[0, 10, 100, 1_000];
assert!(r.find_index!(|e| *e == 100).destroy_some() == 2);
assert!(r.find_index!(|e| *e == 10_000).is_none());

let v = vector[Droppable{}, Droppable{}];
let idx = v.find_index!(|e| e == Droppable{});
assert!(idx.destroy_some() == 0);
assert!(&v[idx.destroy_some()] == Droppable{});
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Method 'filter!()'
Method 'find_index!()'
INSERT TEXT: 'find_index!(|${1}| ${2})'
TARGET : '(std::vector::find_index)'
TYPE : 'fun <$T>(vector<$T>, |&$T| -> bool): Option'
TYPE : 'fun <$T>(&vector<$T>, |&$T| -> bool): Option'
Method 'fold!()'
INSERT TEXT: 'fold!(${1:init}, |${2}, ${3}| ${4})'
TARGET : '(std::vector::fold)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ module std::vector {

/// Finds the index of first element in the vector `v` that satisfies the predicate `f`.
/// Returns `some(index)` if such an element is found, otherwise `none()`.
public macro fun find_index<$T>($v: vector<$T>, $f: |&$T| -> bool): Option<u64> {
public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): Option<u64> {
let v = $v;
'find_index: {
v.length().do!(|i| if ($f(&v[i])) return 'find_index option::some(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ module std::vector_tests {
let r = vector[0, 10, 100, 1_000];
assert!(r.find_index!(|e| *e == 100).destroy_some() == 2);
assert!(r.find_index!(|e| *e == 10_000).is_none());

let v = vector[Droppable{}, Droppable{}];
let idx = v.find_index!(|e| e == Droppable{});
assert!(idx.destroy_some() == 0);
assert!(&v[idx.destroy_some()] == Droppable{});
}

#[test]
Expand Down

0 comments on commit 0512a87

Please sign in to comment.