Skip to content

AbiDecode for arrays using const_generics #7008

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

Merged
merged 8 commits into from
Mar 20, 2025
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Mar 10, 2025

Description

This is part of #6860.

This completes abi encoding for arrays. Allowing arrays of any size as inputs and outputs in predicates/scripts/contracts.
This PR also allows arrays to be initialized with "const generics" such as:

#[cfg(experimental_const_generics = true)]
impl<T, const N: u64> AbiDecode for [T; N]
where
    T: AbiDecode,
{
    fn abi_decode(ref mut buffer: BufferReader) -> [T; N] {
        let first: T = buffer.decode::<T>();
        let mut array = [first; N];  // <-------------------- This can used now
        ...
        array
    }
}

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@xunilrj xunilrj force-pushed the xunilrj/abi-decode-for-arrays branch from 218afe8 to 0a62db8 Compare March 10, 2025 14:21
Copy link

codspeed-hq bot commented Mar 10, 2025

CodSpeed Performance Report

Merging #7008 will not alter performance

Comparing xunilrj/abi-decode-for-arrays (2b53ea5) with master (6cd732b)

Summary

✅ 22 untouched benchmarks

@xunilrj xunilrj self-assigned this Mar 10, 2025
@xunilrj xunilrj marked this pull request as ready for review March 10, 2025 18:57
@xunilrj xunilrj requested review from a team as code owners March 10, 2025 18:57
@xunilrj xunilrj mentioned this pull request Feb 13, 2025
19 tasks
zees-dev
zees-dev previously approved these changes Mar 11, 2025
Copy link
Contributor

@zees-dev zees-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very useful functionality; LGTM 👍

Copy link
Contributor

@jjcnn jjcnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to move the new abi_decode function to std::codec now that std and core have been merged, but otherwise it looks good.

Do we not need an implementation of AbiEncode as well?

sdankel
sdankel previously approved these changes Mar 14, 2025
@xunilrj xunilrj dismissed stale reviews from sdankel and zees-dev via 2b53ea5 March 19, 2025 18:00
@xunilrj xunilrj force-pushed the xunilrj/abi-decode-for-arrays branch from 08bf291 to 2b53ea5 Compare March 19, 2025 18:00
@xunilrj xunilrj requested review from jjcnn and sdankel March 20, 2025 11:25
@xunilrj xunilrj requested a review from zees-dev March 20, 2025 11:25
@tritao tritao merged commit fa96088 into master Mar 20, 2025
40 checks passed
@tritao tritao deleted the xunilrj/abi-decode-for-arrays branch March 20, 2025 14:47
zees-dev pushed a commit that referenced this pull request Apr 1, 2025
## Description

This is part of #6860.

This completes abi encoding for arrays. Allowing arrays of any size as
inputs and outputs in predicates/scripts/contracts.
This PR also allows arrays to be initialized with "const generics" such
as:

```sway
#[cfg(experimental_const_generics = true)]
impl<T, const N: u64> AbiDecode for [T; N]
where
    T: AbiDecode,
{
    fn abi_decode(ref mut buffer: BufferReader) -> [T; N] {
        let first: T = buffer.decode::<T>();
        let mut array = [first; N];  // <-------------------- This can used now
        ...
        array
    }
}
```` 


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants