use crate::exn::*;
use crate::re_throw::OrSyscallThrow;
use crate::sys::{self, SyscallNo};
use crate::sys_result::bytestring as result;
use crate::types::{RuntimeMemoryLength, StorageKey};
use core::mem::MaybeUninit;
pub fn bytestring_get_len(key: &StorageKey) -> u32 {
let mut out: RuntimeMemoryLength = 0;
sys::raw_BytestringGetLen(key, &mut out)
.or_syscall_throw(SyscallNo::kBytestringGetLen);
out
}
pub fn bytestring_exists(key: &StorageKey) -> bool {
result::bytestring_exists(key)
.or_syscall_throw(SyscallNo::kBytestringGetLen)
}
pub fn bytestring_set(key: &StorageKey, slice: &[u8]) {
sys::raw_BytestringSet(key, slice)
.or_syscall_throw(SyscallNo::kBytestringSet);
}
pub fn bytestring_set_exclusive(key: &StorageKey, slice: &[u8]) {
result::bytestring_set_exclusive(key, slice)
.or_syscall_throw(SyscallNo::kBytestringSet);
}
pub fn bytestring_get_sized<const N: usize>(key: &StorageKey) -> [u8; N] {
let mut bytes_mu = [MaybeUninit::<u8>::uninit(); N];
let mut len = MaybeUninit::uninit();
sys::uninit_BytestringGet(key, &mut bytes_mu, &mut len)
.or_syscall_throw(SyscallNo::kBytestringGet);
let len = unsafe { len.assume_init() };
if len != N as u32 {
library_throw(LibraryExn::BytestringWrongSize {
expected: N as u32,
found: len,
});
}
let bytes: [u8; N] =
unsafe { bytes_mu.map(|elt| MaybeUninit::assume_init(elt)) };
bytes
}
#[cfg(feature = "alloc")]
mod bytestring_get_alloc {
use super::*;
use alloc::boxed::Box;
pub fn bytestring_get(key: &StorageKey) -> Box<[u8]> {
use crate::re_throw::OrReThrow;
bytestring_try_get(key).or_re_throw()
}
pub fn bytestring_try_get(
key: &StorageKey,
) -> Result<Box<[u8]>, LibraryExn> {
result::bytestring_get(key).or_syscall_exn(SyscallNo::kBytestringGet)
}
}
#[cfg(feature = "alloc")]
pub use bytestring_get_alloc::*;