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
use std::{fmt, ops::Deref};

/// Any function pointer that can be converted to and from usize
pub trait Fn<T> {
    fn id(&self) -> usize;
    fn from_id(id: usize) -> T;
}

macro_rules! impl_types {
    ( $($ty:ident),* $(,)? ) => {
        impl<Res, $($ty,)*> Fn<Self> for fn($($ty,)*) -> Res
        {
            fn from_id(id: usize) -> Self {
                unsafe { std::mem::transmute(id) }
            }

            fn id(&self) -> usize {
                *self as usize
            }
        }
    };
}

// impl Fn<T> for most fn pointers
impl_types!();
impl_types!(T1);
impl_types!(T1, T2);
impl_types!(T1, T2, T3);
impl_types!(T1, T2, T3, T4);
impl_types!(T1, T2, T3, T4, T5);
impl_types!(T1, T2, T3, T4, T5, T6);
impl_types!(T1, T2, T3, T4, T5, T6, T7);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
impl_types!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);

/// Reference to a function that can be sent to another process
#[derive(Copy, Clone, Debug)]
pub struct FuncRef<T>(T);

impl<T> FuncRef<T>
where
    T: Fn<T> + Copy,
{
    pub fn new(func: T) -> Self {
        FuncRef(func)
    }

    pub fn get(&self) -> T {
        self.0
    }
}

impl<T> Deref for FuncRef<T>
where
    T: Fn<T>,
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> serde::Serialize for FuncRef<T>
where
    T: Fn<T>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u64(self.id() as u64)
    }
}

impl<'de, T> serde::Deserialize<'de> for FuncRef<T>
where
    T: Fn<T>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct U64Visitor;
        impl<'de> serde::de::Visitor<'de> for U64Visitor {
            type Value = u64;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("u64 representing a function pointer")
            }

            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(v)
            }
        }

        let id = deserializer.deserialize_u64(U64Visitor)? as usize;
        Ok(FuncRef(T::from_id(id)))
    }
}