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
/// Implement the value trait for an external type.
///
/// This is required to support the external type as a type argument in a
/// registered function.
///
/// This will be **deprecated** once (or if) [specialization] lands.
///
/// [specialization]: https://github.com/rust-lang/rust/issues/31844
#[macro_export]
macro_rules! decl_external {
    ($external:ty) => {
        impl $crate::ReflectValueType for $external {
            fn value_type() -> $crate::ValueType {
                $crate::ValueType::External(std::any::TypeId::of::<$external>())
            }

            fn value_type_info() -> $crate::ValueTypeInfo {
                $crate::ValueTypeInfo::External(std::any::type_name::<$external>())
            }
        }

        impl<'a> $crate::ReflectValueType for &'a $external {
            fn value_type() -> $crate::ValueType {
                $crate::ValueType::External(std::any::TypeId::of::<$external>())
            }

            fn value_type_info() -> $crate::ValueTypeInfo {
                $crate::ValueTypeInfo::External(std::any::type_name::<$external>())
            }
        }

        impl<'a> $crate::ReflectValueType for &'a mut $external {
            fn value_type() -> $crate::ValueType {
                $crate::ValueType::External(std::any::TypeId::of::<$external>())
            }

            fn value_type_info() -> $crate::ValueTypeInfo {
                $crate::ValueTypeInfo::External(std::any::type_name::<$external>())
            }
        }

        impl $crate::ToValue for $external {
            fn to_value(self, vm: &mut $crate::Vm) -> Result<$crate::ValuePtr, $crate::VmError> {
                Ok(vm.external_allocate(self))
            }
        }

        impl<'a> $crate::UnsafeToValue for &'a $external {
            unsafe fn unsafe_to_value(
                self,
                vm: &mut $crate::Vm,
            ) -> Result<$crate::ValuePtr, $crate::VmError> {
                Ok(vm.external_allocate_ptr(self))
            }
        }

        impl<'a> $crate::UnsafeToValue for &'a mut $external {
            unsafe fn unsafe_to_value(
                self,
                vm: &mut $crate::Vm,
            ) -> Result<$crate::ValuePtr, $crate::VmError> {
                Ok(vm.external_allocate_mut_ptr(self))
            }
        }

        impl $crate::FromValue for $external {
            fn from_value(
                value: $crate::ValuePtr,
                vm: &mut $crate::Vm,
            ) -> Result<Self, $crate::VmError> {
                let slot = value.into_external(vm)?;
                vm.external_take::<$external>(slot)
            }
        }

        impl<'a> $crate::UnsafeFromValue for &'a $external {
            type Output = *const $external;
            type Guard = $crate::RawRefGuard;

            unsafe fn unsafe_from_value(
                value: $crate::ValuePtr,
                vm: &mut $crate::Vm,
            ) -> Result<(Self::Output, Self::Guard), $crate::VmError> {
                let slot = value.into_external(vm)?;
                Ok($crate::Ref::unsafe_into_ref(
                    vm.external_ref::<$external>(slot)?,
                ))
            }

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

        impl<'a> $crate::UnsafeFromValue for &'a mut $external {
            type Output = *mut $external;
            type Guard = $crate::RawMutGuard;

            unsafe fn unsafe_from_value(
                value: $crate::ValuePtr,
                vm: &mut $crate::Vm,
            ) -> Result<(Self::Output, Self::Guard), $crate::VmError> {
                let slot = value.into_external(vm)?;
                Ok($crate::Mut::unsafe_into_mut(
                    vm.external_mut::<$external>(slot)?,
                ))
            }

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