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 missing docs to primitive_conversions library in standard library #5627

Merged
merged 18 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sway-lib-std/src/primitive_conversions/str.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ library;
use ::option::Option::{self, *};

impl str {
/// Attempts to convert the string slice into a string array.
///
/// # Returns
///
/// * [Option<S>] - `Some(str_array)` if the lengths of the `S` str_array type and the string slice's lengths match. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let string_slice = "abcd";
/// let string_array: str[4] = a.try_as_str_array().unwrap();
/// }
/// ```
pub fn try_as_str_array<S>(self) -> Option<S> {
__assert_is_str_array::<S>();
let str_size = __size_of_str_array::<S>();
Expand Down
22 changes: 22 additions & 0 deletions sway-lib-std/src/primitive_conversions/u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ use ::option::Option::{self, *};
use ::u128::U128;

impl u16 {
/// Attempts to convert the u16 value into a u8 value.
///
/// # Additional Information
///
/// The max value a u8 can represent is 255.
///
/// # Returns
///
/// [Option<u8>] - `Some(u8)` if the u16 is less than or equal to the max u8 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 255_u16.try_as_u8();
/// assert(val == Some(255_u8));
///
/// // Conversion fails as value is above the max a u8 can represent.
/// let val2 = 256_u16.try_as_u8();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u16() {
Some(asm(input: self) {
Expand Down
44 changes: 44 additions & 0 deletions sway-lib-std/src/primitive_conversions/u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ use ::option::Option::{self, *};
use ::u128::U128;

impl u32 {
/// Attempts to convert the u32 value into a u8 value.
///
/// # Additional Information
///
/// The max value a u8 can represent is 255.
///
/// # Returns
///
/// [Option<u8>] - `Some(u8)` if the u32 is less than or equal to the max u8 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 255_u32.try_as_u8();
/// assert(val == Some(255_u8));
///
/// // Conversion fails as value is above the max a u8 can represent.
/// let val2 = 256_u32.try_as_u8();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u32() {
Some(asm(input: self) {
Expand All @@ -15,6 +37,28 @@ impl u32 {
}
}

/// Attempts to convert the u32 value into a u16 value.
///
/// # Additional Information
///
/// The max value a u16 can represent is 65_535.
///
/// # Returns
///
/// [Option<u16>] - `Some(u16)` if the u32 is less than or equal to the max u16 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 65_535_u32.try_as_u16();
/// assert(val == Some(65_535_u16));
///
/// // Conversion fails as value is above the max a u16 can represent.
/// let val2 = 65_536_u32.try_as_u16();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u16(self) -> Option<u16> {
if self <= u16::max().as_u32() {
Some(asm(input: self) {
Expand Down
66 changes: 66 additions & 0 deletions sway-lib-std/src/primitive_conversions/u64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ use ::option::Option::{self, *};
use ::u128::U128;

impl u64 {
/// Attempts to convert the u64 value into a u8 value.
///
/// # Additional Information
///
/// The max value a u8 can represent is 255.
///
/// # Returns
///
/// [Option<u8>] - `Some(u8)` if the u64 is less than or equal to the max u8 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 255_u64.try_as_u8();
/// assert(val == Some(255_u8));
///
/// // Conversion fails as value is above the max a u8 can represent.
/// let val2 = 256_u64.try_as_u8();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u64() {
Some(asm(input: self) {
Expand All @@ -15,6 +37,28 @@ impl u64 {
}
}

/// Attempts to convert the u64 value into a u16 value.
///
/// # Additional Information
///
/// The max value a u16 can represent is 65_535.
///
/// # Returns
///
/// [Option<u16>] - `Some(u16)` if the u64 is less than or equal to the max u16 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 65_535_u64.try_as_u16();
/// assert(val == Some(65_535_u16));
///
/// // Conversion fails as value is above the max a u16 can represent.
/// let val2 = 65_536_u64.try_as_u16();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u16(self) -> Option<u16> {
if self <= u16::max().as_u64() {
Some(asm(input: self) {
Expand All @@ -25,6 +69,28 @@ impl u64 {
}
}

/// Attempts to convert the u64 value into a u32 value.
///
/// # Additional Information
///
/// The max value a u32 can represent is 4_294_967_295.
///
/// # Returns
///
/// [Option<u32>] - `Some(u32)` if the u64 is less than or equal to the max u32 value. Else `None`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let val = 4_294_967_295_u64.try_as_u32();
/// assert(val == Some(4_294_967_295_u32));
///
/// // Conversion fails as value is above the max a u32 can represent.
/// let val2 = 4_294_967_296_u64.try_as_u32();
/// assert(val == None);
/// }
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn try_as_u32(self) -> Option<u32> {
if self <= u32::max().as_u64() {
Some(asm(input: self) {
Expand Down
Loading