From 2edf99a928b57a21ed956bec60ba132ada78a405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ron=C4=8Devi=C4=87?= Date: Sat, 11 Jan 2025 00:27:01 +0100 Subject: [PATCH] Smaller improvements in wording and naming --- docs/book/src/common-collections/storage_vec.md | 2 +- docs/book/src/common-collections/vec.md | 4 ++-- docs/book/src/reference/undefined_behavior.md | 2 +- sway-lib-std/src/storage/storage_vec.sw | 2 +- sway-lib-std/src/vec.sw | 13 ++++++------- .../storage_vec_iter_tests/src/main.sw | 14 +++++++------- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/docs/book/src/common-collections/storage_vec.md b/docs/book/src/common-collections/storage_vec.md index 65112a7fd2d..5e81b628fa8 100644 --- a/docs/book/src/common-collections/storage_vec.md +++ b/docs/book/src/common-collections/storage_vec.md @@ -53,7 +53,7 @@ When the `get` method is passed an index that is outside the vector, it returns ## Iterating over the Values in a Vector -Iterating over a storage vector is conceptually the same as [iterating over a `Vec`](./vec.md). The only difference is an additional call to `read()` to actually read the stored value. +Iterating over a storage vector is conceptually the same as [iterating over a `Vec`](./vec.md). The only difference is an additional call to `read()` to actually read the stored value. ```sway {{#include ../../../../examples/storage_vec/src/main.sw:storage_vec_iterate}} diff --git a/docs/book/src/common-collections/vec.md b/docs/book/src/common-collections/vec.md index 26d9f430048..b68020150c1 100644 --- a/docs/book/src/common-collections/vec.md +++ b/docs/book/src/common-collections/vec.md @@ -38,7 +38,7 @@ When the `get` method is passed an index that is outside the vector, it returns ## Iterating over the Values in a Vector -To access elements of a vector, we can iterate through of the valid indices using a `while` loop and the `len` method as shown below: +To access elements of a vector, we can iterate through the valid indices using a `while` loop and the `len` method as shown below: ```sway {{#include ../../../../examples/vec/src/main.sw:vec_iterate_while}} @@ -46,7 +46,7 @@ To access elements of a vector, we can iterate through of the valid indices usin Note two details here. First, we use the method `len` which returns the length of the vector. Second, we call the method `unwrap` to extract the `Option` returned by `get`. We know that `unwrap` will not fail (i.e. will not cause a revert) because each index `i` passed to `get` is known to be smaller than the length of the vector. -The idiomatic and convenient way to access each element in a vector in turn, is to use the `for` loop in the combination with the `Vec::iter()` method. `Vec::iter()` method returns an iterator that iterates over all the elements of the vector sequentially. +The idiomatic and convenient way to access each element in a vector in turn, is to use the `for` loop in the combination with the `iter` method. The `iter` method returns an iterator that iterates over all the elements of the vector sequentially. ```sway {{#include ../../../../examples/vec/src/main.sw:vec_iterate_for}} diff --git a/docs/book/src/reference/undefined_behavior.md b/docs/book/src/reference/undefined_behavior.md index 22ed148a7b5..b6e5a94e8d9 100644 --- a/docs/book/src/reference/undefined_behavior.md +++ b/docs/book/src/reference/undefined_behavior.md @@ -8,7 +8,7 @@ This is not an exhaustive list, it may grow or shrink, there is no formal model of Sway's semantics so there may be more behavior considered undefined. We reserve the right to make some of the listed behavior defined in the future. -* Invalid arithmetic operations (overflows, underflows, division by zero, etc). +* Invalid arithmetic operations (overflows, underflows, division by zero, etc.). * Misuse of compiler intrinsics. * Incorrect use of inline assembly. * Reading and writing `raw_ptr` and `raw_slice`. diff --git a/sway-lib-std/src/storage/storage_vec.sw b/sway-lib-std/src/storage/storage_vec.sw index 298b7a95b78..cd5c571b187 100644 --- a/sway-lib-std/src/storage/storage_vec.sw +++ b/sway-lib-std/src/storage/storage_vec.sw @@ -968,7 +968,7 @@ impl StorageKey> { /// /// for elem in storage.vec.iter() { /// let elem_value = elem.read(); - /// /* ... */ + /// log(elem_value); /// } /// } /// ``` diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 61c3c64f5e4..d36bec92f5f 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -623,7 +623,7 @@ impl Vec { /// assert_eq(15, iter.next().unwrap()); /// /// for elem in vec.iter() { - /// /* ... */ + /// log(elem); /// } /// } /// @@ -641,7 +641,6 @@ impl Vec { /// /// for elem in vec.iter() { /// vec.push(20); // Modification causes undefined behavior. - /// /* ... */ /// } /// } /// ``` @@ -672,9 +671,9 @@ impl Vec { // below). // // Once we fix and formalize the copying of heap types - // this implementation will not work anymore, but for + // this implementation will be changed, but for // the time being, it is the most pragmatic one we can - // have. + // have now. VecIter { values: self, index: 0, @@ -773,7 +772,7 @@ impl Iterator for VecIter { // BEWARE: `self.values` keeps **the copy** of the `Vec` // we iterate over. The below check checks against // the length of that copy, taken when the iterator - // was created, and not the actual vector. + // was created, and not the original vector. // // If the original vector gets modified during the iteration // (e.g., elements are removed), this modification will not @@ -781,8 +780,8 @@ impl Iterator for VecIter { // // But since modifying the vector during iteration is // considered undefined behavior, this implementation, - // that always checks against the original vector is - // perfectly valid. + // that always checks against the length at the time + // the iterator got created is perfectly valid. if self.index >= self.values.len() { return None } diff --git a/test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/main.sw b/test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/main.sw index dc7ca0a9eb3..bdee7f12ace 100644 --- a/test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/main.sw @@ -7,7 +7,7 @@ use impls::*; use std::hash::{Hash, sha256}; use std::storage::storage_vec::*; -abi MyContract { +abi StorageVecIterTest { #[storage(read)] fn assert_empty_vec_next_returns_none(); @@ -98,7 +98,7 @@ fn assert_vec_with_elements_for_loop_iteration_impl(slot_id_preimage: u64) // assert_eq(vec.len(), i); } -impl MyContract for Contract { +impl StorageVecIterTest for Contract { #[storage(read)] fn assert_empty_vec_next_returns_none() { assert_empty_vec_next_returns_none_impl::<()>(1); @@ -232,30 +232,30 @@ impl MyContract for Contract { #[test] fn empty_vec_next_returns_none() { - let contract_abi = abi(MyContract, CONTRACT_ID); + let contract_abi = abi(StorageVecIterTest, CONTRACT_ID); contract_abi.assert_empty_vec_next_returns_none(); } #[test] fn vec_with_elements_next_returns_element() { - let contract_abi = abi(MyContract, CONTRACT_ID); + let contract_abi = abi(StorageVecIterTest, CONTRACT_ID); contract_abi.assert_vec_with_elements_next_returns_element(); } #[test] fn vec_with_elements_for_loop_iteration() { - let contract_abi = abi(MyContract, CONTRACT_ID); + let contract_abi = abi(StorageVecIterTest, CONTRACT_ID); contract_abi.assert_vec_with_elements_for_loop_iteration(); } #[test] fn storage_vec_field_for_loop_iteration() { - let contract_abi = abi(MyContract, CONTRACT_ID); + let contract_abi = abi(StorageVecIterTest, CONTRACT_ID); contract_abi.storage_vec_field_for_loop_iteration(); } #[test] fn storage_vec_field_nested_for_loop_iteration() { - let contract_abi = abi(MyContract, CONTRACT_ID); + let contract_abi = abi(StorageVecIterTest, CONTRACT_ID); contract_abi.storage_vec_field_nested_for_loop_iteration(); }