-
Notifications
You must be signed in to change notification settings - Fork 168
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(sol!
): function calls should directly yield result
#855
base: main
Are you sure you want to change the base?
Conversation
sol!
): functions should directly yield result sol!
): function should directly yield result
sol!
): function should directly yield result sol!
): function calls should directly yield result
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.
ptal @DaniPopes
_ => { | ||
let return_tys = expand_types(returns, cx); | ||
let tuple_ret = generate_return_tuple(returns); |
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.
how did we handle tuples previously? as this also _0: (..,..,..)
?
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.
sol! {
function simple() returns (uint256, uint64);
}
Previously, we handled it by creating a field for each return param in the return struct. For above, it would be
struct simpleReturn {
_0: U256,
_1: u64
}
Now we return (simpleReturn._0, simpleReturn._1)
for simple.call()
instead of the struct.
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.
I'm in favor of this change, however we're basically throwing out the API of the return struct and so we lose the names of the return variables if present.
How about we generate the tuple return if ALL return parameters are unnamed, otherwise generate and use the return struct?
We also need to document this on the macro doccoments, and I would like to see a PR with a patch on the examples repo to see this in action before merging this.
Edit: logic of the question.
Examples patch: alloy-rs/examples#174 |
I started with this initially but I feel the devX gets cumbersome as we still retain the E.g. sol! {
function getPartialNamedTuple() external view returns (uint256 , uint256 b, uint256 );
}
// Gives the following return struct:
struct getPartialNamedTupleReturn {
_0: U256,
b: U256,
_2: U256
} I'm fine with losing names if we have behavior where return types are easily pattern-matched. i.e If a solidity func returns multiple parameters akin to a tuple, then the rust binding shouldn't return a struct with the return params as fields,it should return the tuple directly. Having said that, open to other ideas. wdyt? @DaniPopes @mattsse @gakonst We can add extensive docs to explain the above You can find an example with more complex return types here (compared to previous |
this has benefits when you're only interested in one field or always want to decompose the tuple right away. imo this has significant downsides when you want to do some processing on the return object fn handle_return(x: Return {a,b,c}) vs fn handle_return(x: (U256, String, U256)) without the type and field names you lose the entire context right away. |
False positive in clippy rust-lang/rust-clippy#14020 |
Motivation
Currently, when working with named or unnamed return parameters like the following
the user has to do
namedReturn.a
orunnamedReturn._0
to access the actual result i.e calling the function does not directly yield the result. This is cumbersome and becomes even more so when working with structs, or multiple return values etc.Solution
type Return
in theimpl SolCall
of the call type._0
) inabi_decode_returns
to return directlyConsider the following
The bindings generated by the above have changed like so:
This not only works for unamed singular return types but also named ones and tuple/compound return types. E.g
PR Checklist