diff --git a/crates/sui-framework/packages/move-stdlib/sources/vector.move b/crates/sui-framework/packages/move-stdlib/sources/vector.move index cd98ed0ae7955..55c1abac34b74 100644 --- a/crates/sui-framework/packages/move-stdlib/sources/vector.move +++ b/crates/sui-framework/packages/move-stdlib/sources/vector.move @@ -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 { + public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): Option { let v = $v; 'find_index: { v.length().do!(|i| if ($f(&v[i])) return 'find_index option::some(i)); diff --git a/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move b/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move index 2bb76d6310017..3aac6ef5b4e5f 100644 --- a/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move +++ b/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move @@ -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] diff --git a/external-crates/move/crates/move-analyzer/tests/dot_completion.exp b/external-crates/move/crates/move-analyzer/tests/dot_completion.exp index ad426b6f92568..547bc99ee900a 100644 --- a/external-crates/move/crates/move-analyzer/tests/dot_completion.exp +++ b/external-crates/move/crates/move-analyzer/tests/dot_completion.exp @@ -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)' diff --git a/external-crates/move/crates/move-stdlib/sources/vector.move b/external-crates/move/crates/move-stdlib/sources/vector.move index cd98ed0ae7955..55c1abac34b74 100644 --- a/external-crates/move/crates/move-stdlib/sources/vector.move +++ b/external-crates/move/crates/move-stdlib/sources/vector.move @@ -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 { + public macro fun find_index<$T>($v: &vector<$T>, $f: |&$T| -> bool): Option { let v = $v; 'find_index: { v.length().do!(|i| if ($f(&v[i])) return 'find_index option::some(i)); diff --git a/external-crates/move/crates/move-stdlib/tests/vector_tests.move b/external-crates/move/crates/move-stdlib/tests/vector_tests.move index 2bb76d6310017..3aac6ef5b4e5f 100644 --- a/external-crates/move/crates/move-stdlib/tests/vector_tests.move +++ b/external-crates/move/crates/move-stdlib/tests/vector_tests.move @@ -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]