maomi 0.5.0

Strict and Performant Web Application Programming
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Helper types for node trees.

use std::{any::Any, collections::HashMap, hash::Hash, marker::PhantomData};

use crate::{
    backend::{tree, AsElementTag},
    error::Error,
};
use tree::ForestTokenAddr;

/// An unsafe option as a union used to reduce some checking overhead.
pub union UnionOption<T> {
    none: (),
    some: std::mem::ManuallyDrop<T>,
}

impl<T> UnionOption<T> {
    /// Create a none value.
    #[inline(always)]
    pub fn none() -> Self {
        Self { none: () }
    }

    /// Create a none value.
    #[inline(always)]
    pub fn some(inner: T) -> Self {
        Self {
            some: std::mem::ManuallyDrop::new(inner),
        }
    }

    /// Assume it is not none and get the contained value.
    #[inline(always)]
    pub unsafe fn unwrap_unchecked(self) -> T {
        std::mem::ManuallyDrop::into_inner(self.some)
    }

    /// Assume it is not none and get the reference.
    #[inline(always)]
    pub unsafe fn as_ref_unchecked(&self) -> &T {
        &self.some
    }
}

/// A weak ref to the owner.
///
/// This is used by the backend implementor.
/// *In most cases, it should not be used in component implementors.*
pub trait OwnerWeak {
    /// Schedule an update on the owner.
    fn apply_updates(&self) -> Result<(), Error>;
    /// Clone the owner itself.
    fn clone_owner_weak(&self) -> Box<dyn OwnerWeak>;
}

/// A general node type.
pub struct DynNode {
    inner: Box<dyn Any>,
}

impl DynNode {
    /// Build from a node.
    #[inline(always)]
    pub fn new<N: 'static>(n: N) -> Self {
        Self { inner: Box::new(n) }
    }

    /// Cast into a node of specified type.
    #[inline(always)]
    pub unsafe fn node_unchecked<N: 'static>(&mut self) -> &mut N {
        &mut *(&mut *self.inner as *mut dyn Any as *mut N)
    }

    /// Cast into a node of specified type.
    #[inline(always)]
    pub fn as_mut<N: 'static>(&mut self) -> &mut N {
        self.inner.downcast_mut().unwrap()
    }

    /// Cast into a node of specified type.
    #[inline(always)]
    pub fn as_ref<N: 'static>(&self) -> &N {
        self.inner.downcast_ref().unwrap()
    }
}

/// A general node list.
pub type DynNodeList = Box<[DynNode]>;

/// A helper type for a node with child nodes.
#[derive(Debug)]
pub struct Node<N: AsElementTag> {
    /// The node itself.
    pub tag: N::Target,
    /// The child nodes of the node.
    pub child_nodes: N::SlotChildren,
}

impl<N: AsElementTag> Node<N> {
    /// Create a node with specified children.
    #[inline(always)]
    pub fn new(tag: N::Target, child_nodes: N::SlotChildren) -> Self {
        Self { tag, child_nodes }
    }

    /// Iterator over slots of the node.
    #[inline]
    pub fn iter_slots(
        &self,
    ) -> <N::SlotChildren as SlotKindTrait<ForestTokenAddr, DynNodeList>>::Iter<'_> {
        self.child_nodes.iter()
    }

    /// If the node has only one slot, returns it.
    #[inline]
    pub fn single_slot(&self) -> Option<&DynNodeList> {
        self.child_nodes.single_slot()
    }
}

/// A helper type for control flow node such as "if" node and "for" node.
#[derive(Debug)]
pub struct ControlNode<C> {
    /// The backend node token
    ///
    /// It is auto-managed by the `#[component]` .
    /// Do not touch unless you know how it works exactly.
    pub forest_token: tree::ForestToken,
    /// The content nodes of the control node.
    pub content: C,
}

impl<C> ControlNode<C> {
    /// Create a control node.
    #[inline(always)]
    pub fn new(forest_token: tree::ForestToken, content: C) -> Self {
        Self {
            forest_token,
            content,
        }
    }
}

/// A helper type for "if" and "match" node.
pub struct Branch {
    /// The current branch index.
    pub cur: usize,
    /// Child node list in "if...else" or "match" node.
    pub children: DynNodeList,
}

/// A helper trait for managing slot list and slot content.
///
/// It is auto-managed by the `#[component]` .
/// Do not touch unless you know how it works exactly.
pub trait SlotKindTrait<K, C>: Default {
    /// The updater type.
    type Update<'a>: SlotKindUpdateTrait<'a, K, C>
    where
        Self: 'a,
        K: 'a,
        C: 'a;

    /// The iterator type.
    type Iter<'a>: Iterator<Item = &'a C>
    where
        Self: 'a,
        C: 'a;

    /// Whether the slot may update after created
    fn may_update(&self) -> bool;

    /// Add a slot with the slot content.
    #[doc(hidden)]
    fn add(&mut self, k: K, c: C) -> Result<(), Error>;

