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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

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 (076e2a6) with master (b07d0ca)

Summary

✅ 22 untouched benchmarks

@xunilrj xunilrj deployed to fuel-sway-bot February 21, 2025 21:51 — with GitHub Actions Active
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.

1 participant