/
githubmirror
/
sway
Обзор
Документация
Войти
/
githubmirror
/
sway
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sway-lib-std/src/array_conversions/u16.sw
124 строки
3 KB
Cameron Carstens
Increase test coverage on std-lib (#6087)
11 июн 2024, 07:03
Не верифицирован
11 июн 2024, 07:03
5e24673
Код
Авторство
О чём код?
library; use ::assert::assert; impl u16 { /// Converts the `u16` to a sequence of little-endian bytes. /// /// # Returns /// /// * [[u8; 2]] - An array of 2 `u8` bytes that compose the `u16`. /// /// # Examples /// /// ```sway /// fn foo() { /// let x: u16 = 513; /// let result = x.to_le_bytes(); /// /// assert(result[0] == 1_u8); /// assert(result[1] == 2_u8); /// } /// ``` pub fn to_le_bytes(self) -> [u8; 2] { let output = [0_u8, 0_u8]; asm(input: self, off: 0xFF, i: 0x8, output: output, r1) { and r1 input off; sb output r1 i0; srl r1 input i; and r1 r1 off; sb output r1 i1; output: [u8; 2] } } /// Converts a sequence of little-endian bytes to a `u16`. /// /// # Arguments /// /// * `bytes`: [[u8; 2]] - A sequence of 2 `u8` bytes that represent a `u16`. /// /// # Returns /// /// * [u16] - The resulting `u16` value. /// /// # Examples /// /// ```sway /// fn foo() { /// let bytes = [1_u8, 2_u8]; /// let result = u16::from_le_bytes(bytes); /// /// assert(result == 513_u16); /// } /// ``` pub fn from_le_bytes(bytes: [u8; 2]) -> Self { asm(a: bytes[0], b: bytes[1], i: 0x8, r1) { sll r1 b i; or r1 a r1; r1: u16 } } /// Converts the `u16` to a sequence of big-endian bytes. /// /// # Returns /// /// * [[u8; 2]] - An array of 2 `u8` bytes that compose the `u16`. /// /// # Examples /// /// ```sway /// fn foo() { /// let x: u16 = 513; /// let result = x.to_be_bytes(); /// /// assert(result[0] == 2_u8); /// assert(result[1] == 1_u8); /// } /// ``` pub fn to_be_bytes(self) -> [u8; 2] { let output = [0_u8, 0_u8]; asm(input: self, off: 0xFF, i: 0x8, output: output, r1) { srl r1 input i; sb output r1 i0; and r1 input off; sb output r1 i1; output: [u8; 2] } } /// Converts a sequence of big-endian bytes to a `u16`. /// /// # Arguments /// /// * `bytes`: [[u8; 2]] - A sequence of 2 `u8` bytes that represent a `u16`. /// /// # Returns /// /// * [u16] - The resulting `u16` value. /// /// # Examples /// /// ```sway /// fn foo() { /// let bytes = [2_u8, 1_u8]; /// let result = u16::from_be_bytes(bytes); /// /// assert(result == 513_u16); /// } /// ``` pub fn from_be_bytes(bytes: [u8; 2]) -> Self { asm(a: bytes[0], b: bytes[1], i: 0x8, r1) { sll r1 a i; or r1 r1 b; r1: u16 } } }