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
use crate::{
    FnPtr, FromValue, OwnedRef, RawOwnedRef, Shared, ToValue, UnsafeFromValue, Value, ValueError,
};

value_types!(crate::FN_PTR_TYPE, FnPtr => FnPtr, &FnPtr, Shared<FnPtr>, OwnedRef<FnPtr>);

impl FromValue for FnPtr {
    fn from_value(value: Value) -> Result<Self, ValueError> {
        Ok(value.into_fn_ptr()?.take()?)
    }
}

impl FromValue for Shared<FnPtr> {
    fn from_value(value: Value) -> Result<Self, ValueError> {
        Ok(value.into_fn_ptr()?)
    }
}

impl FromValue for OwnedRef<FnPtr> {
    fn from_value(value: Value) -> Result<Self, ValueError> {
        Ok(value.into_fn_ptr()?.owned_ref()?)
    }
}

impl UnsafeFromValue for &FnPtr {
    type Output = *const FnPtr;
    type Guard = RawOwnedRef;

    unsafe fn unsafe_from_value(value: Value) -> Result<(Self::Output, Self::Guard), ValueError> {
        let fn_ptr = value.into_fn_ptr()?;
        let (fn_ptr, guard) = OwnedRef::into_raw(fn_ptr.owned_ref()?);
        Ok((fn_ptr, guard))
    }

    unsafe fn to_arg(output: Self::Output) -> Self {
        &*output
    }
}

impl ToValue for FnPtr {
    fn to_value(self) -> Result<Value, ValueError> {
        Ok(Value::FnPtr(Shared::new(self)))
    }
}

impl ToValue for Shared<FnPtr> {
    fn to_value(self) -> Result<Value, ValueError> {
        Ok(Value::FnPtr(self))
    }
}