use wasm_bindgen::prelude::*;
use crate::wasm_iter::ByteIterator;
use super::BytesMut;
#[allow(missing_docs)]
#[wasm_bindgen]
impl BytesMut {
#[wasm_bindgen(constructor)]
pub fn new_wasm() -> Self {
Self::new()
}
#[wasm_bindgen(js_name = "withCapacity")]
pub fn with_capacity_wasm(capacity: usize) -> Self {
Self::with_capacity(capacity)
}
#[wasm_bindgen(js_name = "zeroed")]
pub fn zeroed_wasm(len: usize) -> Self {
Self::zeroed(len)
}
#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes_wasm(data: &[u8]) -> BytesMut {
BytesMut::from(data)
}
#[wasm_bindgen(js_name = "fromUtf8")]
pub fn from_utf8_wasm(s: js_sys::JsString) -> Result<BytesMut, JsError> {
let s = crate::wasm::js_string_to_string(&s)?;
Ok(BytesMut::from(s.as_bytes()))
}
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes_wasm(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
#[wasm_bindgen(js_name = "decodeUtf8")]
pub fn decode_utf8_wasm(&self) -> Result<String, JsError> {
core::str::from_utf8(self.as_slice())
.map(|s| s.to_string())
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "len")]
pub fn len_wasm(&self) -> usize {
self.len()
}
#[wasm_bindgen(js_name = "isEmpty")]
pub fn is_empty_wasm(&self) -> bool {
self.is_empty()
}
#[wasm_bindgen(js_name = "capacity")]
pub fn capacity_wasm(&self) -> usize {
self.capacity()
}
#[wasm_bindgen(js_name = "isInline")]
pub fn is_inline_wasm(&self) -> bool {
self.is_inline()
}
#[wasm_bindgen(js_name = "isHeap")]
pub fn is_heap_wasm(&self) -> bool {
self.is_heap()
}
#[wasm_bindgen(js_name = "remaining")]
pub fn remaining_wasm(&self) -> usize {
bytes::Buf::remaining(self)
}
#[wasm_bindgen(js_name = "remainingMut")]
pub fn remaining_mut_wasm(&self) -> usize {
bytes::BufMut::remaining_mut(self)
}
#[wasm_bindgen(js_name = "advance")]
pub fn advance_wasm(&mut self, cnt: usize) -> Result<(), JsError> {
self
.try_advance(cnt)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "clear")]
pub fn clear_wasm(&mut self) {
self.clear();
}
#[wasm_bindgen(js_name = "truncate")]
pub fn truncate_wasm(&mut self, new_len: usize) {
self.truncate(new_len);
}
#[wasm_bindgen(js_name = "reserve")]
pub fn reserve_wasm(&mut self, additional: usize) {
self.reserve(additional);
}
#[wasm_bindgen(js_name = "makeHeap")]
pub fn make_heap_wasm(&mut self) {
self.make_heap();
}
#[wasm_bindgen(js_name = "tryMakeInline")]
pub fn try_make_inline_wasm(&mut self) -> bool {
self.try_make_inline()
}
#[wasm_bindgen(js_name = "resize")]
pub fn resize_wasm(&mut self, new_len: usize, value: u8) {
self.resize(new_len, value);
}
#[wasm_bindgen(js_name = "splitTo")]
pub fn split_to_wasm(&mut self, at: usize) -> Result<BytesMut, JsError> {
self
.try_split_to(at)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitOff")]
pub fn split_off_wasm(&mut self, at: usize) -> Result<BytesMut, JsError> {
self
.try_split_off(at)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "split")]
pub fn split_wasm(&mut self) -> BytesMut {
self.split()
}
#[wasm_bindgen(js_name = "slice")]
pub fn slice_wasm(&self, start: usize, end: usize) -> Result<BytesMut, JsError> {
if start > end || end > self.len() {
return Err(JsError::new("slice range is out of bounds"));
}
Ok(BytesMut::from(&self.as_slice()[start..end]))
}
#[wasm_bindgen(js_name = "clone")]
pub fn clone_wasm(&self) -> BytesMut {
self.clone()
}
#[wasm_bindgen(js_name = "freezeShared")]
pub fn freeze_shared_wasm(&mut self) -> crate::bytes::strategy::shared::wasm::WasmSharedBytes {
let value = core::mem::take(self);
crate::bytes::strategy::shared::wasm::WasmSharedBytes::from_inner(value.freeze_shared())
}
#[wasm_bindgen(js_name = "freezeCompact")]
pub fn freeze_compact_wasm(&mut self) -> crate::bytes::strategy::compact::wasm::WasmCompactBytes {
let value = core::mem::take(self);
crate::bytes::strategy::compact::wasm::WasmCompactBytes::from_inner(value.freeze_compact())
}
#[wasm_bindgen(js_name = "getU8")]
pub fn get_u8_wasm(&mut self) -> Result<u8, JsError> {
if bytes::Buf::remaining(self) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u8(self))
}
#[wasm_bindgen(js_name = "getI8")]
pub fn get_i8_wasm(&mut self) -> Result<i8, JsError> {
if bytes::Buf::remaining(self) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i8(self))
}
#[wasm_bindgen(js_name = "getU16")]
pub fn get_u16_wasm(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16(self))
}
#[wasm_bindgen(js_name = "getU16Le")]
pub fn get_u16_le_wasm(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16_le(self))
}
#[wasm_bindgen(js_name = "getI16")]
pub fn get_i16_wasm(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16(self))
}
#[wasm_bindgen(js_name = "getI16Le")]
pub fn get_i16_le_wasm(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16_le(self))
}
#[wasm_bindgen(js_name = "getU32")]
pub fn get_u32_wasm(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32(self))
}
#[wasm_bindgen(js_name = "getU32Le")]
pub fn get_u32_le_wasm(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32_le(self))
}
#[wasm_bindgen(js_name = "getI32")]
pub fn get_i32_wasm(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32(self))
}
#[wasm_bindgen(js_name = "getI32Le")]
pub fn get_i32_le_wasm(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32_le(self))
}
#[wasm_bindgen(js_name = "getU64")]
pub fn get_u64_wasm(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64(self))
}
#[wasm_bindgen(js_name = "getU64Le")]
pub fn get_u64_le_wasm(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64_le(self))
}
#[wasm_bindgen(js_name = "getI64")]
pub fn get_i64_wasm(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64(self))
}
#[wasm_bindgen(js_name = "getI64Le")]
pub fn get_i64_le_wasm(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64_le(self))
}
#[wasm_bindgen(js_name = "getF32")]
pub fn get_f32_wasm(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32(self))
}
#[wasm_bindgen(js_name = "getF32Le")]
pub fn get_f32_le_wasm(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32_le(self))
}
#[wasm_bindgen(js_name = "getF64")]
pub fn get_f64_wasm(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64(self))
}
#[wasm_bindgen(js_name = "getF64Le")]
pub fn get_f64_le_wasm(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64_le(self))
}
#[wasm_bindgen(js_name = "getUint")]
pub fn get_uint_wasm(&mut self, width: usize) -> Result<u64, JsError> {
bytes::Buf::try_get_uint(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "getUintLe")]
pub fn get_uint_le_wasm(&mut self, width: usize) -> Result<u64, JsError> {
bytes::Buf::try_get_uint_le(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "getUintNe")]
pub fn get_uint_ne_wasm(&mut self, width: usize) -> Result<u64, JsError> {
bytes::Buf::try_get_uint_ne(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "getInt")]
pub fn get_int_wasm(&mut self, width: usize) -> Result<i64, JsError> {
bytes::Buf::try_get_int(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "getIntLe")]
pub fn get_int_le_wasm(&mut self, width: usize) -> Result<i64, JsError> {
bytes::Buf::try_get_int_le(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "getIntNe")]
pub fn get_int_ne_wasm(&mut self, width: usize) -> Result<i64, JsError> {
bytes::Buf::try_get_int_ne(self, width).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putSlice")]
pub fn put_slice_wasm(&mut self, data: &[u8]) {
self.extend_from_slice(data);
}
#[wasm_bindgen(js_name = "putBytes")]
pub fn put_bytes_wasm(&mut self, value: u8, count: usize) {
bytes::BufMut::put_bytes(self, value, count);
}
#[wasm_bindgen(js_name = "putUint")]
pub fn put_uint_wasm(&mut self, value: u64, width: usize) -> Result<(), JsError> {
self
.try_put_uint(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putUintLe")]
pub fn put_uint_le_wasm(&mut self, value: u64, width: usize) -> Result<(), JsError> {
self
.try_put_uint_le(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putUintNe")]
pub fn put_uint_ne_wasm(&mut self, value: u64, width: usize) -> Result<(), JsError> {
self
.try_put_uint_ne(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putInt")]
pub fn put_int_wasm(&mut self, value: i64, width: usize) -> Result<(), JsError> {
self
.try_put_int(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putIntLe")]
pub fn put_int_le_wasm(&mut self, value: i64, width: usize) -> Result<(), JsError> {
self
.try_put_int_le(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putIntNe")]
pub fn put_int_ne_wasm(&mut self, value: i64, width: usize) -> Result<(), JsError> {
self
.try_put_int_ne(value, width)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "putU8")]
pub fn put_u8_wasm(&mut self, val: u8) {
bytes::BufMut::put_u8(self, val);
}
#[wasm_bindgen(js_name = "putI8")]
pub fn put_i8_wasm(&mut self, val: i8) {
bytes::BufMut::put_i8(self, val);
}
#[wasm_bindgen(js_name = "putU16")]
pub fn put_u16_wasm(&mut self, val: u16) {
bytes::BufMut::put_u16(self, val);
}
#[wasm_bindgen(js_name = "putU16Le")]
pub fn put_u16_le_wasm(&mut self, val: u16) {
bytes::BufMut::put_u16_le(self, val);
}
#[wasm_bindgen(js_name = "putI16")]
pub fn put_i16_wasm(&mut self, val: i16) {
bytes::BufMut::put_i16(self, val);
}
#[wasm_bindgen(js_name = "putI16Le")]
pub fn put_i16_le_wasm(&mut self, val: i16) {
bytes::BufMut::put_i16_le(self, val);
}
#[wasm_bindgen(js_name = "putU32")]
pub fn put_u32_wasm(&mut self, val: u32) {
bytes::BufMut::put_u32(self, val);
}
#[wasm_bindgen(js_name = "putU32Le")]
pub fn put_u32_le_wasm(&mut self, val: u32) {
bytes::BufMut::put_u32_le(self, val);
}
#[wasm_bindgen(js_name = "putI32")]
pub fn put_i32_wasm(&mut self, val: i32) {
bytes::BufMut::put_i32(self, val);
}
#[wasm_bindgen(js_name = "putI32Le")]
pub fn put_i32_le_wasm(&mut self, val: i32) {
bytes::BufMut::put_i32_le(self, val);
}
#[wasm_bindgen(js_name = "putU64")]
pub fn put_u64_wasm(&mut self, val: u64) {
bytes::BufMut::put_u64(self, val);
}
#[wasm_bindgen(js_name = "putU64Le")]
pub fn put_u64_le_wasm(&mut self, val: u64) {
bytes::BufMut::put_u64_le(self, val);
}
#[wasm_bindgen(js_name = "putI64")]
pub fn put_i64_wasm(&mut self, val: i64) {
bytes::BufMut::put_i64(self, val);
}
#[wasm_bindgen(js_name = "putI64Le")]
pub fn put_i64_le_wasm(&mut self, val: i64) {
bytes::BufMut::put_i64_le(self, val);
}
#[wasm_bindgen(js_name = "putF32")]
pub fn put_f32_wasm(&mut self, val: f32) {
bytes::BufMut::put_f32(self, val);
}
#[wasm_bindgen(js_name = "putF32Le")]
pub fn put_f32_le_wasm(&mut self, val: f32) {
bytes::BufMut::put_f32_le(self, val);
}
#[wasm_bindgen(js_name = "putF64")]
pub fn put_f64_wasm(&mut self, val: f64) {
bytes::BufMut::put_f64(self, val);
}
#[wasm_bindgen(js_name = "putF64Le")]
pub fn put_f64_le_wasm(&mut self, val: f64) {
bytes::BufMut::put_f64_le(self, val);
}
#[wasm_bindgen(js_name = "iter")]
pub fn iter_wasm(&self) -> ByteIterator {
ByteIterator::new(self.as_slice().to_vec())
}
}