1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use wasm_bindgen::prelude::*;
use crate::wasm_iter::ByteIterator;
/// Immutable byte buffer using the Shared strategy.
///
/// Stores up to 62 bytes inline with zero-copy reference-counted cloning for larger data.
/// Available as `SharedBytes` via `import { SharedBytes } from 'smol-bytes/shared'`.
#[allow(missing_docs)]
#[wasm_bindgen(js_name = "SharedBytes")]
pub struct WasmSharedBytes {
inner: super::Bytes,
}
#[allow(missing_docs)]
#[wasm_bindgen(js_class = "SharedBytes")]
impl WasmSharedBytes {
/// Create a new empty `SharedBytes`.
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: super::Bytes::new(),
}
}
/// Create a `SharedBytes` from a byte array.
///
/// @param data - The source bytes to copy.
#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes(data: &[u8]) -> Self {
Self {
inner: super::Bytes::copy_from_slice(data),
}
}
/// Create a `SharedBytes` from a UTF-8 string.
///
/// @param s - The source string whose bytes are copied.
#[wasm_bindgen(js_name = "fromString")]
pub fn from_string(s: &str) -> Self {
Self {
inner: super::Bytes::from(s),
}
}
/// Create a `SharedBytes` from a static string.
///
/// Note: In WASM, this copies the data (true static references cannot cross the JS boundary).
#[wasm_bindgen(js_name = "fromStatic")]
pub fn from_static_wasm(s: &str) -> Self {
// NOTE: we cannot truly take a &'static str from JS, so we copy.
Self {
inner: super::Bytes::copy_from_slice(s.as_bytes()),
}
}
/// Return contents as a `Uint8Array` (copy).
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes(&self) -> Vec<u8> {
self.inner.as_slice().to_vec()
}
/// Return contents as a UTF-8 string.
///
/// @throws {Error} If the buffer contains invalid UTF-8.
#[wasm_bindgen(js_name = "toString")]
pub fn to_string_wasm(&self) -> Result<String, JsError> {
core::str::from_utf8(self.inner.as_slice())
.map(|s| s.to_string())
.map_err(|e| JsError::new(&e.to_string()))
}
/// Return the byte length of the buffer.
#[wasm_bindgen(js_name = "len")]
pub fn len(&self) -> usize {
self.inner.len()
}
/// Return `true` if the buffer has no bytes.
#[wasm_bindgen(js_name = "isEmpty")]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Return `true` if data is stored inline (no heap allocation).
#[wasm_bindgen(js_name = "isInline")]
pub fn is_inline(&self) -> bool {
self.inner.is_inline()
}
/// Return `true` if data is stored on the heap.
#[wasm_bindgen(js_name = "isHeap")]
pub fn is_heap(&self) -> bool {
self.inner.is_heap()
}
/// Return the number of readable bytes remaining.
#[wasm_bindgen(js_name = "remaining")]
pub fn remaining(&self) -> usize {
bytes::Buf::remaining(&self.inner)
}
/// Advance the read cursor by `cnt` bytes.
///
/// @throws {Error} If `cnt` exceeds the number of remaining bytes.
#[wasm_bindgen(js_name = "advance")]
pub fn advance(&mut self, cnt: usize) -> Result<(), JsError> {
self
.inner
.try_advance(cnt)
.map_err(|e| JsError::new(&e.to_string()))
}
/// Clear the buffer, removing all data and resetting the cursor.
#[wasm_bindgen(js_name = "clear")]
pub fn clear(&mut self) {
self.inner.clear();
}
/// Truncate the buffer to `new_len` bytes, discarding any data beyond that point.
#[wasm_bindgen(js_name = "truncate")]
pub fn truncate(&mut self, new_len: usize) {
self.inner.truncate(new_len);
}
/// Split the buffer at position `at`, returning bytes `[0, at)`.
/// Self becomes `[at, len)`.
///
/// @throws {Error} If `at` is out of bounds.
#[wasm_bindgen(js_name = "splitTo")]
pub fn split_to(&mut self, at: usize) -> Result<WasmSharedBytes, JsError> {
self
.inner
.try_split_to(at)
.map(|b| WasmSharedBytes { inner: b })
.map_err(|e| JsError::new(&e.to_string()))
}
/// Split the buffer at position `at`, returning bytes `[at, len)`.
/// Self becomes `[0, at)`.
///
/// @throws {Error} If `at` is out of bounds.
#[wasm_bindgen(js_name = "splitOff")]
pub fn split_off(&mut self, at: usize) -> Result<WasmSharedBytes, JsError> {
self
.inner
.try_split_off(at)
.map(|b| WasmSharedBytes { inner: b })
.map_err(|e| JsError::new(&e.to_string()))
}
/// Return a copy of bytes in range `[start, end)`.
///
/// @throws {Error} If the range is out of bounds.
#[wasm_bindgen(js_name = "slice")]
pub fn slice(&self, start: usize, end: usize) -> Result<WasmSharedBytes, JsError> {
self
.inner
.try_slice(start..end)
.map(|b| WasmSharedBytes { inner: b })
.map_err(|e| JsError::new(&e.to_string()))
}
// -- Buf getters --
/// Read an unsigned 8-bit integer, advancing the cursor by 1 byte.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU8")]
pub fn get_u8(&mut self) -> Result<u8, JsError> {
if bytes::Buf::remaining(&self.inner) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u8(&mut self.inner))
}
/// Read a signed 8-bit integer, advancing the cursor by 1 byte.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI8")]
pub fn get_i8(&mut self) -> Result<i8, JsError> {
if bytes::Buf::remaining(&self.inner) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i8(&mut self.inner))
}
/// Read an unsigned 16-bit integer in big-endian byte order, advancing the cursor by 2 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU16")]
pub fn get_u16(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(&self.inner) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16(&mut self.inner))
}
/// Read an unsigned 16-bit integer in little-endian byte order, advancing the cursor by 2 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU16Le")]
pub fn get_u16_le(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(&self.inner) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16_le(&mut self.inner))
}
/// Read a signed 16-bit integer in big-endian byte order, advancing the cursor by 2 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI16")]
pub fn get_i16(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(&self.inner) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16(&mut self.inner))
}
/// Read a signed 16-bit integer in little-endian byte order, advancing the cursor by 2 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI16Le")]
pub fn get_i16_le(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(&self.inner) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16_le(&mut self.inner))
}
/// Read an unsigned 32-bit integer in big-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU32")]
pub fn get_u32(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32(&mut self.inner))
}
/// Read an unsigned 32-bit integer in little-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU32Le")]
pub fn get_u32_le(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32_le(&mut self.inner))
}
/// Read a signed 32-bit integer in big-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI32")]
pub fn get_i32(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32(&mut self.inner))
}
/// Read a signed 32-bit integer in little-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI32Le")]
pub fn get_i32_le(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32_le(&mut self.inner))
}
/// Read an unsigned 64-bit integer in big-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU64")]
pub fn get_u64(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64(&mut self.inner))
}
/// Read an unsigned 64-bit integer in little-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getU64Le")]
pub fn get_u64_le(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64_le(&mut self.inner))
}
/// Read a signed 64-bit integer in big-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI64")]
pub fn get_i64(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64(&mut self.inner))
}
/// Read a signed 64-bit integer in little-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getI64Le")]
pub fn get_i64_le(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64_le(&mut self.inner))
}
/// Read a 32-bit float in big-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getF32")]
pub fn get_f32(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32(&mut self.inner))
}
/// Read a 32-bit float in little-endian byte order, advancing the cursor by 4 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getF32Le")]
pub fn get_f32_le(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(&self.inner) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32_le(&mut self.inner))
}
/// Read a 64-bit float in big-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getF64")]
pub fn get_f64(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64(&mut self.inner))
}
/// Read a 64-bit float in little-endian byte order, advancing the cursor by 8 bytes.
///
/// @throws {Error} If not enough data remains.
#[wasm_bindgen(js_name = "getF64Le")]
pub fn get_f64_le(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(&self.inner) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64_le(&mut self.inner))
}
// -- Iterator --
/// Return a JS-compatible byte iterator.
///
/// Use `for (const b of buf)` after attaching `Symbol.iterator`.
#[wasm_bindgen(js_name = "iter")]
pub fn iter(&self) -> ByteIterator {
ByteIterator::new(self.inner.as_slice().to_vec())
}
}