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

Introduce the Zero trait #5973

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

Introduce the Zero trait #5973

wants to merge 16 commits into from

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented May 8, 2024

Description

Introduces the Zero trait with the following functions to be implemented:

  • zero()
  • is_zero()

This falls inline with what we see in Rust's Zero Trait, making the language feel and look more like Rust.

This PR also deprecates the following:

  • ZERO_B256 -> b256::zero()
  • ZERO_U256 -> u256::zero()

To define a u256 or b256 zero address, the following should be used:

let zero_b256 = b256::zero();
let zero_u256 = u256::zero();

A common use of the ZERO_B256 was to define the zero addresses and contract id`:

let zero_address = Address::from(ZERO_B256);
let zero_contract = ContractId::from(ZERO_B25);

The following is now possible:

let zero_address = Address::zero();
let zero_contract = ContractId::zero();

The following types now implement the Zero trait:

  • u8
  • u16
  • u32
  • u64
  • u256
  • b256
  • SubId
  • AssetId
  • ContractId
  • Address
  • EvmAddress
  • U128
  • B512

Closes #5830

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.

@bitzoic bitzoic added enhancement New feature or request lib: std Standard library labels May 8, 2024
@bitzoic bitzoic self-assigned this May 8, 2024
@bitzoic bitzoic linked an issue May 8, 2024 that may be closed by this pull request
@IGI-111
Copy link
Contributor

IGI-111 commented May 8, 2024

I know Rust's num has it, but is it common enough to want to condition things over the ability to produce a identity element? These could just be part of their respective impls and the trait be a library.

As in Rust, incidentally.

@bitzoic bitzoic requested a review from a team May 10, 2024 03:58
@bitzoic bitzoic marked this pull request as ready for review May 10, 2024 03:58
K1-R1
K1-R1 previously approved these changes May 13, 2024
docs/book/src/blockchain-development/native_assets.md Outdated Show resolved Hide resolved
@K1-R1 K1-R1 requested a review from a team May 13, 2024 17:33
bitzoic and others added 2 commits May 14, 2024 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce the Zero trait
3 participants