Skip to content

Commit

Permalink
Smaller improvements in wording and naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcev committed Jan 10, 2025
1 parent 2c306b4 commit 2edf99a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/book/src/common-collections/storage_vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`](./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<T>`](./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}}
Expand Down
4 changes: 2 additions & 2 deletions docs/book/src/common-collections/vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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}}
```

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}}
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/reference/undefined_behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/storage/storage_vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ impl<V> StorageKey<StorageVec<V>> {
///
/// for elem in storage.vec.iter() {
/// let elem_value = elem.read();
/// /* ... */
/// log(elem_value);
/// }
/// }
/// ```
Expand Down
13 changes: 6 additions & 7 deletions sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl<T> Vec<T> {
/// assert_eq(15, iter.next().unwrap());
///
/// for elem in vec.iter() {
/// /* ... */
/// log(elem);
/// }
/// }
///
Expand All @@ -641,7 +641,6 @@ impl<T> Vec<T> {
///
/// for elem in vec.iter() {
/// vec.push(20); // Modification causes undefined behavior.
/// /* ... */
/// }
/// }
/// ```
Expand Down Expand Up @@ -672,9 +671,9 @@ impl<T> Vec<T> {
// 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,
Expand Down Expand Up @@ -773,16 +772,16 @@ impl<T> Iterator for VecIter<T> {
// 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
// be reflected in `self.values.len()`.
//
// 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -98,7 +98,7 @@ fn assert_vec_with_elements_for_loop_iteration_impl<T>(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);
Expand Down Expand Up @@ -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();
}

0 comments on commit 2edf99a

Please sign in to comment.