From b7588c796ef98a13e9fdd99825a988773919bec1 Mon Sep 17 00:00:00 2001 From: segfault_magnet Date: Wed, 20 Sep 2023 15:44:51 +0200 Subject: [PATCH] fix docs --- docs/src/types/custom_types.md | 9 +++--- .../types/contracts/generics/src/main.sw | 30 ++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docs/src/types/custom_types.md b/docs/src/types/custom_types.md index 5f7572ca0c..e681661e2a 100644 --- a/docs/src/types/custom_types.md +++ b/docs/src/types/custom_types.md @@ -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 { field: u64 @@ -73,9 +75,8 @@ enum SomeEnum { ``` -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 { pub field: u64, @@ -90,13 +91,13 @@ enum SomeEnum { ``` 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}} ``` diff --git a/packages/fuels/tests/types/contracts/generics/src/main.sw b/packages/fuels/tests/types/contracts/generics/src/main.sw index ae17b166f5..90abfe0be7 100644 --- a/packages/fuels/tests/types/contracts/generics/src/main.sw +++ b/packages/fuels/tests/types/contracts/generics/src/main.sw @@ -72,18 +72,28 @@ abi MyContract { } impl MyContract for Contract { - fn unused_generic_args(_arg_1: StructOneUnusedGenericParam, _arg_2: EnumOneUnusedGenericParam){} - fn two_unused_generic_args(_arg_1: StructTwoUnusedGenericParams, _arg_2: EnumTwoUnusedGenericParams){} - fn used_and_unused_generic_args(arg_1: StructUsedAndUnusedGenericParams, arg_2: EnumUsedAndUnusedGenericParams) -> (StructUsedAndUnusedGenericParams, EnumUsedAndUnusedGenericParams){ + fn unused_generic_args( + _arg_1: StructOneUnusedGenericParam, + _arg_2: EnumOneUnusedGenericParam, + ) {} + fn two_unused_generic_args( + _arg_1: StructTwoUnusedGenericParams, + _arg_2: EnumTwoUnusedGenericParams, + ) {} + fn used_and_unused_generic_args( + arg_1: StructUsedAndUnusedGenericParams, + arg_2: EnumUsedAndUnusedGenericParams, + ) -> (StructUsedAndUnusedGenericParams, EnumUsedAndUnusedGenericParams) { 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) -> SimpleGeneric { let expected = SimpleGeneric {