Skip to content
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

impl trait for "const generics" #6946

Merged
merged 15 commits into from
Mar 7, 2025
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Feb 20, 2025

Description

This PR is part of #6860.
It implements "impl traits" for "const generics", which means syntax such as:

trait A {
    fn my_len(self) -> u64;
}

impl<T, const N: u64> A for [T; N] {
    fn my_len(self) -> u64 {
        N
    }
}

This also opens the space for simplifying our current implementation of AbiEncode for arrays. Today we implement for each size, which limits us to a specific size. With this one can have just one "impl item", and support arrays of arbitrary size.

Both implementations will coexist in codec.sw until we stabilize "const generics".

#[cfg(experimental_const_generics = true)]
impl<T, const N: u64> AbiEncode for [T; N] 
where
    T: AbiEncode,
{
    fn abi_encode(self, buffer: Buffer) -> Buffer {
        ....
    }
}

#[cfg(experimental_const_generics = false)]
impl<T> AbiEncode for [T; 0]
where
    T: AbiEncode,
{
    fn abi_encode(self, buffer: Buffer) -> Buffer {
        ...
    }
}

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 self-assigned this Feb 20, 2025
@xunilrj xunilrj requested review from a team as code owners February 20, 2025 21:56
Copy link

codspeed-hq bot commented Feb 20, 2025

CodSpeed Performance Report

Merging #6946 will not alter performance

Comparing xunilrj/impl-trait-const-generic (ba56ca8) with master (8a3d8df)

Summary

✅ 22 untouched benchmarks

@xunilrj xunilrj force-pushed the xunilrj/impl-trait-const-generic branch from 6e66cf7 to 76079ed Compare March 3, 2025 13:49
@xunilrj xunilrj temporarily deployed to fuel-sway-bot March 3, 2025 14:09 — with GitHub Actions Inactive
@xunilrj xunilrj temporarily deployed to fuel-sway-bot March 3, 2025 14:52 — with GitHub Actions Inactive
@xunilrj xunilrj requested a review from IGI-111 March 3, 2025 15:17
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.

A few minor issues and corrections, but overall it looks very good.

@xunilrj xunilrj force-pushed the xunilrj/impl-trait-const-generic branch from b1ff9f0 to 266d427 Compare March 4, 2025 16:16
jjcnn
jjcnn previously approved these changes Mar 4, 2025
@xunilrj xunilrj temporarily deployed to fuel-sway-bot March 4, 2025 16:37 — with GitHub Actions Inactive
@xunilrj xunilrj requested a review from jjcnn March 4, 2025 17:01
jjcnn
jjcnn previously approved these changes Mar 6, 2025
@xunilrj xunilrj enabled auto-merge (squash) March 6, 2025 15:00
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

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

One nit but otherwise LGTM

@xunilrj xunilrj force-pushed the xunilrj/impl-trait-const-generic branch from 12e03b7 to ba56ca8 Compare March 6, 2025 17:24
@xunilrj xunilrj temporarily deployed to fuel-sway-bot March 6, 2025 17:24 — with GitHub Actions Inactive
@xunilrj xunilrj requested review from IGI-111 and jjcnn March 6, 2025 17:43
@xunilrj xunilrj merged commit 13542d2 into master Mar 7, 2025
42 checks passed
@xunilrj xunilrj deleted the xunilrj/impl-trait-const-generic branch March 7, 2025 00:32
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.

4 participants