-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: struct destructuring #2243
Changes from 22 commits
115efc3
bcec9bf
58dfcee
9ae75e5
cb889fb
483d18b
74188c1
e06e04b
df1d3d5
69aeccc
2af43ac
204a251
bd01ad5
60eaa9b
d715323
06292e8
9f91f64
8e159fd
22c76dd
4cad09a
60adadb
5d8b0c3
9d6b86a
0730cb6
32fcea7
5350b4c
f614df8
3904206
1a5b93e
11c5567
b48937a
802009d
b9a0e6a
7cd5ad6
9139e18
12b6f35
6575499
fd69348
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-F93ECA3248F311E3' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'struct_destructuring' | ||
source = 'root' | ||
dependencies = ['core'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "struct_destructuring" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"inputs": [], | ||
"name": "main", | ||
"outputs": [ | ||
{ | ||
"components": null, | ||
"name": "", | ||
"type": "u64", | ||
"typeArguments": null | ||
} | ||
], | ||
"type": "function" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
script; | ||
|
||
// Tests nested struct destructuring | ||
|
||
fn main() -> u64 { | ||
let point1 = Point { x: 0, y: 0 }; | ||
let point2 = Point { x: 1, y: 1 }; | ||
let line = Line { p1: point1, p2: point2 }; | ||
let Line { p1: Point { x: x0, y: y0 }, p2: Point { x: x1, y: y1} } = line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alright, don't hate me, but what about a nested tuple in a struct and vice versa? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I can add a test for that. Should I also include an example for the docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs example could be cool, to show people just how deep destructuring can go! |
||
x0 | ||
} | ||
|
||
struct Point { | ||
x: u64, | ||
y: u64, | ||
} | ||
|
||
struct Line { | ||
p1: Point, | ||
p2: Point, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 0 } | ||
validate_abi = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-F93ECA3248F311E3' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'struct_destructuring' | ||
source = 'root' | ||
dependencies = ['core'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "struct_destructuring" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"inputs": [], | ||
"name": "main", | ||
"outputs": [ | ||
{ | ||
"components": null, | ||
"name": "", | ||
"type": "u64", | ||
"typeArguments": null | ||
} | ||
], | ||
"type": "function" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
script; | ||
|
||
// Tests struct destructuring | ||
|
||
fn gimme_a_struct() -> Dummy { | ||
Dummy { value1: 1, value2: true } | ||
} | ||
|
||
fn main() -> u64 { | ||
let Dummy { value1, value2 } = gimme_a_struct(); | ||
let Dummy { value1, value2 }: Dummy = gimme_a_struct(); | ||
let data = Data { | ||
value: 42, | ||
}; | ||
let Data { value }: Data = data; | ||
return value; | ||
} | ||
|
||
struct Data { | ||
value: u64, | ||
} | ||
|
||
struct Dummy { | ||
value1: u64, | ||
value2: bool, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 42 } | ||
validate_abi = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as this isn't a contract, do we need to validate the abi? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I'm not sure, there are other test cases in the same folder such as tuple_access that are scripts and validate the abi. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I don't think it is necessary. Cc @otrho for consensus on that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Time for an RFC! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been validating the ABI for all passing tests including scripts because scripts emit a JSON ABI now. Not sure how important that is though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, all I did was refactor the test harness. This doesn't make me the authority on how or what to test. 😉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for these comments, great addition