    /// Remove a slot and return the slot content.
    #[doc(hidden)]
    fn remove(&mut self, k: K) -> Result<C, Error>;

    /// Get a reference of the slot content.
    #[doc(hidden)]
    fn get(&self, k: K) -> Result<&C, Error>;

    /// Get a mutable reference of the slot content.
    #[doc(hidden)]
    fn get_mut(&mut self, k: K) -> Result<&mut C, Error>;

    /// Start an update for all slots.
    #[doc(hidden)]
    fn update<'a>(&'a mut self) -> Self::Update<'a>;

    /// Iterator over all slots.
    fn iter<'a>(&'a self) -> Self::Iter<'a>;

    /// If there is only one slot, returns it.
    fn single_slot(&self) -> Option<&C>;
}

/// A helper trait for a group of slot list updates.
pub trait SlotKindUpdateTrait<'a, K: 'a, C: 'a> {
    /// Add a slot with the slot content.
    #[doc(hidden)]
    fn add(&mut self, k: K, c: C) -> Result<(), Error>;

    /// Reuse a slot, returning it.
    #[doc(hidden)]
    fn reuse(&mut self, k: K) -> Result<&mut C, Error>;

    /// Finish update, handling unused items
    #[doc(hidden)]
    fn finish(self, remove_item_fn: impl FnMut(C) -> Result<(), Error>) -> Result<(), Error>;
}

/// A slot list that is always empty.
#[derive(Debug)]
pub struct NoneSlot<K, C> {
    phantom: PhantomData<(K, C)>,
}

impl<K, C> Default for NoneSlot<K, C> {
    #[inline]
    fn default() -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

impl<K, C> SlotKindTrait<K, C> for NoneSlot<K, C> {
    type Update<'a> = NoneSlotUpdate<'a, K, C> where K: 'a, C: 'a;
    type Iter<'a> = std::iter::Empty<&'a C> where K: 'a, C: 'a;

    #[inline(always)]
    fn may_update(&self) -> bool {
        false
    }

    #[inline]
    fn add(&mut self, _: K, _: C) -> Result<(), Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn remove(&mut self, _: K) -> Result<C, Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn get(&self, _: K) -> Result<&C, Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn get_mut(&mut self, _: K) -> Result<&mut C, Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn update<'a>(&'a mut self) -> Self::Update<'a> {
        NoneSlotUpdate {
            phantom: PhantomData,
        }
    }

    #[inline]
    fn iter(&self) -> Self::Iter<'_> {
        std::iter::empty()
    }

    #[inline]
    fn single_slot(&self) -> Option<&C> {
        None
    }
}

#[doc(hidden)]
pub struct NoneSlotUpdate<'a, K, C> {
    phantom: PhantomData<&'a (K, C)>,
}

impl<'a, K: 'a, C: 'a> SlotKindUpdateTrait<'a, K, C> for NoneSlotUpdate<'a, K, C> {
    #[inline]
    fn add(&mut self, _: K, _: C) -> Result<(), Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn reuse(&mut self, _: K) -> Result<&mut C, Error> {
        Err(Error::ListChangeWrong)
    }

    #[inline]
    fn finish(self, _: impl FnMut(C) -> Result<(), Error>) -> Result<(), Error> {
        Ok(())
    }
}

/// A slot list that always contains a single slot.
///
/// It is auto-managed by the `#[component]` .
/// Do not touch unless you know how it works exactly.
#[derive(Debug)]
pub struct StaticSingleSlot<K, C> {
    kc: Option<C>,
    phantom: PhantomData<K>,
}

impl<K, C> Default for StaticSingleSlot<K, C> {
    #[inline]
    fn default() -> Self
    where
        Self: Sized,
    {
        Self {
            kc: None,
            phantom: PhantomData,
        }
    }
}

impl<K, C> SlotKindTrait<K, C> for StaticSingleSlot<K, C> {
    type Update<'a> = StaticSingleSlotUpdate<'a, K, C> where K: 'a, C: 'a;
    type Iter<'a> = std::option::IntoIter<&'a C> where K: 'a, C: 'a;

    #[inline(always)]
    fn may_update(&self) -> bool {
        false
    }

    #[inline(always)]
    fn add(&mut self, _: K, c: C) -> Result<(), Error> {
        if self.kc.is_some() {
            return Err(Error::ListChangeWrong);
        }
        self.kc = Some(c);
        Ok(())
    }

    #[inline(always)]
    fn remove(&mut self, _: K) -> Result<C, Error> {
        if self.kc.is_none() {
            return Err(Error::ListChangeWrong);
        }
        match self.kc.take() {
            Some(c) => Ok(c),
            None => Err(Error::ListChangeWrong),
        }
    }

    #[inline]
    fn get(&self, _: K) -> Result<&C, Error> {
        self.kc.as_ref().ok_or(Error::ListChangeWrong)
    }

    #[inline]
    fn get_mut(&mut self, _: K) -> Result<&mut C, Error> {
        self.kc.as_mut().ok_or(Error::ListChangeWrong)
    }

    #[inline]
    fn update<'a>(&'a mut self) -> Self::Update<'a> {
        StaticSingleSlotUpdate {
            s: self,
            visited: false,
        }
    }

    #[inline]
    fn iter<'a>(&'a self) -> Self::Iter<'a> {
        self.kc.as_ref().into_iter()
    }

    #[inline]
    fn single_slot(&self) -> Option<&C> {
        self.kc.as_ref()
    }
}

#[doc(hidden)]
pub struct StaticSingleSlotUpdate<'a, K, C> {
    s: &'a mut StaticSingleSlot<K, C>,
    visited: bool,
}

impl<'a, K, C> SlotKindUpdateTrait<'a, K, C> for StaticSingleSlotUpdate<'a, K, C> {
    #[inline]
    fn add(&mut self, k: K, c: C) -> Result<(), Error> {
        let ret = self.s.add(k, c);
        if ret.is_ok() {
            self.visited = true;
        }
        ret
    }

    #[inline]
    fn reuse(&mut self, k: K) -> Result<&mut C, Error> {
        let ret = self.s.get_mut(k);
        if ret.is_ok() {
            self.visited = true;
        }
        ret
    }

    #[inline]
    fn finish(self, mut remove_item_fn: impl FnMut(C) -> Result<(), Error>) -> Result<(), Error> {
        if !self.visited {
            if let Some(c) = self.s.kc.take() {
                return remove_item_fn(c);
            }
        }
        Ok(())
    }
}

/// A slot list that can contain any number of slots.
///
/// It is auto-managed by the `#[component]` .
/// Do not touch unless you know how it works exactly.
#[derive(Debug)]
pub struct DynamicSlot<K, C> {
    slots: HashMap<K, C>,
}

impl<K, C> Default for DynamicSlot<K, C> {
    #[inline]
    fn default() -> Self
    where
        Self: Sized,
    {
        Self {
            slots: HashMap::new(),
        }
    }
}

impl<K: Hash + Eq, C> SlotKindTrait<K, C> for DynamicSlot<K, C> {
    type Update<'a> = DynamicSlotUpdate<'a, K, C> where K: 'a, C: 'a;
    type Iter<'a> = std::collections::hash_map::Values<'a, K, C> where K: 'a, C: 'a;

    #[inline(always)]
    fn may_update(&self) -> bool {
        true
    }

    #[inline]
    fn add(&mut self, k: K, v: C) -> Result<(), Error> {
        self.slots.insert(k, v);
        Ok(())
    }

    #[inline]
    fn remove(&mut self, k: K) -> Result<C, Error> {
        self.slots.remove(&k).ok_or(Error::ListChangeWrong)
    }

    #[inline]
    fn get(&self, k: K) -> Result<&C, Error> {
        self.slots.get(&k).ok_or(Error::ListChangeWrong)
    }

    #[inline]
    fn get_mut(&mut self, k: K) -> Result<&mut C, Error> {
        self.slots.get_mut(&k).ok_or(Error::ListChangeWrong)
    }

    #[inline]
    fn update(&mut self) -> Self::Update<'_> {
        DynamicSlotUpdate {
            cur_map: HashMap::with_capacity(self.slots.len()),
            old: self,
        }
    }

    #[inline]
    fn iter(&self) -> Self::Iter<'_> {
        self.slots.values()
    }

    #[inline]
    fn single_slot(&self) -> Option<&C> {
        match self.slots.len() {
            1 => self.slots.values().next(),
            _ => None,
        }
    }
}

#[doc(hidden)]
pub struct DynamicSlotUpdate<'a, K, C> {
    cur_map: HashMap<K, C>,
    old: &'a mut DynamicSlot<K, C>,
}

impl<'a, K: Hash + Eq, C> SlotKindUpdateTrait<'a, K, C> for DynamicSlotUpdate<'a, K, C> {
    #[inline]
    fn add(&mut self, k: K, v: C) -> Result<(), Error> {
        self.cur_map.insert(k, v);
        Ok(())
    }

    #[inline]
    fn reuse(&mut self, k: K) -> Result<&mut C, Error> {
        let c = self.old.slots.remove(&k).ok_or(Error::ListChangeWrong)?;
        let ret = self.cur_map.entry(k).or_insert(c);
        Ok(ret)
    }

    #[inline]
    fn finish(self, mut item_fn: impl FnMut(C) -> Result<(), Error>) -> Result<(), Error> {
        let r = std::mem::replace(&mut self.old.slots, self.cur_map);
        for (_, c) in r {
            item_fn(c)?;
        }
        Ok(())
    }
}

/// A helper type for slot changes
///
/// It is auto-managed by the `#[component]` .
/// Do not touch unless you know how it works exactly.
#[derive(Debug, Clone, PartialEq)]
pub enum SlotChange<N, M, T> {
    /// The slot is not changed.
    Unchanged(N, M, T),
    /// The data of the slot may have changed.
    DataChanged(N, M, T),
    /// The slot is added.
    Added(N, M, T),
    /// The slot is removed.
    Removed(M),
}