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::{UserdataMethods, UserdataRegistry};
use crate::callback::{Arguments, CallbackReturn};
use crate::error::Error;
use crate::lua::LuaRef;

impl<T: 'static> UserdataMethods<T> for UserdataRegistry<'_, T> {
    fn add_method<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_member_callback(
            name,
            Box::new(UserdataMethodCallback {
                method,
                expected_results: None,
                _marker: PhantomData,
            }),
        );
    }

    fn add_method_mut<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_member_callback(
            name,
            Box::new(UserdataMethodMutCallback {
                method: method.into(),
                expected_results: None,
                _marker: PhantomData,
            }),
        );
    }

    fn add_function<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_member_callback(
            name,
            Box::new(UserdataFunctionCallback {
                function,
                expected_results: None,
            }),
        );
    }

    fn add_function_mut<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_member_callback(
            name,
            Box::new(UserdataFunctionMutCallback {
                function: function.into(),
                expected_results: None,
            }),
        );
    }

    fn add_meta_method<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_metatable_callback(
            name,
            Box::new(UserdataMethodCallback {
                method,
                expected_results: None,
                _marker: PhantomData,
            }),
        );
    }

    fn add_meta_method_mut<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_metatable_callback(
            name,
            Box::new(UserdataMethodMutCallback {
                method: method.into(),
                expected_results: None,
                _marker: PhantomData,
            }),
        );
    }

    fn add_meta_function<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_metatable_callback(
            name,
            Box::new(UserdataFunctionCallback {
                function,
                expected_results: None,
            }),
        );
    }

    fn add_meta_function_mut<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_metatable_callback(
            name,
            Box::new(UserdataFunctionMutCallback {
                function: function.into(),
                expected_results: None,
            }),
        );
    }
}