Skip to content

Commit 2edf99a

Browse files
committed
Smaller improvements in wording and naming
1 parent 2c306b4 commit 2edf99a

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

docs/book/src/common-collections/storage_vec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When the `get` method is passed an index that is outside the vector, it returns
5353

5454
## Iterating over the Values in a Vector
5555

56-
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.
56+
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.
5757

5858
```sway
5959
{{#include ../../../../examples/storage_vec/src/main.sw:storage_vec_iterate}}

docs/book/src/common-collections/vec.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ When the `get` method is passed an index that is outside the vector, it returns
3838

3939
## Iterating over the Values in a Vector
4040

41-
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:
41+
To access elements of a vector, we can iterate through the valid indices using a `while` loop and the `len` method as shown below:
4242

4343
```sway
4444
{{#include ../../../../examples/vec/src/main.sw:vec_iterate_while}}
4545
```
4646

4747
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.
4848

49-
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.
49+
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.
5050

5151
```sway
5252
{{#include ../../../../examples/vec/src/main.sw:vec_iterate_for}}

docs/book/src/reference/undefined_behavior.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is not an exhaustive list, it may grow or shrink, there is no formal model
88
of Sway's semantics so there may be more behavior considered undefined. We
99
reserve the right to make some of the listed behavior defined in the future.
1010

11-
* Invalid arithmetic operations (overflows, underflows, division by zero, etc).
11+
* Invalid arithmetic operations (overflows, underflows, division by zero, etc.).
1212
* Misuse of compiler intrinsics.
1313
* Incorrect use of inline assembly.
1414
* Reading and writing `raw_ptr` and `raw_slice`.

sway-lib-std/src/storage/storage_vec.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl<V> StorageKey<StorageVec<V>> {
968968
///
969969
/// for elem in storage.vec.iter() {
970970
/// let elem_value = elem.read();
971-
/// /* ... */
971+
/// log(elem_value);
972972
/// }
973973
/// }
974974
/// ```

sway-lib-std/src/vec.sw

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<T> Vec<T> {
623623
/// assert_eq(15, iter.next().unwrap());
624624
///
625625
/// for elem in vec.iter() {
626-
/// /* ... */
626+
/// log(elem);
627627
/// }
628628
/// }
629629
///
@@ -641,7 +641,6 @@ impl<T> Vec<T> {
641641
///
642642
/// for elem in vec.iter() {
643643
/// vec.push(20); // Modification causes undefined behavior.
644-
/// /* ... */
645644
/// }
646645
/// }
647646
/// ```
@@ -672,9 +671,9 @@ impl<T> Vec<T> {
672671
// below).
673672
//
674673
// Once we fix and formalize the copying of heap types
675-
// this implementation will not work anymore, but for
674+
// this implementation will be changed, but for
676675
// the time being, it is the most pragmatic one we can
677-
// have.
676+
// have now.
678677
VecIter {
679678
values: self,
680679
index: 0,
@@ -773,16 +772,16 @@ impl<T> Iterator for VecIter<T> {
773772
// BEWARE: `self.values` keeps **the copy** of the `Vec`
774773
// we iterate over. The below check checks against
775774
// the length of that copy, taken when the iterator
776-
// was created, and not the actual vector.
775+
// was created, and not the original vector.
777776
//
778777
// If the original vector gets modified during the iteration
779778
// (e.g., elements are removed), this modification will not
780779
// be reflected in `self.values.len()`.
781780
//
782781
// But since modifying the vector during iteration is
783782
// considered undefined behavior, this implementation,
784-
// that always checks against the original vector is
785-
// perfectly valid.
783+
// that always checks against the length at the time
784+
// the iterator got created is perfectly valid.
786785
if self.index >= self.values.len() {
787786
return None
788787
}

test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/main.sw

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use impls::*;
77
use std::hash::{Hash, sha256};
88
use std::storage::storage_vec::*;
99

10-
abi MyContract {
10+
abi StorageVecIterTest {
1111
#[storage(read)]
1212
fn assert_empty_vec_next_returns_none();
1313

@@ -98,7 +98,7 @@ fn assert_vec_with_elements_for_loop_iteration_impl<T>(slot_id_preimage: u64)
9898
// assert_eq(vec.len(), i);
9999
}
100100

101-
impl MyContract for Contract {
101+
impl StorageVecIterTest for Contract {
102102
#[storage(read)]
103103
fn assert_empty_vec_next_returns_none() {
104104
assert_empty_vec_next_returns_none_impl::<()>(1);
@@ -232,30 +232,30 @@ impl MyContract for Contract {
232232

233233
#[test]
234234
fn empty_vec_next_returns_none() {
235-
let contract_abi = abi(MyContract, CONTRACT_ID);
235+
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
236236
contract_abi.assert_empty_vec_next_returns_none();
237237
}
238238

239239
#[test]
240240
fn vec_with_elements_next_returns_element() {
241-
let contract_abi = abi(MyContract, CONTRACT_ID);
241+
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
242242
contract_abi.assert_vec_with_elements_next_returns_element();
243243
}
244244

245245
#[test]
246246
fn vec_with_elements_for_loop_iteration() {
247-
let contract_abi = abi(MyContract, CONTRACT_ID);
247+
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
248248
contract_abi.assert_vec_with_elements_for_loop_iteration();
249249
}
250250

251251
#[test]
252252
fn storage_vec_field_for_loop_iteration() {
253-
let contract_abi = abi(MyContract, CONTRACT_ID);
253+
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
254254
contract_abi.storage_vec_field_for_loop_iteration();
255255
}
256256

257257
#[test]
258258
fn storage_vec_field_nested_for_loop_iteration() {
259-
let contract_abi = abi(MyContract, CONTRACT_ID);
259+
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
260260
contract_abi.storage_vec_field_nested_for_loop_iteration();
261261
}

0 commit comments

Comments
 (0)