Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ env:
CARGO_TERM_COLOR: always

jobs:
format:
name: format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

- uses: dtolnay/rust-toolchain@stable

- run: cargo fmt --check

test:
# Run the tests in each of the feature combinations.
# This is not an exhaustive test of all possible combinations,
Expand Down
22 changes: 13 additions & 9 deletions examples/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use mlua_extras::{

/// Simple Counter
#[derive(Clone, TypedUserData)]
struct Counter { value: i64 }
struct Counter {
value: i64,
}

#[typed_user_data_impl]
impl Counter {
Expand Down Expand Up @@ -42,11 +44,15 @@ impl Counter {

/// Get the current counter value
#[method]
fn get(&self) -> i64 { self.value }
fn get(&self) -> i64 {
self.value
}

/// Increment the counter
#[method]
fn increment(&mut self) { self.value += 1 }
fn increment(&mut self) {
self.value += 1
}

/// Create a new table
#[method]
Expand All @@ -56,7 +62,9 @@ impl Counter {

/// String representation of the counter
#[metamethod(ToString)]
fn to_string(&self) -> String { format!("Counter({})", self.value) }
fn to_string(&self) -> String {
format!("Counter({})", self.value)
}

// Requires the `async` feature
// Must be accessed from lua code with an entry of `mlua::Chunk::eval_async` or `mlua::Chunk::exec_async`
Expand All @@ -71,11 +79,7 @@ impl Counter {

fn main() -> mlua::Result<()> {
let definitions: Definitions = Definitions::start()
.define(
"macros",
Definition::start()
.register::<Counter>("Counter")
)
.define("macros", Definition::start().register::<Counter>("Counter"))
.finish();

let types_path = PathBuf::from("examples/types");
Expand Down
7 changes: 5 additions & 2 deletions examples/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ fn main() -> mlua::Result<()> {
lua.append_cpath(PathBuf::from("examples").join("?.dll"))?;
lua.append_cpath(PathBuf::from("examples").join("?.lib"))?;

lua.load(r#"
lua.load(
r#"
print(package.path, '\n')
print(package.cpath, '\n')
"#).eval::<()>()?;
"#,
)
.eval::<()>()?;

// Set globals in with shorthand helpers
lua.set_global("key", "value")?;
Expand Down
4 changes: 2 additions & 2 deletions examples/typed.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::path::PathBuf;

use mlua_extras::{
Typed, UserData,
extras::LuaExtras,
mlua::{self, FromLua, Lua, LuaSerdeExt, MetaMethod, Value, Variadic},
typed::{
Type, TypedDataFields, TypedDataMethods, TypedUserData,
generator::{
Definition, DefinitionFileGenerator, Definitions, LuauDefinitionFileGenerator,
},
Type, TypedDataFields, TypedDataMethods, TypedUserData,
},
Typed, UserData,
};
use serde::Deserialize;

Expand Down
6 changes: 3 additions & 3 deletions src/extras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use mlua::{AnyUserData, FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Lua, Table

mod module;

pub use module::{LuaModule, Module, ModuleBuilder, ModuleFields, ModuleMethods, ExtendModule};
pub use module::{ExtendModule, LuaModule, Module, ModuleBuilder, ModuleFields, ModuleMethods};

use crate::MaybeSend;

Expand Down Expand Up @@ -46,7 +46,7 @@ pub trait LuaExtras {
/// - <https://www.lua.org/manual/5.4/manual.html#pdf-package.path>
/// - <https://www.lua.org/manual/5.4/manual.html#pdf-package.searchpath>
fn prepend_paths<S: AsRef<Path>>(&self, paths: impl IntoIterator<Item = S>)
-> mlua::Result<()>;
-> mlua::Result<()>;

/// Append a path tothe `package.path` value
///
Expand Down Expand Up @@ -114,7 +114,7 @@ pub trait LuaExtras {
/// - <https://www.lua.org/manual/5.4/manual.html#pdf-package.cpath>
/// - <https://www.lua.org/manual/5.4/manual.html#pdf-package.searchpath>
fn append_cpaths<S: AsRef<Path>>(&self, paths: impl IntoIterator<Item = S>)
-> mlua::Result<()>;
-> mlua::Result<()>;

/// Set the `package.cpath` value
///
Expand Down
2 changes: 1 addition & 1 deletion src/extras/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait Module: Sized {
fn add_fields<F: ModuleFields>(fields: &mut F) -> mlua::Result<()> {
Ok(())
}

/// Add methods/functions to the module
#[allow(unused_variables)]
fn add_methods<M: ModuleMethods>(methods: &mut M) -> mlua::Result<()> {
Expand Down
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
pub mod ser;

#[cfg(feature="mlua")]
pub mod typed;
#[cfg(feature="mlua")]
#[cfg(feature = "mlua")]
pub mod extras;
#[cfg(feature = "mlua")]
pub mod typed;

#[cfg(feature="mlua")]
#[cfg(feature = "mlua")]
pub use mlua;

#[cfg(feature="macros")]
pub use mlua_extras_derive::{UserData, user_data_impl, Typed, TypedUserData, typed_user_data_impl};
#[cfg(feature = "macros")]
pub use mlua_extras_derive::{
Typed, TypedUserData, UserData, typed_user_data_impl, user_data_impl,
};

#[cfg(feature = "send")]
/// Used by the `send` feature
Expand Down
4 changes: 2 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a, 'b> Serializer for &'a mut LuaSerializer<'b> {
if v.is_nan() {
self.out.push_str("0/0");
} else if v.is_infinite() {
if v.is_sign_positive() {
if v.is_sign_positive() {
self.out.push_str("math.huge");
} else {
self.out.push_str("-math.huge");
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a, 'b> Serializer for &'a mut LuaSerializer<'b> {
if v.is_nan() {
self.out.push_str("0/0");
} else if v.is_infinite() {
if v.is_sign_positive() {
if v.is_sign_positive() {
self.out.push_str("math.huge");
} else {
self.out.push_str("-math.huge");
Expand Down
32 changes: 19 additions & 13 deletions src/typed/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use mlua::{UserDataRef, UserDataRefMut};

use crate::{MaybeSend, typed::IntoDocComment};

use super::{Typed, TypedMultiValue, Type};
use super::{Type, Typed, TypedMultiValue};

mod wrapped;
mod standard;
mod wrapped;

pub use standard::{TypedClass, TypedClassBuilder};
pub use wrapped::WrappedBuilder;
pub use standard::{TypedClassBuilder, TypedClass};

/// Typed variant of [`mlua::UserData`]
pub trait TypedUserData: Sized {
Expand Down Expand Up @@ -136,24 +136,29 @@ pub trait TypedDataMethods<T> {
fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;

/// Adds a param name and doc comment to the next method/function that gets added.
///
///
/// These will be applied to the params in the order they were defined.
fn param(&mut self, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;

/// Adds a param name and doc comment to the next method/function that gets added.
/// Will also add an override type to the param.
///
///
/// These will be applied to the params in the order they were defined.
fn param_as(&mut self, ty: impl Into<Type>, name: impl std::fmt::Display, doc: impl IntoDocComment) -> &mut Self;
fn param_as(
&mut self,
ty: impl Into<Type>,
name: impl std::fmt::Display,
doc: impl IntoDocComment,
) -> &mut Self;

/// Adds a return doc comment to the next method/function that gets added.
///
///
/// These will be applied to the returns in the order they were defined.
fn ret(&mut self, doc: impl IntoDocComment) -> &mut Self;

/// Adds a return doc comment to the next method/function that gets added.
/// Will also add an override type to the return.
///
///
/// These will be applied to the returns in the order they were defined.
fn ret_as(&mut self, ty: impl Into<Type>, doc: impl IntoDocComment) -> &mut Self;

Expand All @@ -170,7 +175,7 @@ pub trait TypedDataFields<T> {
fn document(&mut self, doc: impl IntoDocComment) -> &mut Self;

/// Adds a type to the queued overrides.
///
///
/// It will be used on the next field and will override the type that is automatically used.
fn coerce(&mut self, ty: impl Into<Type>) -> &mut Self;

Expand Down Expand Up @@ -199,8 +204,8 @@ pub trait TypedDataFields<T> {
S: Into<String>,
R: IntoLua + Typed,
A: FromLua + Typed,
GET: 'static + MaybeSend + Fn(& Lua, &T) -> mlua::Result<R>,
SET: 'static + MaybeSend + Fn(& Lua, &mut T, A) -> mlua::Result<()>;
GET: 'static + MaybeSend + Fn(&Lua, &T) -> mlua::Result<R>,
SET: 'static + MaybeSend + Fn(&Lua, &mut T, A) -> mlua::Result<()>;

/// Typed version of [add_field_function_get](mlua::UserDataFields::add_field_function_get)
fn add_field_function_get<S, R, F>(&mut self, name: S, function: F)
Expand All @@ -227,7 +232,8 @@ pub trait TypedDataFields<T> {

/// Typed version of [add_meta_field](mlua::UserDataFields::add_meta_field)
fn add_meta_field<V>(&mut self, meta: impl Into<String>, value: V)
where V: IntoLua + Typed + 'static;
where
V: IntoLua + Typed + 'static;

/// Typed version of [add_meta_field](mlua::UserDataFields::add_meta_field_with)
fn add_meta_field_with<R, F>(&mut self, meta: impl Into<String>, f: F)
Expand Down
Loading