sledgehammer 0.2.0

Fast bindings for dom manipulations
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use crate::{channel::MaybeId, ElementBuilder, IntoAttribue, IntoElement, NodeId, WritableText};

// operations that have no booleans can be encoded as a half byte, these are placed first
pub(crate) enum Op {
    /// Navigates to the last node to the first child of the current node.
    FirstChild = 0,

    /// Navigates to the last node to the last child of the current node.
    NextSibling = 1,

    /// Navigates to the last node to the parent of the current node.
    ParentNode = 2,

    /// Stores the last node with a new id.
    StoreWithId = 3,

    /// Manually set the last node.
    SetLastNode = 4,

    /// Stop
    Stop = 5,

    /// Build Full Element
    BuildFullElement = 6,

    /// Pop the topmost node from our stack and append them to the node
    AppendChildren = 7,

    /// Replace a given (single) node with a handful of nodes currently on the stack.
    ReplaceWith = 8,

    /// Insert a number of nodes after a given node.
    InsertAfter = 9,

    /// Insert a number of nodes before a given node.
    InsertBefore = 10,

    /// Remove a particular node from the DOM
    Remove = 11,

    /// Create a new text node
    CreateTextNode = 12,

    /// Create a new element node
    CreateElement = 13,

    /// Set the textcontent of a node.
    SetText = 14,

    /// Set the value of a node's attribute.
    SetAttribute = 15,

    /// Remove an attribute from a node.
    RemoveAttribute = 16,

    /// Set a style property on a node.
    SetStyle = 17,

    /// Remove a style property from a node.
    RemoveStyle = 18,

    /// Clones a node.
    CloneNode = 19,

    /// Does nothing, but allows us to skip a byte.
    NoOp = 20,
}

/// A batch of operations ready to perform on the DOM.
pub trait PreparedBatch {
    fn msg(&self) -> &[u8];
    fn str(&self) -> &[u8];
}

/// A batch of operations ready to perform on the DOM.
pub struct FinalizedBatch {
    pub msg: Vec<u8>,
    pub str: Vec<u8>,
}

impl PreparedBatch for FinalizedBatch {
    fn msg(&self) -> &[u8] {
        &self.msg
    }
    fn str(&self) -> &[u8] {
        &self.str
    }
}

impl<'a> PreparedBatch for &'a FinalizedBatch {
    fn msg(&self) -> &[u8] {
        &self.msg
    }
    fn str(&self) -> &[u8] {
        &self.str
    }
}

/// A batch of static operations ready to perform on the DOM.
/// This is meant to be generated from FinalizedBatch from a macro.
pub struct StaticBatch {
    pub msg: &'static [u8],
    pub str: &'static [u8],
}

impl PreparedBatch for StaticBatch {
    fn msg(&self) -> &[u8] {
        self.msg
    }
    fn str(&self) -> &[u8] {
        self.str
    }
}

impl<'a> PreparedBatch for &'a StaticBatch {
    fn msg(&self) -> &[u8] {
        self.msg
    }
    fn str(&self) -> &[u8] {
        self.str
    }
}

/// A batch of operations to perform on the DOM.
///
/// This allows you to build up a batch of operations to perform on the DOM outside of the main MsgChannel batch.
/// This is useful for building up a batch of operations to perform on the DOM many times. If the operation is only performed once, it is better to use the `MsgChannel` directly because it reuses the same allocation from the last batch of operations.
/// See [`MsgChannel::append`] and [`MsgChannel::run_batch`] for examples.
/// The methods on this struct are a subset of the methods on [`MsgChannel`] and work the same with the exception of [`Batch::finalize`].
pub struct Batch {
    pub(crate) msg: Vec<u8>,
    pub(crate) str_buf: Vec<u8>,
    pub(crate) current_op_batch_idx: usize,
    pub(crate) current_op_byte_idx: usize,
    pub(crate) current_op_bit_pack_index: u8,
}

impl Default for Batch {
    fn default() -> Self {
        Self {
            msg: Vec::new(),
            str_buf: Vec::new(),
            current_op_byte_idx: 3,
            current_op_bit_pack_index: 0,
            current_op_batch_idx: 0,
        }
    }
}

impl Batch {
    /// Finalizes the batch and prepares it to be run
    pub fn finalize(mut self) -> FinalizedBatch {
        self.encode_op(Op::Stop);
        FinalizedBatch {
            msg: self.msg,
            str: self.str_buf,
        }
    }

