Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Sep 20, 2023
1 parent d5cf8d6 commit b7588c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
9 changes: 5 additions & 4 deletions docs/src/types/custom_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Your Rust code would look like this:
```

### Unused generic type parameters

Sway supports unused generic type parameters when declaring structs/enums:

```Rust
struct SomeStruct<T, K> {
field: u64
Expand All @@ -73,9 +75,8 @@ enum SomeEnum<T, K> {

```

If you tried the same in Rust you'd get complaints that `T` and `K` must be used or removed. When generating Rust bindings for such types we make use of the [`PhantomData`](https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-type-parameters) type.
If you tried the same in Rust you'd get complaints that `T` and `K` must be used or removed. When generating Rust bindings for such types we make use of the [`PhantomData`](https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-type-parameters) type. The generated bindings for the above example would look something like this:

The generated bindings for the above example would look something like this:
```Rust
struct SomeStruct<T, K> {
pub field: u64,
Expand All @@ -90,13 +91,13 @@ enum SomeEnum<T, K> {
```

To lessen the impact to DEX you may use `SomeStruct::new` to initialize the above structure without bothering with the `PhantomData`s:

```rust,ignore
{{#include ../../../examples/types/src/lib.rs:unused_generics_struct}}
```

If your struct doesn't have any fields we'll also derive `Default`.
If your struct doesn't have any fields we'll also derive `Default`. As for enums all `PhantomData`s are placed inside a new variant called `IgnoreMe` which you'll need to ignore in your matches:

As for enums all `PhantomData`s are placed inside a new variant called `IgnoreMe` which you'll need to ignore in your matches:
```rust,ignore
{{#include ../../../examples/types/src/lib.rs:unused_generics_enum}}
```
30 changes: 20 additions & 10 deletions packages/fuels/tests/types/contracts/generics/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,28 @@ abi MyContract {
}

impl MyContract for Contract {
fn unused_generic_args(_arg_1: StructOneUnusedGenericParam<u64>, _arg_2: EnumOneUnusedGenericParam<u32>){}
fn two_unused_generic_args(_arg_1: StructTwoUnusedGenericParams<u32, u64>, _arg_2: EnumTwoUnusedGenericParams<u64, u32>){}
fn used_and_unused_generic_args(arg_1: StructUsedAndUnusedGenericParams<u32, u8, u64>, arg_2: EnumUsedAndUnusedGenericParams<u64, u8, u32>) -> (StructUsedAndUnusedGenericParams<u64, u8, u32>, EnumUsedAndUnusedGenericParams<u32, u8, u64>){
fn unused_generic_args(
_arg_1: StructOneUnusedGenericParam<u64>,
_arg_2: EnumOneUnusedGenericParam<u32>,
) {}
fn two_unused_generic_args(
_arg_1: StructTwoUnusedGenericParams<u32, u64>,
_arg_2: EnumTwoUnusedGenericParams<u64, u32>,
) {}
fn used_and_unused_generic_args(
arg_1: StructUsedAndUnusedGenericParams<u32, u8, u64>,
arg_2: EnumUsedAndUnusedGenericParams<u64, u8, u32>,
) -> (StructUsedAndUnusedGenericParams<u64, u8, u32>, EnumUsedAndUnusedGenericParams<u32, u8, u64>) {
assert_eq(arg_1.field, 10u8);
if let EnumUsedAndUnusedGenericParams::Two(val) = arg_2 {
assert_eq(val, 11u8);

} else {
require(false, "Expected the variant EnumUsedAndUnusedGenericParams::Two");
}

(StructUsedAndUnusedGenericParams { field: 12u8 }, EnumUsedAndUnusedGenericParams::Two(13u8))
assert_eq(val, 11u8);
} else {
require(false, "Expected the variant EnumUsedAndUnusedGenericParams::Two");
}
(
StructUsedAndUnusedGenericParams { field: 12u8 },
EnumUsedAndUnusedGenericParams::Two(13u8),
)
}
fn struct_w_generic(arg1: SimpleGeneric<u64>) -> SimpleGeneric<u64> {
let expected = SimpleGeneric {
Expand Down

0 comments on commit b7588c7

Please sign in to comment.