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
use std::{
    hash::{Hash, Hasher},
    marker::PhantomData,
    ptr::NonNull,
    rc::Rc,
};

use super::{storage::Storage, QueryHasher};

/// Query parameters should be hashable and clonable.
///
/// The hash infrastructure is part of this trait itself, because
/// it's not unlikely you want to hash the elements you pass into
/// queries in a different way than they are hashed by default.
pub trait QueryParameter: Sized + Clone + 'static {
    fn hash_stable(&self, hasher: &mut QueryHasher);

    fn get_clone<'cx>() -> TypeErasedCloneFn<'cx>
    where
        Self: 'cx,
    {
        fn clone<'a, T: QueryParameter>(
            this: TypeErasedQueryParam,
            storage: &'a Storage,
        ) -> TypeErasedQueryParam<'a> {
            // safety: T == Self, this clone function clones self
            let this_ref = unsafe { this.get_ref::<T>() };
            let new_this = this_ref.clone();
            let new_ref = storage.alloc(new_this);
            TypeErasedQueryParam::new(new_ref)
        }

        clone::<Self>
    }
}

impl<T> QueryParameter for Option<T>
where
    T: QueryParameter,
{
    fn hash_stable(&self, hasher: &mut QueryHasher) {
        self.is_some().hash(hasher);
        if let Some(i) = self {
            T::hash_stable(i, hasher);
        }
    }
}
impl<T> QueryParameter for Box<T>
where
    T: QueryParameter,
{
    fn hash_stable(&self, hasher: &mut QueryHasher) {
        T::hash_stable(self, hasher)
    }
}
impl<T> QueryParameter for Rc<T>
where
    T: QueryParameter,
{
    fn hash_stable(&self, hasher: &mut QueryHasher) {
        T::hash_stable(self, hasher)
    }
}
impl QueryParameter for () {
    fn hash_stable(&self, _hasher: &mut QueryHasher) {}
}

type TypeErasedCloneFn<'cx> =
    for<'a> fn(this: TypeErasedQueryParam<'cx>, &'a Storage) -> TypeErasedQueryParam<'a>;

#[derive(Copy, Clone)]
pub struct TypeErasedQueryParam<'cx> {
    ptr: NonNull<()>,
    clone: TypeErasedCloneFn<'cx>,
    phantom: PhantomData<&'cx ()>,
}

impl<'cx> TypeErasedQueryParam<'cx> {
    pub fn new<Q: QueryParameter>(inp: &'cx Q) -> Self {
        TypeErasedQueryParam {
            ptr: std::ptr::NonNull::from(inp).cast(),
            clone: Q::get_clone(),
            phantom: PhantomData,
        }
    }

    pub fn get_ptr(&self) -> NonNull<()> {
        self.ptr
    }

    /// Convert this pointer to a query parameter,
    /// into a reference to a concrete query parameter
    ///
    /// # Safety
    /// unsound unless T is the original type of this query parameter
    pub unsafe fn get_ref<T>(&self) -> &'cx T {
        &*self.ptr.as_ptr().cast()
    }

    pub fn deep_clone<'a>(&self, storage: &'a Storage) -> TypeErasedQueryParam<'a> {
        (self.clone)(*self, storage)
    }
}

macro_rules! count {
    () => (0usize);
    ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
}

macro_rules! replace_expr {
    ($_t:tt $sub:expr) => {
        $sub
    };
}

macro_rules! map {
    (let $name: ident; $e: expr; $tup: ident; $($items: tt)*) => {
        $(
            let $name = replace_expr!($items &$tup.${index()});
            $e;
        )*
    };
}

macro_rules! impl_query_parameter {
    () => {};
    ($first: ident $($rest: ident)*) => {
        impl<$first: QueryParameter, $($rest: QueryParameter),*> QueryParameter for ($first, $($rest),*) {
            fn hash_stable(&self, hasher: &mut $crate::QueryHasher) {
                hasher.write_usize(count!($first $($rest)*));

                map!(let x; x.hash_stable(hasher); self; $first $($rest)*);
            }
        }

        impl_query_parameter!($($rest)*);
    };
}

impl_query_parameter!(
    T1 T2 T3 T4 T5 T6
);

macro_rules! impl_integers {
    ($($ty: ty => $name: ident),* $(,)?) => {
        $(
            impl QueryParameter for $ty {
                fn hash_stable(&self, hasher: &mut $crate::QueryHasher) {
                    hasher.$name(*self);
                }
            }
        )*
    };
}

impl_integers!(
    usize => write_usize,
    isize => write_isize,

    u64 => write_u64,
    u32 => write_u32,
    u16 => write_u16,
    u8 => write_u8,

    i64 => write_i64,
    i32 => write_i32,
    i16 => write_i16,
    i8 => write_i8,
);

impl QueryParameter for bool {
    fn hash_stable(&self, hasher: &mut QueryHasher) {
        hasher.write_u8(*self as u8);
    }
}