luau 0.732.0

Safe lifetime-bound Rust embedding API for the Luau runtime
use core::marker::PhantomData;

use super::callback::{
    UserdataFunctionCallback, UserdataFunctionMutCallback, UserdataMethodCallback,
    UserdataMethodMutCallback,
};
use super::{UserdataFields, UserdataRegistry};
use crate::callback::{Arguments, CallbackReturn};
use crate::error::Error;
use crate::lua::LuaRef;
use crate::value::{IntoLua, Value};

impl<T: 'static> UserdataFields<T> for UserdataRegistry<'_, T> {
    fn add_field<V>(&mut self, name: impl AsRef<[u8]>, value: V)
    where
        V: for<'lua> IntoLua<'lua>,
    {
        self.set_member(name, value);
    }

    fn add_field_with<F>(&mut self, name: impl AsRef<[u8]>, field: F)
    where
        F: for<'lua> FnOnce(LuaRef<'lua>) -> Result<Value<'lua>, Error> + 'static,
    {
        self.set_member_value_with(name, field);
    }

    fn add_field_method_get<M>(&mut self, name: impl AsRef<[u8]>, method: M)
    where
        M: for<'call> Fn(
                LuaRef<'call>,
                &T,
                Arguments<'call>,
            ) -> Result<CallbackReturn<'call>, Error>
            + 'static,
    {
        self.set_getter_callback(
            name,
            Box::new(UserdataMethodCallback {
                method,
                expected_results: Some(1),
                _marker: PhantomData,
            }),
        );
    }

    fn add_field_method_set<M>(&mut self, name: impl AsRef<[u8]>, method: M)
    where
        M: for<'call> FnMut(
                LuaRef<'call>,
                &mut T,
                Arguments<'call>,
            ) -> Result<CallbackReturn<'call>, Error>
            + 'static,
    {
        self.set_setter_callback(
            name,
            Box::new(UserdataMethodMutCallback {
                method: method.into(),
                expected_results: Some(0),
                _marker: PhantomData,
            }),
        );
    }

    fn add_field_function_get<F>(&mut self, name: impl AsRef<[u8]>, function: F)
    where
        F: for<'call> Fn(LuaRef<'call>, Arguments<'call>) -> Result<CallbackReturn<'call>, Error>
            + 'static,
    {
        self.set_getter_callback(
            name,
            Box::new(UserdataFunctionCallback {
                function,
                expected_results: Some(1),
            }),
        );
    }

    fn add_field_function_set<F>(&mut self, name: impl AsRef<[u8]>, function: F)
    where
        F: for<'call> FnMut(
                LuaRef<'call>,
                Arguments<'call>,
            ) -> Result<CallbackReturn<'call>, Error>
            + 'static,
    {
        self.set_setter_callback(
            name,
            Box::new(UserdataFunctionMutCallback {
                function: function.into(),
                expected_results: Some(0),
            }),
        );
    }

    fn add_meta_field<V>(&mut self, name: impl AsRef<[u8]>, value: V)
    where
        V: for<'lua> IntoLua<'lua>,
    {
        self.set_metatable_value(name, value);
    }

    fn add_meta_field_with<F>(&mut self, name: impl AsRef<[u8]>, field: F)
    where
        F: for<'lua> FnOnce(LuaRef<'lua>) -> Result<Value<'lua>, Error> + 'static,
    {
        self.set_metatable_value_with(name, field);
    }
}