Skip to content

Commit

Permalink
Merge pull request #83 from lenscas/chore/update_dependencies
Browse files Browse the repository at this point in the history
Chore/update dependencies
  • Loading branch information
lenscas authored Dec 14, 2023
2 parents 48335f1 + 96d559a commit d104a52
Show file tree
Hide file tree
Showing 33 changed files with 811 additions and 475 deletions.
723 changes: 439 additions & 284 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ repository = "https://github.com/lenscas/tealr/"
version = "0.9.0-alpha4"

[workspace]
members = [
"tealr_derive",
]
members = ["tealr_derive"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
Expand Down Expand Up @@ -48,13 +46,13 @@ rlua_system-lua53 = ["rlua/system-lua53"]
rlua_system-lua54 = ["rlua/system-lua54"]

[dependencies]
bstr = {version = "0.2", default_features = false, features = ["std"]}
bstr = { version = "0.2", default_features = false, features = ["std"] }
itertools = "0.10.3"
mlua = {version = "0.8.1", optional = true, default_features = false}
rlua = {version = "0.19.2", optional = true, default_features = false}
serde = {version = "1.0.136", features = ["derive"]}
mlua = { version = "0.9.2", optional = true, default_features = false }
rlua = { version = "0.19.7", optional = true, default_features = false }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.91"
tealr_derive = {version = "0.9.0-alpha4", optional = true, path = "./tealr_derive"}
tealr_derive = { version = "0.9.0-alpha4", optional = true, path = "./tealr_derive" }

# Rlua tests
[[test]]
Expand Down Expand Up @@ -207,5 +205,13 @@ path = "examples/mlua/named_parameters.rs"
required-features = ["mlua"]

[package.metadata.docs.rs]
features = ["mlua", "rlua", "rlua_builtin-lua54", "mlua_lua54", "mlua_vendored", "mlua_async", "mlua_serialize"]
features = [
"mlua",
"rlua",
"rlua_builtin-lua54",
"mlua_lua54",
"mlua_vendored",
"mlua_async",
"mlua_serialize",
]
rustdoc-args = ["--cfg", "docsrs"]
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ Mlua:
use tealr::ToTypename;
#[derive(Clone, tealr::mlu::UserData, ToTypename)]
struct ExampleMlua {}
impl<'lua> FromLua<'lua> for ExampleMlua {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}

impl tealr::mlu::TealData for ExampleMlua {
//implement your methods/functions
fn add_methods<'lua, T: tealr::mlu::TealDataMethods<'lua, Self>>(methods: &mut T) {
Expand Down Expand Up @@ -136,7 +149,7 @@ To go along with typed functions, tealr also comes with a way to mimic generics.
In the following example we take a generic function and call it, returning whatever it returned back to lua. Thanks to the use of generics, it i clear that the return type of the method is equal to the return type of the lambda. If `lua::Value` was used instead this was not clear.

```rust ignore
use mlua::ToLua;
use mlua::IntoLua;
use tealr::{
create_generic_mlua,
mlu::{mlua::FromLua, TealData, TealDataMethods, TypedFunction,UserData},
Expand All @@ -147,6 +160,7 @@ create_generic_mlua!(X);

#[derive(Clone, UserData, ToTypename)]
struct Example {}

impl TealData for Example {
fn add_methods<'lua, T: TealDataMethods<'lua, Self>>(methods: &mut T) {
methods.add_method(
Expand All @@ -157,6 +171,21 @@ impl TealData for Example {
);
}
}

impl<'lua> FromLua<'lua> for Example {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}


```

For rlua, all you have to do is replace `mlua` for `rlua`
Expand Down Expand Up @@ -201,7 +230,7 @@ let compiler = embed_compiler!("v0.13.1");
{
let code = compiler("example/basic_teal_file");
let lua = tealr::mlu::mlua::Lua::new();
let res: u8 = lua.load(&code).set_name("embedded_compiler")?.eval()?;
let res: u8 = lua.load(&code).set_name("embedded_compiler").eval()?;
};
Ok::<(), Box<dyn std::error::Error>>(())
```
Expand Down
2 changes: 1 addition & 1 deletion examples/mlua/compile_inline_teal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ return add(1,2)

let result: String = lua
.load(code)
.set_name("compile inline teal example")?
.set_name("compile inline teal example")
.eval()?;
println!("output:{}", result);

Expand Down
16 changes: 14 additions & 2 deletions examples/mlua/derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tealr::{
mlu::{
mlua::{Lua, Result},
mlua::{FromLua, Lua, Result},
TealData, TealDataMethods, UserData,
},
ToTypename, TypeWalker,
Expand All @@ -17,6 +17,18 @@ use tealr::{
//The clone is only needed because one of the example functions has it as a parameter
#[derive(Clone, UserData, ToTypename)]
struct Example {}
impl<'lua> FromLua<'lua> for Example {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}

//now, implement TealData. This tells rlua what methods are available and tealr what the types are
impl TealData for Example {
Expand Down Expand Up @@ -55,6 +67,6 @@ print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?")?.eval()?;
lua.load(code).set_name("test?").eval()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/mlua/embed_teal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() -> mlua::Result<()> {
let compiler = embed_compiler!("v0.9.0");
let lua = mlua::Lua::new();
let code = compiler("examples/mlua/basic_teal_file");
let res: u8 = lua.load(&code).set_name("embedded_compiler")?.eval()?;
let res: u8 = lua.load(&code).set_name("embedded_compiler").eval()?;
println!("got:{}", res);
Ok(())
}
16 changes: 14 additions & 2 deletions examples/mlua/manual.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tealr::{
mlu::{
mlua::{Lua, Result, UserData, UserDataMethods},
mlua::{FromLua, Lua, Result, UserData, UserDataMethods},
TealData, TealDataMethods, UserDataWrapper,
},
ToTypename, TypeBody, TypeWalker,
Expand All @@ -13,6 +13,18 @@ use tealr::{
//First, create the struct you want to export to lua.
#[derive(Clone, Copy)]
struct Example(u32);
impl<'lua> FromLua<'lua> for Example {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}

//now, implement TealData. This tells rlua what methods are available and tealr what the types are
impl TealData for Example {
Expand Down Expand Up @@ -90,6 +102,6 @@ print(\"Example field\", test.example_field)
test.example_field = 2
print(\"After modifying\",test.example_field)
";
lua.load(code).set_name("test?")?.eval()?;
lua.load(code).set_name("test?").eval()?;
Ok(())
}
16 changes: 14 additions & 2 deletions examples/mlua/manual_documentation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tealr::{
mlu::{
mlua::{Lua, Result, UserData, UserDataMethods},
mlua::{FromLua, Lua, Result, UserData, UserDataMethods},
TealData, TealDataMethods, UserDataWrapper,
},
ToTypename, TypeBody, TypeWalker,
Expand All @@ -13,6 +13,18 @@ use tealr::{
//First, create the struct you want to export to lua.
#[derive(Clone, Copy)]
struct Example(u32);
impl<'lua> FromLua<'lua> for Example {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}

//now, implement TealData. This tells rlua what methods are available and tealr what the types are
impl TealData for Example {
Expand Down Expand Up @@ -104,6 +116,6 @@ print(test.example_function({}))
print(test.example_function_mut(true))
";

lua.load(code).set_name("test?")?.eval()?;
lua.load(code).set_name("test?").eval()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/mlua/named_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ fn main() -> Result<()> {
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
lua.load(code).set_name("test?")?.eval()?;
lua.load(code).set_name("test?").eval()?;
Ok(())
}
16 changes: 15 additions & 1 deletion examples/mlua/userdata_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use mlua::FromLua;
use tealr::{
mlu::{
mlua::{Lua, Result},
Expand All @@ -17,6 +18,19 @@ struct Example {
float: f32,
}

impl<'lua> FromLua<'lua> for Example {
fn from_lua(value: mlua::prelude::LuaValue<'lua>, _: &'lua Lua) -> Result<Self> {
value
.as_userdata()
.map(|x| x.take())
.unwrap_or(Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Example",
message: None,
}))
}
}

//now, implement TealData. This tells rlua what methods are available and tealr what the types are
impl TealData for Example {
//implement your methods/functions
Expand Down Expand Up @@ -88,6 +102,6 @@ print(\" Calling from global `Example` :\")
print(Example.example_static_field)
print(Example.example_function({}))
";
lua.load(code).set_name("test?")?.eval()?;
lua.load(code).set_name("test?").eval()?;
Ok(())
}
6 changes: 3 additions & 3 deletions src/exported_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl ExportedFunction {
///Creates an ExportedFunction with the given name, Parameters and return value
///```no_run
///# use tealr::ExportedFunction;
///ExportedFunction::new::<(String,String),String,_>(b"concat",false,None);
///ExportedFunction::new::<(String,String),String,_>("concat",false,None);
///```
#[cfg(any(feature = "rlua", feature = "mlua"))]
pub fn new<A: crate::TealMultiValue, R: crate::TealMultiValue, S: AsRef<[u8]>>(
pub fn new<A: crate::TealMultiValue, R: crate::TealMultiValue, S: AsRef<str>>(
name: S,
is_meta_method: bool,
extra_self: Option<Type>,
Expand All @@ -72,7 +72,7 @@ impl ExportedFunction {
let signature = crate::new_type_to_old(type_to_generate, false);
#[allow(deprecated)]
Self {
name: name.as_ref().to_vec().into(),
name: name.as_ref().as_bytes().to_vec().into(),
signature,
is_meta_method,
params,
Expand Down
2 changes: 1 addition & 1 deletion src/mlu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn get_meta_name(name: mlua::MetaMethod) -> Cow<'static, str> {
MetaMethod::Close => Cow::Borrowed("__close"),
#[cfg(feature = "mlua_luau")]
MetaMethod::Iter => Cow::Borrowed("__iter"),
MetaMethod::Custom(x) => Cow::Owned(x),
_ => Cow::Borrowed("unknown"),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/mlu/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ macro_rules! create_generic_mlua {
Ok(value.into())
}
}
impl<'lua> $crate::mlu::mlua::ToLua<'lua> for $type_name<'lua> {
fn to_lua(self, _: &'lua $crate::mlu::mlua::Lua) -> ::std::result::Result<$crate::mlu::mlua::Value<'lua>, $crate::mlu::mlua::Error> {
impl<'lua> $crate::mlu::mlua::IntoLua<'lua> for $type_name<'lua> {
fn into_lua(self, _: &'lua $crate::mlu::mlua::Lua) -> ::std::result::Result<$crate::mlu::mlua::Value<'lua>, $crate::mlu::mlua::Error> {
Ok(self.into())
}
}
Expand All @@ -53,7 +53,7 @@ macro_rules! create_generic_mlua {
UserData(x) => $type_name::UserData(x),
Error(x) => $type_name::Error(x),
#[cfg(feature = "mlua_luau")]
Vector(x,y,z) => $type_name::Vector(x,y,z)
Vector(vec) => $type_name::Vector(vec.x(),vec.y(),vec.z())
}
}
}
Expand All @@ -73,7 +73,7 @@ macro_rules! create_generic_mlua {
UserData(x) => $crate::mlu::mlua::Value::UserData(x),
Error(x) => $crate::mlu::mlua::Value::Error(x),
#[cfg(feature = "mlua_luau")]
Vector(x,y,z) => $crate::mlu::mlua::Value::Vector(x,y,z)
Vector(x,y,z) => $crate::mlu::mlua::Value::Vector($crate::mlu::mlua::Vector::new(x,y,z))
}
}
}
Expand All @@ -98,7 +98,7 @@ macro_rules! create_generic_mlua {
($type_name::Thread(a), $crate::mlu::mlua::Value::Thread(b)) => a == b,
($type_name::UserData(a), $crate::mlu::mlua::Value::UserData(b)) => a == b,
#[cfg(feature = "mlua_luau")]
($type_name::Vector(x,y,z), $crate::mlu::mlua::Value::Vector(x2,y2,z2)) => x == x2 && y == y2 && z == z2,
($type_name::Vector(x,y,z), $crate::mlu::mlua::Value::Vector(vec)) => *x == vec.x() && *y == vec.y() && *z == vec.z(),
_ => false,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/mlu/picker_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ macro_rules! create_union_mlua {
$visibility enum $type_name {
$($sub_types($sub_types) ,)*
}
impl<'lua> $crate::mlu::mlua::ToLua<'lua> for $type_name {
fn to_lua(self, lua: &'lua $crate::mlu::mlua::Lua) -> ::std::result::Result<$crate::mlu::mlua::Value<'lua>, $crate::mlu::mlua::Error> {
impl<'lua> $crate::mlu::mlua::IntoLua<'lua> for $type_name {
fn into_lua(self, lua: &'lua $crate::mlu::mlua::Lua) -> ::std::result::Result<$crate::mlu::mlua::Value<'lua>, $crate::mlu::mlua::Error> {
match self {
$($type_name::$sub_types(x) => x.to_lua(lua),)*
$($type_name::$sub_types(x) => x.into_lua(lua),)*
}
}
}
Expand Down
Loading

0 comments on commit d104a52

Please sign in to comment.