Skip to content

Commit

Permalink
remove support for returning vectors for the time being
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Sep 29, 2022
1 parent 5225152 commit 8883493
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 662 deletions.
84 changes: 1 addition & 83 deletions docs/src/types/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,4 @@ You can use a vector just like you would use any other type -- e.g. a `[Vec<u32>

## Returning vectors

There is a mandatory extra step to returning vectors -- you need to `log` every element before returning the vector.

You must not mix any unrelated `log`s once you start logging vector elements.

These logs must be the last ones you make.


### A simple case:
```rust,ignore
{{#include ../../../packages/fuels/tests/test_projects/vectors/src/main.sw:sway_returning_a_vec}}
```

Calling it from the SDK would look like this:

```rust,ignore
{{#include ../../../packages/fuels/tests/harness.rs:harness_returning_a_vec}}
```

### Respect the order

If you have vectors embedded in some other type, you must take care to log them in order:

```rust,ignore
{{#include ../../../packages/fuels/tests/test_projects/vectors/src/data_structures.sw:sway_nested_vec_types}}
```

you must take care to log them in order:
```rust,ignore
{{#include ../../../packages/fuels/tests/test_projects/vectors/src/main.sw:sway_returning_type_w_nested_vectors}}
```

where `log_vec` is a helper defined as:
```rust,ignore
{{#include ../../../packages/fuels/tests/test_projects/vectors/src/utils.sw:sway_log_vec_helper}}
```


Calling it from the SDK would look like this:

```rust,ignore
{{#include ../../../packages/fuels/tests/harness.rs:harness_returning_type_w_nested_vectors}}
```


### Nested vectors

There is one more step you must take if you're logging a vector nested immediately inside another vector -- e.g. `Vec<Vec<u32>>`

An example:

```rust,ignore
{{#include ../../../packages/fuels/tests/test_projects/vectors/src/main.sw:sway_returning_immediately_nested_vectors}}
```
Calling it from the SDK would look like this:

```rust,ignore
{{#include ../../../packages/fuels/tests/harness.rs:harness_returning_immediately_nested_vectors}}
```


To log a `Vec<Vec<u32>>` we need to do the following:

1. We're looking at the parent vector. The type is `Vec<Vec<..>>`.

This is a vector with two elements `inner_vec_1` and `inner_vec_2`. Both are vectors themselves.

2. We're looking at `inner_vec_1`. The type is `Vec<u32>`.

This is a vector with one element: `1`. Log it.

3. We're looking at the second element of the parent vector `inner_vec_2`. The type is `Vec<u32>`.

This is a vector with one element: `2`. Log it.

We've finished logging the elements of the parent vector. Since the type is `Vec<Vec<..>>` we have an additional step.

4. Call `log` on each element of the vector:

`log(inner_vec_1)`

`log(inner_vec_2)`

In the example this is done by the `log_vec` helper.
This is currently not supported. If you try returning a type that is or contains a vector you will get a compile time error.
2 changes: 1 addition & 1 deletion packages/fuels-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl ContractCall {
receipts.remove(i);
}

let decoded_value = ABIDecoder::decode_single(param_type, &encoded_value, receipts)?;
let decoded_value = ABIDecoder::decode_single(param_type, &encoded_value)?;
Ok(decoded_value)
}
}
Expand Down
Loading

0 comments on commit 8883493

Please sign in to comment.