Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 3a9dfe9

Browse files
committed
rustfmt, add more lua macros
* Added all of the lua_is* macros * Added luaL_dostring * Added luaL_dofile * Added luaL_getmetatable * Added luaL_argcheck * Bump to edition 2021
1 parent 26fcab0 commit 3a9dfe9

14 files changed

Lines changed: 298 additions & 216 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[package]
22
name = "rglua"
33
description = "Rust bindings to the lua api for gmod binary module creation"
4-
version = "0.5.0"
4+
version = "0.6.0"
55
authors = ["Vurv <vurvdevelops@gmail.com>"]
66
keywords = ["glua", "garrysmod", "lua", "gmod"]
77
readme = "README.md"
88
license = "MIT"
9-
edition = "2018"
9+
edition = "2021"
1010

1111
# Remember to make your output module a cdylib.
1212

1313
[dependencies]
14-
libloading = "0.7.0"
15-
once_cell = "1.7.2"
14+
libloading = "0.7.2"
15+
once_cell = "1.8.0"
1616

1717
vtables = { version = "0.1.0", optional = true }
1818
vtables_derive = { version = "0.1.0", optional = true }

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hard_tabs = true

src/globals.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub mod Lua {
1212
pub static NUMTYPES: c_int = 9;
1313
pub static NUMTAGS: c_int = NUMTYPES;
1414

15-
1615
// Proper enums to use. Cast these to integers when using them
1716
pub enum Type {
1817
None = -1,
@@ -24,7 +23,7 @@ pub mod Lua {
2423
Table,
2524
Function,
2625
UserData,
27-
Thread
26+
Thread,
2827
}
2928

3029
pub enum Status {
@@ -33,7 +32,7 @@ pub mod Lua {
3332
ErrRun,
3433
ErrSyntax,
3534
ErrMem,
36-
ErrErr
35+
ErrErr,
3736
}
3837

3938
// Garbage collection
@@ -48,7 +47,7 @@ pub mod Lua {
4847
SetStepMul,
4948
IsRunning,
5049
Gen,
51-
Inc // 11
50+
Inc, // 11
5251
}
5352

5453
// To be used with debug.sethook
@@ -57,14 +56,14 @@ pub mod Lua {
5756
Ret,
5857
Line,
5958
Count,
60-
TailCall
59+
TailCall,
6160
}
6261

6362
pub enum Mask {
6463
Call = (1 << Hook::Call as i32),
6564
Ret = (1 << Hook::Ret as i32),
6665
Line = (1 << Hook::Line as i32),
67-
Count = (1 << Hook::Count as i32)
66+
Count = (1 << Hook::Count as i32),
6867
}
6968
}
7069

@@ -78,7 +77,7 @@ pub mod Jit {
7877
TRACE,
7978
WRAPCFUNC = 0x10,
8079
MAX,
81-
MASK = 0x0ff // LUAJIT_MODE_MASK
80+
MASK = 0x0ff, // LUAJIT_MODE_MASK
8281
}
8382

8483
use super::c_int;
@@ -88,4 +87,4 @@ pub mod Jit {
8887
pub const ON: c_int = 0x0100;
8988
pub const FLUSH: c_int = 0x0200;
9089
}
91-
}
90+
}

src/helper/mod.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
11
#![allow(unused)]
22

3-
#[deprecated(
4-
since = "0.2.0",
5-
note = "Use cstr instead."
6-
)]
3+
#[deprecated(since = "0.2.0", note = "Use cstr instead.")]
74
#[macro_export]
85
macro_rules! cstring {
9-
($rstring:expr) => {
10-
{
11-
let v = std::ffi::CString::new($rstring);
12-
v.expect("Couldn't make CString from rust string").as_ptr()
13-
}
14-
}
6+
($rstring:expr) => {{
7+
let v = std::ffi::CString::new($rstring);
8+
v.expect("Couldn't make CString from rust string").as_ptr()
9+
}};
1510
}
1611

1712
// Creates *const i8 from a rust string literal by concatting a \0 to the end of it.
1813
#[macro_export]
1914
macro_rules! cstr {
2015
($rstring:expr) => {
2116
concat!($rstring, "\0").as_ptr() as *const i8
22-
}
17+
};
2318
}
2419

2520
// Get a rust &str from a const char*
2621
#[macro_export]
2722
macro_rules! rstr {
28-
($cstring:expr) => {
29-
{
30-
#[allow(unused_unsafe)]
31-
let cstr = unsafe{ std::ffi::CStr::from_ptr($cstring) };
32-
cstr.to_str().expect("Couldn't unwrap CString")
33-
}
34-
}
23+
($cstring:expr) => {{
24+
#[allow(unused_unsafe)]
25+
let cstr = unsafe { std::ffi::CStr::from_ptr($cstring) };
26+
cstr.to_str().expect("Couldn't unwrap CString")
27+
}};
3528
}
3629

3730
#[macro_export]
3831
#[deprecated(since = "0.5.0", note = "Use rstr instead.")]
3932
macro_rules! rstring {
40-
($a:tt) => { rstr!($a) }
33+
($a:tt) => {
34+
rstr!($a)
35+
};
4136
}
4237

