Skip to content

Commit

Permalink
docs: add type conversions page (#5708)
Browse files Browse the repository at this point in the history
This PR adds a Type Conversions page to make it easier for developers to
quickly find a solution to converting a type.
  • Loading branch information
sarahschwartz authored Mar 11, 2024
1 parent dc20ac2 commit 1589c8f
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Built-in Types](./basics/built_in_types.md)
- [Commonly Used Library Types](./basics/commonly_used_library_types.md)
- [Blockchain Types](./basics/blockchain_types.md)
- [Converting Types](./basics/converting_types.md)
- [Functions](./basics/functions.md)
- [Structs, Tuples, and Enums](./basics/structs_tuples_and_enums.md)
- [Methods and Associated Functions](./basics/methods_and_associated_functions.md)
Expand Down
142 changes: 142 additions & 0 deletions docs/book/src/basics/converting_types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Converting Types

Below are some common type conversions in Sway:

- [Identity Conversions](#identity-conversions)
- [String Conversions](#string-conversions)
- [Number Conversions](#number-conversions)
- [Byte Array Conversions](#byte-array-conversions)

## Identity Conversions

### Convert to `Identity`

```sway
{{#include ../../../../examples/converting_types/src/identity.sw:convert_to_identity}}
```

### Convert `Identity` to `ContractId` or `Address`

```sway
{{#include ../../../../examples/converting_types/src/identity.sw:convert_from_identity}}
```

### Convert `ContractId` or `Address` to `b256`

```sway
{{#include ../../../../examples/converting_types/src/identity.sw:convert_to_b256}}
```

### Convert `b256` to `ContractId` or `Address`

```sway
{{#include ../../../../examples/converting_types/src/identity.sw:convert_b256_to_address_or_contract_id}}
```

## String Conversions

### Convert `str` to `str[]`

```sway
{{#include ../../../../examples/converting_types/src/strings.sw:strings_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/strings.sw:str_to_str_array}}
```

### Convert `str[]` to `str`

```sway
{{#include ../../../../examples/converting_types/src/strings.sw:str_array_to_str}}
```

## Number Conversions

### Convert to `u256`

```sway
{{#include ../../../../examples/converting_types/src/to_u256.sw:to_u256}}
```

### Convert to `u64`

```sway
{{#include ../../../../examples/converting_types/src/to_u64.sw:to_u64_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/to_u64.sw:to_u64}}
```

### Convert to `u32`

```sway
{{#include ../../../../examples/converting_types/src/to_u32.sw:to_u32_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/to_u32.sw:to_u32}}
```

### Convert to `u16`

```sway
{{#include ../../../../examples/converting_types/src/to_u16.sw:to_u16_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/to_u16.sw:to_u16}}
```

### Convert to `u8`

```sway
{{#include ../../../../examples/converting_types/src/to_u8.sw:to_u8_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/to_u8.sw:to_u8}}
```

### Convert to `Bytes`

```sway
{{#include ../../../../examples/converting_types/src/bytes.sw:to_bytes_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/bytes.sw:to_bytes}}
```

### Convert from `Bytes`

```sway
{{#include ../../../../examples/converting_types/src/bytes.sw:to_bytes_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/bytes.sw:from_bytes}}
```

## Byte Array Conversions

### Convert to a Byte Array

```sway
{{#include ../../../../examples/converting_types/src/byte_arrays.sw:to_byte_array_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/byte_arrays.sw:to_byte_array}}
```

### Convert from a Byte Array

```sway
{{#include ../../../../examples/converting_types/src/byte_arrays.sw:to_byte_array_import}}
```

```sway
{{#include ../../../../examples/converting_types/src/byte_arrays.sw:from_byte_array}}
```
70 changes: 42 additions & 28 deletions examples/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,44 @@ source = "member"
[[package]]
name = "array"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "asm_return_tuple_pointer"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "break_and_continue"
source = "member"
dependencies = ["core"]
dependencies = ["core path+from-root-087D5D776F294F03"]

[[package]]
name = "cei_analysis"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "configurable_constants"
source = "member"

[[package]]
name = "converting_types"
source = "member"
dependencies = ["std git+https://github.com/fuellabs/sway?tag=v0.51.1#d1e8f019c1da46273c3d3a72b385ce356ba2bc20"]

[[package]]
name = "core"
source = "path+from-root-087D5D776F294F03"

[[package]]
name = "core"
source = "path+from-root-4F1FAAE4510FEB23"

[[package]]
name = "counter"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "enums"
Expand All @@ -42,92 +51,97 @@ source = "member"
[[package]]
name = "fizzbuzz"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "hashing"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "identity"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "liquidity_pool"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "match_expressions"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "methods_and_associated_functions"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "msg_sender"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "mut_ref_params"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "native_asset"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "option"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "ownership"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "result"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "signatures"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.51.1#d1e8f019c1da46273c3d3a72b385ce356ba2bc20"
dependencies = ["core path+from-root-4F1FAAE4510FEB23"]

[[package]]
name = "std"
source = "path+from-root-087D5D776F294F03"
dependencies = ["core"]
dependencies = ["core path+from-root-087D5D776F294F03"]

[[package]]
name = "storage_example"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "storage_map"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "storage_variables"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "storage_vec"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "structs"
Expand All @@ -140,30 +154,30 @@ source = "member"
[[package]]
name = "type_aliases"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "vec"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "wallet_abi"
source = "member"
dependencies = ["std"]
dependencies = ["std path+from-root-087D5D776F294F03"]

[[package]]
name = "wallet_contract_caller_script"
source = "member"
dependencies = [
"std",
"std path+from-root-087D5D776F294F03",
"wallet_abi",
]

[[package]]
name = "wallet_smart_contract"
source = "member"
dependencies = [
"std",
"std path+from-root-087D5D776F294F03",
"wallet_abi",
]
1 change: 1 addition & 0 deletions examples/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"break_and_continue",
"cei_analysis",
"configurable_constants",
"converting_types",
"counter",
"enums",
"fizzbuzz",
Expand Down
2 changes: 2 additions & 0 deletions examples/converting_types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions examples/converting_types/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "converting_types"

[dependencies]
Loading

0 comments on commit 1589c8f

Please sign in to comment.