From 8dba8d3518693ff76d1b42cae946218e1df5c0b7 Mon Sep 17 00:00:00 2001 From: Todd Nowacki Date: Mon, 29 Jul 2024 12:25:46 -0700 Subject: [PATCH] Cherry pick #18842 (#18845) ## Description - Cherry pick #18842 - Source only change, does not affect devnet/testnet or any other build ## Test plan - CI --- ## 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: --- .../sui-framework/packages/move-stdlib/sources/vector.move | 2 +- .../packages/move-stdlib/tests/vector_tests.move | 5 +++++ .../move/crates/move-analyzer/tests/dot_completion.exp | 2 +- external-crates/move/crates/move-stdlib/sources/vector.move | 2 +- .../move/crates/move-stdlib/tests/vector_tests.move | 5 +++++ 5 files changed, 13 insertions(+), 3 deletions(-) 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]