4338
#[allow(unused_macros)]
@@ -51,10 +46,10 @@ macro_rules! printgm {
5146
{
5247
let printargs = format!( $($x,)* );
5348
if let Ok(fmt) = std::ffi::CString::new(printargs) {
54-
rglua::lua_shared::lua_getglobal( $state, cstr!("print") );
49+
rglua::lua_shared::lua_getglobal( $state, rglua::helper::cstr!("print") );
5550
rglua::lua_shared::lua_pushstring( $state, fmt.as_ptr() );
5651
rglua::lua_shared::lua_call( $state, 1, 0 );
5752
}
5853
}
5954
};
60-
}
55+
}

src/interface.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
2-
use std::ffi::{ c_void, CString };
31
pub use crate::interfaces::*;
2+
use std::ffi::{c_void, CString};
43

5-
pub type CreateInterfaceFn = extern "system" fn(
6-
pName: *const i8,
7-
pReturnCode: *mut i32
8-
) -> *mut c_void;
4+
pub type CreateInterfaceFn =
5+
extern "system" fn(pName: *const i8, pReturnCode: *mut i32) -> *mut c_void;
96

107
/// # Safety
118
/// This function is unsafe to transmute the internal libloading symbol to a proper createinterface function pointer.
129
pub unsafe fn get_interface_handle(file: &str) -> Result<CreateInterfaceFn, libloading::Error> {
1310
let lib = libloading::Library::new(file)?;
1411
let sym: libloading::Symbol<CreateInterfaceFn> = lib.get(b"CreateInterface\0")?;
1512

16-
Ok( std::mem::transmute(sym) )
13+
Ok(std::mem::transmute(sym))
1714
}
1815

1916
#[derive(Debug)]
2017
pub enum InterfaceError {
21-
BadCString( std::ffi::NulError ),
18+
BadCString(std::ffi::NulError),
2219
FactoryNotFound,
2320
}
2421

25-
pub fn get_from_interface(iface: &str, factory: CreateInterfaceFn) -> Result<*mut (), InterfaceError> {
22+
pub fn get_from_interface(
23+
iface: &str,
24+
factory: CreateInterfaceFn,
25+
) -> Result<*mut (), InterfaceError> {
2626
let mut status = 0;
2727

28-
let iface = CString::new(iface)
29-
.map_err(InterfaceError::BadCString)?;
28+
let iface = CString::new(iface).map_err(InterfaceError::BadCString)?;
3029

31-
let result = factory( iface.as_ptr(), &mut status );
30+
let result = factory(iface.as_ptr(), &mut status);
3231

3332
if status == 0 && !result.is_null() {
3433
Ok(result as *mut ())
3534
} else {
36-
Err( InterfaceError::FactoryNotFound )
35+
Err(InterfaceError::FactoryNotFound)
3736
}
38-
}
37+
}

src/interfaces/engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::os::raw::c_char;
88
#[has_vtable]
99
#[derive(VTable, Debug)]
1010
pub struct EngineClient {
11-
pub vtable: usize
11+
pub vtable: usize,
1212
}
1313

1414
impl EngineClient {
@@ -38,4 +38,4 @@ impl EngineClient {
3838

3939
#[virtual_index(113)]
4040
pub fn ClientCmd_Unrestricted(&self, command: *const c_char) {}
41-
}
41+
}

src/interfaces/lua.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ pub struct CLuaShared {}
1010
impl CLuaShared {
1111
#[virtual_index(6)]
1212
pub fn GetLuaInterface(&self, realm: u8) -> *mut ILuaInterface {}
13-
}
13+
}

src/interfaces/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
pub mod structs {
32
use vtables::VTable;
43
use vtables_derive::*;
@@ -20,13 +19,13 @@ pub mod structs {
2019
ishltv: bool,
2120
customFiles: [u64; 4],
2221
filesDownloaded: u8,
23-
pad: [i8; 304]
22+
pad: [i8; 304],
2423
}
2524

2625
#[has_vtable]
2726
#[derive(VTable)]
2827
pub struct ILuaInterface {
29-
pub vtable: usize
28+
pub vtable: usize,
3029
}
3130

3231
impl ILuaInterface {
@@ -48,4 +47,4 @@ mod lua;
4847
pub use lua::CLuaShared;
4948

5049
mod panel;
51-
pub use panel::IPanel;
50+
pub use panel::IPanel;

src/interfaces/panel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use vtables_derive::*;
44
#[has_vtable]
55
#[derive(VTable)]
66
pub struct IPanel {
7-
pub vtable: usize
7+
pub vtable: usize,
88
}
99

1010
impl IPanel {
@@ -13,4 +13,4 @@ impl IPanel {
1313

1414
#[virtual_index(41)]
1515
pub fn PaintTraverse(&self, vguiPanel: u32, forceRepaint: bool, allowForce: bool) {}
16-
}
16+
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#![allow(non_snake_case)]
33
#![allow(non_upper_case_globals)]
44

5-
pub mod types;
65
pub mod globals;
76
mod helper;
87
#[cfg(feature = "interfaces")]
9-
mod interfaces;
10-
#[cfg(feature = "interfaces")]
118
pub mod interface;
9+
#[cfg(feature = "interfaces")]
10+
mod interfaces;
11+
pub mod types;
1212

1313
#[macro_use]
1414
pub mod lua_shared;
1515

16-
pub mod prelude;
16+
pub mod prelude;

0 commit comments

Comments
 (0)