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

feat: Implementation of the SizeOf<T> trait #4245

Closed
1 of 2 tasks
akhercha opened this issue Oct 11, 2023 · 1 comment · Fixed by #4251
Closed
1 of 2 tasks

feat: Implementation of the SizeOf<T> trait #4245

akhercha opened this issue Oct 11, 2023 · 1 comment · Fixed by #4251
Labels
enhancement New feature or request

Comments

@akhercha
Copy link
Contributor

akhercha commented Oct 11, 2023

Feature Request

Describe the Feature Request

Introduction of a SizeOf<T> trait designed to retrieve the size in bits of various data types.

Describe Preferred Solution

trait SizeOf<T> {
    /// Returns the size in bits of T. 
    fn size() -> T;
    /// Returns the size in bits of Self.
    fn size_of(self: @T) -> T;
}

impl U8SizeOf of SizeOf<u8> {
    fn size() -> u8 {
        8
    }
    fn size_of(self: @u8) -> u8 {
        U8SizeOf::size()
    }
}

After implemented for u8 for example, it would be then:

assert(10_u8.size_of() == 8, 'should be 8');
// or
assert(U8SizeOf::size() == 8, 'should be 8');

Related Code

Implemented in Kakarot::utils::num for PR #410

The usage was:

fn shl(self: T, shift: T) -> T {
        if shift > shift.size_of() - One::one() {
            panic_with_felt252('mul Overflow');
        }
        (...)
    }

Additional Context

If the feature request is approved, would you be willing to submit a PR?

  • Yes
  • No
@akhercha akhercha added the enhancement New feature or request label Oct 11, 2023
@orizi
Copy link
Collaborator

orizi commented Oct 12, 2023

this is not "size" - this might be "representation bits" - "size" implies the size in memory.
but asside from that sounds fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants