Skip to main content

sark_core/route/
raw.rs

1//! Trusted generated route dispatch.
2
3use std::mem;
4
5use crate::route::table;
6
7#[derive(Clone, Copy, PartialEq, Eq)]
8pub struct PathCapture {
9    start: usize,
10    end: usize,
11}
12
13impl PathCapture {
14    /// Creates a path capture proved by the generated route machine.
15    ///
16    /// # Safety
17    /// The range must be the matched segment in its paired target.
18    pub const unsafe fn new(start: usize, end: usize) -> Self {
19        Self { start, end }
20    }
21
22    pub const fn start(self) -> usize {
23        self.start
24    }
25
26    pub const fn end(self) -> usize {
27        self.end
28    }
29
30    #[doc(hidden)]
31    pub const fn is_schema(expected: &[&[u8]], actual: &[&[u8]]) -> bool {
32        if expected.len() != actual.len() {
33            return false;
34        }
35        let mut field = 0;
36        while field < expected.len() {
37            if expected[field].len() != actual[field].len() {
38                return false;
39            }
40            let mut byte = 0;
41            while byte < expected[field].len() {
42                if expected[field][byte] != actual[field][byte] {
43                    return false;
44                }
45                byte += 1;
46            }
47            field += 1;
48        }
49        true
50    }
51}
52
53const _: () = {
54    assert!(mem::size_of::<PathCapture>() == 2 * mem::size_of::<usize>());
55    assert!(!mem::needs_drop::<PathCapture>());
56};
57
58/// Generated route dispatch trusted by typed storage.
59///
60/// # Safety
61/// Visitors must receive only the owning slot; `each` requires `P::INDEX == index`.
62pub unsafe trait Table<S> {
63    type Set;
64
65    const LEN: usize;
66    const METHODS: u8;
67
68    fn find<'a, V>(key: table::Key<'a>, visit: V) -> Option<V::Output>
69    where
70        V: table::Visit<S, Self::Set>;
71
72    fn each<V>(index: u32, visit: V) -> Option<V::Output>
73    where
74        V: table::Each<S, Self::Set>;
75}