    /// Appends a number of nodes as children of the given node.
    pub fn append_child(&mut self, root: MaybeId, child: MaybeId) {
        self.encode_op(Op::AppendChildren);
        let size = root.encoded_size() + child.encoded_size();
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            self.encode_maybe_id_prealloc(child);
        }
    }

    /// Replace a node with another node
    pub fn replace_with(&mut self, root: MaybeId, node: MaybeId) {
        self.encode_op(Op::ReplaceWith);
        let size = root.encoded_size() + node.encoded_size();
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            self.encode_maybe_id_prealloc(node);
        }
    }

    /// Insert a single node after a given node.
    pub fn insert_after(&mut self, root: MaybeId, node: MaybeId) {
        self.encode_op(Op::InsertAfter);
        let size = root.encoded_size() + node.encoded_size();
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            self.encode_maybe_id_prealloc(node);
        }
    }

    /// Insert a single node before a given node.
    pub fn insert_before(&mut self, root: MaybeId, node: MaybeId) {
        self.encode_op(Op::InsertBefore);
        let size = root.encoded_size() + node.encoded_size();
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            self.encode_maybe_id_prealloc(node);
        }
    }

    /// Remove a node from the DOM.
    pub fn remove(&mut self, id: MaybeId) {
        self.encode_op(Op::Remove);
        self.encode_maybe_id(id);
    }

    /// Create a new text node
    pub fn create_text_node(&mut self, text: impl WritableText, id: MaybeId) {
        self.encode_op(Op::CreateTextNode);
        let size = id.encoded_size() + 2;
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_str_prealloc(text);
            self.encode_maybe_id_prealloc(id);
        }
    }

    /// Create a new element node
    pub fn create_element<'a, 'b, E>(&mut self, tag: E, id: Option<NodeId>)
    where
        E: IntoElement<'a, 'b>,
    {
        self.encode_op(Op::CreateElement);
        self.msg
            .reserve((E::SINGLE_BYTE as u8 + (id.is_some() as u8) * 4) as usize);
        unsafe {
            tag.encode_prealloc(self);
            self.encode_optional_id_prealloc(id);
        }
    }

    /// Set the textcontent of a node.
    pub fn set_text(&mut self, text: impl WritableText, root: MaybeId) {
        self.encode_op(Op::SetText);
        let size = root.encoded_size() + 2;
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            self.encode_str_prealloc(text);
        }
    }

    /// Set the value of a node's attribute.
    #[inline(never)]
    pub fn set_attribute<'a, 'b, A>(&mut self, attr: A, value: impl WritableText, root: MaybeId)
    where
        A: IntoAttribue<'a, 'b>,
    {
        self.encode_op(Op::SetAttribute);
        self.msg
            .reserve((A::SINGLE_BYTE as u8 + root.encoded_size() + 2) as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            attr.encode_prealloc(self);
            self.encode_str_prealloc(value);
        }
    }

    /// Remove an attribute from a node.
    pub fn remove_attribute<'a, 'b, A>(&mut self, attr: A, root: MaybeId)
    where
        A: IntoAttribue<'a, 'b>,
    {
        self.encode_op(Op::RemoveAttribute);
        let size = A::SINGLE_BYTE as u8 + root.encoded_size();
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(root);
            attr.encode_prealloc(self);
        }
    }

    /// Clone a node and store it with a new id.
    pub fn clone_node(&mut self, id: MaybeId, new_id: MaybeId) {
        self.encode_op(Op::CloneNode);
        let size = id.encoded_size() + new_id.encoded_size();
        self.msg.reserve(size as usize);
        self.encode_maybe_id(id);
        self.encode_maybe_id(new_id);
    }

    /// Move the last node to the first child
    pub fn first_child(&mut self) {
        self.encode_op(Op::FirstChild);
    }

    /// Move the last node to the next sibling
    pub fn next_sibling(&mut self) {
        self.encode_op(Op::NextSibling);
    }

    /// Move the last node to the parent node
    pub fn parent_node(&mut self) {
        self.encode_op(Op::ParentNode);
    }

    /// Store the last node with the given id. This is useful when traversing the document tree.
    pub fn store_with_id(&mut self, id: NodeId) {
        self.encode_op(Op::StoreWithId);
        self.encode_id(id);
    }

    /// Set the last node to the given id. The last node can be used to traverse the document tree without passing objects between wasm and js every time.
    pub fn set_last_node(&mut self, id: NodeId) {
        self.encode_op(Op::SetLastNode);
        self.encode_id(id);
    }

    /// Build a full element, slightly more efficent than creating the element creating the element with `create_element` and then setting the attributes.
    pub fn build_full_element(&mut self, el: ElementBuilder) {
        self.encode_op(Op::BuildFullElement);
        el.encode(self);
    }

    /// Set a style property on a node.
    pub fn set_style(&mut self, style: &str, value: &str, id: MaybeId) {
        self.encode_op(Op::SetStyle);
        let size = id.encoded_size() + 2 + 2;
        self.msg.reserve(size as usize);
        self.encode_maybe_id(id);
        unsafe {
            self.encode_str_prealloc(style);
            self.encode_str_prealloc(value);
        }
    }

    /// Remove a style property from a node.
    pub fn remove_style(&mut self, style: &str, id: MaybeId) {
        self.encode_op(Op::RemoveStyle);
        let size = id.encoded_size() + 2;
        self.msg.reserve(size as usize);
        unsafe {
            self.encode_maybe_id_prealloc(id);
            self.encode_str_prealloc(style);
        }
    }

    #[inline]
    pub(crate) unsafe fn encode_optional_id_prealloc(&mut self, id: Option<NodeId>) {
        match id {
            Some(id) => {
                self.encode_bool(true);
                self.encode_id_prealloc(id);
            }
            None => {
                self.encode_bool(false);
            }
        }
    }

    #[inline]
    pub(crate) unsafe fn encode_maybe_id_prealloc(&mut self, id: MaybeId) {
        match id {
            MaybeId::Node(id) => {
                self.encode_bool(true);
                self.encode_id_prealloc(id);
            }
            MaybeId::LastNode => {
                self.encode_bool(false);
            }
        }
    }

    #[inline]
    pub(crate) fn encode_maybe_id(&mut self, id: MaybeId) {
        match id {
            MaybeId::Node(id) => {
                self.encode_bool(true);
                self.encode_id(id);
            }
            MaybeId::LastNode => {
                self.encode_bool(false);
            }
        }
    }

    #[inline(always)]
    pub(crate) unsafe fn encode_id_prealloc(&mut self, id: NodeId) {
        self.encode_u32_prealloc(id.0);
    }

    #[inline(always)]
    pub(crate) fn encode_id(&mut self, id: NodeId) {
        self.encode_u32(id.0);
    }

    #[inline(always)]
    pub(crate) fn encode_u32(&mut self, val: u32) {
        self.msg.reserve(4);
        unsafe {
            self.encode_u32_prealloc(val);
        }
    }

    #[inline(always)]
    pub(crate) unsafe fn encode_u32_prealloc(&mut self, val: u32) {
        let le = val.to_le();
        unsafe {
            let len = self.msg.len();
            self.msg.as_mut_ptr().add(len).cast::<u32>().write(le);
            self.msg.set_len(len + 4);
        }
    }

    #[inline(always)]
    pub(crate) fn encode_u16(&mut self, val: u16) {
        self.msg.reserve(2);
        unsafe {
            self.encode_u16_prealloc(val);
        }
    }

    #[inline(always)]
    pub(crate) unsafe fn encode_u16_prealloc(&mut self, val: u16) {
        let le = val.to_le();
        #[allow(clippy::uninit_vec)]
        unsafe {
            let len = self.msg.len();
            self.msg.as_mut_ptr().add(len).cast::<u16>().write(le);
            self.msg.set_len(len + 2);
        }
    }

    #[inline]
    pub(crate) fn encode_str(&mut self, string: impl WritableText) {
        let prev_len = self.str_buf.len();
        string.write_as_text(&mut self.str_buf);
        let len = self.str_buf.len() - prev_len;
        self.encode_u16(len as u16);
    }

    #[inline]
    pub(crate) unsafe fn encode_str_prealloc(&mut self, string: impl WritableText) {
        let prev_len = self.str_buf.len();
        string.write_as_text(&mut self.str_buf);
        let len = self.str_buf.len() - prev_len;
        self.encode_u16_prealloc(len as u16);
    }

    #[inline]
    pub(crate) fn encode_cachable_str(&mut self, string: impl WritableText) {
        let prev_len = self.str_buf.len();
        string.write_as_text(&mut self.str_buf);
        let len = self.str_buf.len() - prev_len;
        self.encode_u16(len as u16);
    }

    #[inline]
    pub(crate) fn encode_op(&mut self, op: Op) {
        let u8_op = op as u8;

        self.current_op_byte_idx += 1;
        if self.current_op_byte_idx - self.current_op_batch_idx < 4 {
            unsafe {
                *self.msg.get_unchecked_mut(self.current_op_byte_idx) = u8_op;
            }
        } else {
            self.current_op_batch_idx = self.msg.len();
            self.current_op_byte_idx = self.current_op_batch_idx;
            // reserve four bytes for the op batch
            #[allow(clippy::uninit_vec)]
            unsafe {
                let len = self.msg.len();
                self.msg.reserve(4);
                self.msg.set_len(len + 4);
                *self.msg.get_unchecked_mut(self.current_op_batch_idx) = u8_op;
            }
        }
        self.current_op_bit_pack_index = 0;
    }

    #[inline]
    pub(crate) fn encode_bool(&mut self, value: bool) {
        if self.current_op_bit_pack_index < 3 {
            if value {
                unsafe {
                    *self.msg.get_unchecked_mut(self.current_op_byte_idx) |=
                        1 << (self.current_op_bit_pack_index + 5);
                }
            }
            self.current_op_bit_pack_index += 1;
        } else {
            todo!("handle more than 3 bools in a op");
        }
    }

    pub(crate) fn append(&mut self, mut batch: Self) {
        // add empty operations to the batch to make sure the batch is aligned
        let operations_left = 3 - (self.current_op_byte_idx - self.current_op_batch_idx);
        for _ in 0..operations_left {
            self.encode_op(Op::NoOp);
        }

        self.current_op_byte_idx = self.msg.len() + batch.current_op_byte_idx;
        self.current_op_batch_idx = self.msg.len() + batch.current_op_batch_idx;
        self.current_op_bit_pack_index = batch.current_op_bit_pack_index;
        self.str_buf.extend_from_slice(&batch.str_buf);
        self.msg.append(&mut batch.msg);
    }
}