/
githubmirror
/
sway
Обзор
Документация
Войти
/
githubmirror
/
sway
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sway-lib-std/src/raw_slice.sw
215 строк
5 KB
Igor Rončević
Optimize dynamic `std` types (#7683)
15 июл 2026, 13:25
Не верифицирован
15 июл 2026, 13:25
be2f1bc
Код
Авторство
О чём код?
library; use ::ops::*; use ::raw_ptr::*; use ::error_signals::REVERT_WITH_RAW_SLICE_LEN_ZST; /// Trait to return a type as a `raw_slice`. pub trait AsRawSlice { /// Converts self into a `raw_slice`. /// /// # Returns /// /// * [raw_slice] - The newly created `raw_slice` from self. /// /// # Examples /// /// ```sway /// use std::alloc::alloc_bytes; /// /// struct MyType { /// ptr: raw_ptr, /// len: u64 /// } /// /// impl AsRawSlice for MyType { /// fn as_raw_slice(self) -> raw_slice { /// from_parts(self.ptr, self.len) /// } /// } /// /// fn foo() { /// let my_type = MyType { /// ptr: alloc_bytes(0), /// len: 0 /// } /// let slice = my_type.as_raw_slice(); /// assert(slice.ptr() == my_type.ptr); /// assert(slice.number_of_bytes() == my_type.len); /// } /// ``` fn as_raw_slice(self) -> raw_slice; } /// Returns a `raw_slice` from a pointer and length. /// /// # Arguments /// /// * `parts`: [(raw_ptr, u64)] - A location in memory and a length to become a `raw_slice`. /// /// # Returns /// /// * [raw_slice] - The newly created `raw_slice`. fn from_parts(parts: (raw_ptr, u64)) -> raw_slice { __transmute::<(raw_ptr, u64), raw_slice>(parts) } /// Returns a pointer and length from a `raw_slice`. /// /// # Arguments /// /// * `slice`: [raw_slice] - The slice to be broken into its parts. /// /// # Returns /// /// * [(raw_ptr, u64)] - A tuple of the location in memory of the original `raw_slice` and its length. fn into_parts(slice: raw_slice) -> (raw_ptr, u64) { __transmute::<raw_slice, (raw_ptr, u64)>(slice) } impl raw_slice { /// Forms a slice from a pointer and a length. /// /// # Arguments /// /// * `ptr`: [raw_ptr] - The pointer to the location in memory. /// * `count`: [u64] - The number of `__size_of::<T>` bytes. /// /// # Returns /// /// * [raw_slice] - The newly created `raw_slice`. /// /// # Examples /// /// ```sway /// use std::alloc::alloc; /// /// fn foo() { /// let ptr = alloc::<u64>(1); /// let slice = raw_slice::from_parts::<u64>(ptr, 1); /// assert(slice.len::<u64>() == 1); /// } /// ``` pub fn from_parts<T>(ptr: raw_ptr, count: u64) -> Self { from_parts((ptr, count * __size_of::<T>())) } /// Returns `self` as a pointer and length in bytes. /// /// # Returns /// /// * [(raw_ptr, u64)] - A tuple of the `self`'s pointer and length in bytes. /// /// # Examples /// /// ```sway /// use std::alloc::alloc; /// /// fn foo() { /// let ptr = alloc::<u64>(1); /// let slice = raw_slice::from_parts::<u64>(ptr, 1); /// let slice_ptr = slice.ptr(); /// let slice_len_in_bytes = slice.number_of_bytes(); /// /// let (ptr_part, len_part) = slice.into_parts(); /// /// assert(slice_ptr == ptr_part); /// assert(slice_len_in_bytes == len_part); /// } /// ``` pub fn into_parts(self) -> (raw_ptr, u64) { into_parts(self) } /// Returns the pointer to the slice. /// /// # Returns /// /// * [raw_ptr] - The pointer to the location in memory of the `raw_slice`. /// /// # Examples /// /// ```sway /// use std::alloc::alloc; /// /// fn foo() { /// let ptr = alloc::<u64>(1); /// let slice = raw_slice::from_parts::<u64>(ptr, 1); /// let slice_ptr = slice.ptr(); /// assert(slice_ptr == ptr); /// } /// ``` pub fn ptr(self) -> raw_ptr { into_parts(self).0 } /// Returns the number of elements in the slice, where each element has a `__size_of::<T>`. /// /// # Additional information /// /// If the slice size in bytes is not a multiple of `__size_of::<T>`, the return length /// is the maximum number of elements of type `T` that can fit into the slice. /// /// # Returns /// /// * [u64] - The length of the slice based on `__size_of::<T>`. /// /// # Examples /// /// ```sway /// use std::alloc::alloc; /// /// fn foo() { /// let ptr = alloc::<u64>(1); /// let slice = raw_slice::from_parts::<u64>(ptr, 1); /// assert(slice.len::<u64>() == 1); /// } /// ``` /// /// # Reverts /// /// * When `T` is a zero-sized type. pub fn len<T>(self) -> u64 { const SIZE_OF_T: u64 = __size_of::<T>(); if SIZE_OF_T != 0 { into_parts(self).1 / SIZE_OF_T } else { __revert(REVERT_WITH_RAW_SLICE_LEN_ZST); } } /// Returns the number of elements in the slice when the elements are bytes. /// /// # Returns /// /// * [u64] - The number of bytes in the `raw_slice`. /// /// # Examples /// /// ```sway /// use std::alloc::alloc; /// /// fn foo() { /// let ptr = alloc::<u64>(1); /// let slice = raw_slice::from_parts::<u64>(ptr, 1); /// assert(slice.number_of_bytes() == 8); /// } /// ``` pub fn number_of_bytes(self) -> u64 { into_parts(self).1 } } impl PartialEq for raw_slice { fn eq(self, other: Self) -> bool { self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() } } impl Eq for raw_slice {} impl AsRawSlice for str { fn as_raw_slice(self) -> raw_slice { __transmute::<str, raw_slice>(self) } }