Remove FromLua impl for UserData+Clone

This commit is contained in:
Alex Orlenko 2022-12-19 23:05:17 +00:00
parent a13c188de3
commit 61c285746c
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
3 changed files with 24 additions and 18 deletions

View File

@ -1,7 +1,9 @@
use std::f32;
use std::iter::FromIterator;
use mlua::{chunk, Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic};
use mlua::{
chunk, FromLua, Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Value, Variadic,
};
fn main() -> Result<()> {
// You can create a new Lua state with `Lua::new()`. This loads the default Lua std library
@ -151,6 +153,16 @@ fn main() -> Result<()> {
#[derive(Copy, Clone)]
struct Vec2(f32, f32);
// We can implement `FromLua` trait for our `Vec2` to return a copy
impl<'lua> FromLua<'lua> for Vec2 {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
match value {
Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
}
}
impl UserData for Vec2 {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method("magnitude", |_, vec, ()| {

View File

@ -194,21 +194,6 @@ impl<'lua, T: 'static + MaybeSend + UserData> IntoLua<'lua> for T {
}
}
// TODO: Remove
impl<'lua, T: 'static + UserData + Clone> FromLua<'lua> for T {
#[inline]
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<T> {
match value {
Value::UserData(ud) => Ok(ud.borrow::<T>()?.clone()),
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "userdata",
message: None,
}),
}
}
}
impl<'lua> IntoLua<'lua> for Error {
#[inline]
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {

View File

@ -12,8 +12,8 @@ use std::{cell::RefCell, rc::Rc};
use std::sync::atomic::{AtomicI64, Ordering};
use mlua::{
AnyUserData, Error, ExternalError, Function, Lua, MetaMethod, Nil, Result, String, UserData,
UserDataFields, UserDataMethods, Value,
AnyUserData, Error, ExternalError, FromLua, Function, Lua, MetaMethod, Nil, Result, String,
UserData, UserDataFields, UserDataMethods, Value,
};
#[test]
@ -95,6 +95,15 @@ fn test_metamethods() -> Result<()> {
#[derive(Copy, Clone)]
struct MyUserData(i64);
impl<'lua> FromLua<'lua> for MyUserData {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
match value {
Value::UserData(ud) => Ok(ud.borrow::<Self>()?.clone()),
_ => unreachable!(),
}
}
}
impl UserData for MyUserData {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method("get", |_, data, ()| Ok(data.0));