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

Add u256 conversions #5767

Merged
merged 15 commits into from
Apr 2, 2024
1 change: 1 addition & 0 deletions sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod primitive_conversions;
pub mod math;
pub mod flags;
pub mod u128;
pub mod u256;
pub mod alias;
pub mod hash;
pub mod asset_id;
Expand Down
145 changes: 145 additions & 0 deletions sway-lib-std/src/u256.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
library;

use ::convert::From;

/// Functions for casting between `u256` and other types.
impl From<u8> for u256 {
/// Casts a `u8` to a `u256`.
///
/// # Arguments
///
/// * `num`: [u8] - The `u8` to be casted.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the `u8` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u256_value = u256::from(255_u8);
/// }
/// ```
fn from(num: u8) -> Self {
num.as_u256()
}
}

impl From<u16> for u256 {
/// Casts a `u16` to a `u256`.
///
/// # Arguments
///
/// * `num`: [u16] - The `u16` to be casted.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the `u16` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u256_value = u256::from(65535_u16);
/// }
/// ```
fn from(num: u16) -> Self {
num.as_u256()
}
}

impl From<u32> for u256 {
/// Casts a `u32` to a `u256`.
///
/// # Arguments
///
/// * `num`: [u32] - The `u32` to be casted.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the `u32` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u256_value = u256::from(4294967295_u32);
/// }
/// ```
fn from(num: u32) -> Self {
num.as_u256()
}
}

impl From<u64> for u256 {
/// Casts a `u64` to a `u256`.
///
/// # Arguments
///
/// * `num`: [u64] - The `u64` to be casted.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the `u64` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u256_value = u256::from(18446744073709551615_u64);
/// }
/// ```
fn from(num: u64) -> Self {
num.as_u256()
}
}

impl From<b256> for u256 {
/// Casts raw `b256` data to a `u256`.
///
/// # Arguments
///
/// * `bits`: [b256] - The raw `b256` data to be casted.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the raw `b256`.
///
/// # Examples
///
/// ```sway
/// use std::constants::ZERO_B256;
///
/// fn foo() {
/// let u256_value = u256::from(ZERO_B256);
/// }
/// ```
fn from(bits: b256) -> Self {
bits.as_u256()
}
}

impl From<u256> for b256 {
/// Casts a `u256` to raw `b256` data.
///
/// # Returns
///
/// * [b256] - The underlying raw `b256` data of the `u256`.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256);
/// }
/// ```
fn from(num: u256) -> Self {
num.as_b256()
}
}
Loading