Skip to content

Commit 3641c98

Browse files
committed
Prepare for Rust 2024 edition (see rust-lang/rust#123748)
Replace `IntoLua(Multi)` generic with positional arg (impl trait) where possible This allow to shorten syntax from `a.get::<_, T>` to `a.get::<T>`
1 parent b7d170a commit 3641c98

31 files changed

+378
-413
lines changed

benches/benchmark.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn table_get_set(c: &mut Criterion) {
7474
.enumerate()
7575
{
7676
table.raw_set(s, i).unwrap();
77-
assert_eq!(table.raw_get::<_, usize>(s).unwrap(), i);
77+
assert_eq!(table.raw_get::<usize>(s).unwrap(), i);
7878
}
7979
},
8080
BatchSize::SmallInput,
@@ -153,7 +153,7 @@ fn function_call_sum(c: &mut Criterion) {
153153
b.iter_batched(
154154
|| collect_gc_twice(&lua),
155155
|_| {
156-
assert_eq!(sum.call::<_, i64>((10, 20, 30)).unwrap(), 0);
156+
assert_eq!(sum.call::<i64>((10, 20, 30)).unwrap(), 0);
157157
},
158158
BatchSize::SmallInput,
159159
);
@@ -172,7 +172,7 @@ fn function_call_lua_sum(c: &mut Criterion) {
172172
b.iter_batched(
173173
|| collect_gc_twice(&lua),
174174
|_| {
175-
assert_eq!(sum.call::<_, i64>((10, 20, 30)).unwrap(), 0);
175+
assert_eq!(sum.call::<i64>((10, 20, 30)).unwrap(), 0);
176176
},
177177
BatchSize::SmallInput,
178178
);
@@ -195,7 +195,7 @@ fn function_call_concat(c: &mut Criterion) {
195195
},
196196
|i| {
197197
assert_eq!(
198-
concat.call::<_, LuaString>(("num:", i)).unwrap(),
198+
concat.call::<LuaString>(("num:", i)).unwrap(),
199199
format!("num:{i}")
200200
);
201201
},
@@ -221,7 +221,7 @@ fn function_call_lua_concat(c: &mut Criterion) {
221221
},
222222
|i| {
223223
assert_eq!(
224-
concat.call::<_, LuaString>(("num:", i)).unwrap(),
224+
concat.call::<LuaString>(("num:", i)).unwrap(),
225225
format!("num:{i}")
226226
);
227227
},
@@ -246,7 +246,7 @@ fn function_async_call_sum(c: &mut Criterion) {
246246
b.to_async(rt).iter_batched(
247247
|| collect_gc_twice(&lua),
248248
|_| async {
249-
assert_eq!(sum.call_async::<_, i64>((10, 20, 30)).await.unwrap(), 0);
249+
assert_eq!(sum.call_async::<i64>((10, 20, 30)).await.unwrap(), 0);
250250
},
251251
BatchSize::SmallInput,
252252
);
@@ -319,7 +319,7 @@ fn userdata_call_index(c: &mut Criterion) {
319319
b.iter_batched(
320320
|| collect_gc_twice(&lua),
321321
|_| {
322-
assert_eq!(index.call::<_, LuaString>(&ud).unwrap(), "test");
322+
assert_eq!(index.call::<LuaString>(&ud).unwrap(), "test");
323323
},
324324
BatchSize::SmallInput,
325325
);
@@ -349,7 +349,7 @@ fn userdata_call_method(c: &mut Criterion) {
349349
i.fetch_add(1, Ordering::Relaxed)
350350
},
351351
|i| {
352-
assert_eq!(method.call::<_, usize>((&ud, i)).unwrap(), 123 + i);
352+
assert_eq!(method.call::<usize>((&ud, i)).unwrap(), 123 + i);
353353
},
354354
BatchSize::SmallInput,
355355
);
@@ -384,7 +384,7 @@ fn userdata_async_call_method(c: &mut Criterion) {
384384
(method.clone(), ud.clone(), i.fetch_add(1, Ordering::Relaxed))
385385
},
386386
|(method, ud, i)| async move {
387-
assert_eq!(method.call_async::<_, usize>((ud, i)).await.unwrap(), 123 + i);
387+
assert_eq!(method.call_async::<usize>((ud, i)).await.unwrap(), 123 + i);
388388
},
389389
BatchSize::SmallInput,
390390
);

benches/serde.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn encode_json(c: &mut Criterion) {
3737
b.iter_batched(
3838
|| collect_gc_twice(&lua),
3939
|_| {
40-
encode.call::<_, LuaString>(&table).unwrap();
40+
encode.call::<LuaString>(&table).unwrap();
4141
},
4242
BatchSize::SmallInput,
4343
);
@@ -69,7 +69,7 @@ fn decode_json(c: &mut Criterion) {
6969
b.iter_batched(
7070
|| collect_gc_twice(&lua),
7171
|_| {
72-
decode.call::<_, LuaTable>(json).unwrap();
72+
decode.call::<LuaTable>(json).unwrap();
7373
},
7474
BatchSize::SmallInput,
7575
);

examples/async_http_server.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ impl hyper::service::Service<Request<Incoming>> for Svc {
4747
let handler = self.handler.clone();
4848
let lua_req = LuaRequest(self.peer_addr, req);
4949
Box::pin(async move {
50-
match handler.call_async::<_, Table>(lua_req).await {
50+
match handler.call_async::<Table>(lua_req).await {
5151
Ok(lua_resp) => {
52-
let status = lua_resp.get::<_, Option<u16>>("status")?.unwrap_or(200);
52+
let status = lua_resp.get::<Option<u16>>("status")?.unwrap_or(200);
5353
let mut resp = Response::builder().status(status);
5454

5555
// Set headers
56-
if let Some(headers) = lua_resp.get::<_, Option<Table>>("headers")? {
56+
if let Some(headers) = lua_resp.get::<Option<Table>>("headers")? {
5757
for pair in headers.pairs::<String, LuaString>() {
5858
let (h, v) = pair?;
5959
resp = resp.header(&h, &*v.as_bytes());
@@ -62,7 +62,7 @@ impl hyper::service::Service<Request<Incoming>> for Svc {
6262

6363
// Set body
6464
let body = lua_resp
65-
.get::<_, Option<LuaString>>("body")?
65+
.get::<Option<LuaString>>("body")?
6666
.map(|b| Full::new(Bytes::copy_from_slice(&b.as_bytes())).boxed())
6767
.unwrap_or_else(|| Empty::<Bytes>::new().boxed());
6868

examples/async_tcp_server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn run_server(handler: Function) -> io::Result<()> {
4747
let handler = handler.clone();
4848
tokio::task::spawn(async move {
4949
let stream = LuaTcpStream(stream);
50-
if let Err(err) = handler.call_async::<_, ()>(stream).await {
50+
if let Err(err) = handler.call_async::<()>(stream).await {
5151
eprintln!("{}", err);
5252
}
5353
});

examples/guided_tour.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fn main() -> Result<()> {
1717
globals.set("string_var", "hello")?;
1818
globals.set("int_var", 42)?;
1919

20-
assert_eq!(globals.get::<_, String>("string_var")?, "hello");
21-
assert_eq!(globals.get::<_, i64>("int_var")?, 42);
20+
assert_eq!(globals.get::<String>("string_var")?, "hello");
21+
assert_eq!(globals.get::<i64>("int_var")?, 42);
2222

2323
// You can load and evaluate Lua code. The returned type of `Lua::load` is a builder
2424
// that allows you to change settings before running Lua code. Here, we are using it to set
@@ -32,7 +32,7 @@ fn main() -> Result<()> {
3232
)
3333
.set_name("example code")
3434
.exec()?;
35-
assert_eq!(globals.get::<_, String>("global")?, "foobar");
35+
assert_eq!(globals.get::<String>("global")?, "foobar");
3636

3737
assert_eq!(lua.load("1 + 1").eval::<i32>()?, 2);
3838
assert_eq!(lua.load("false == false").eval::<bool>()?, true);
@@ -85,16 +85,16 @@ fn main() -> Result<()> {
8585
// You can load Lua functions
8686

8787
let print: Function = globals.get("print")?;
88-
print.call::<_, ()>("hello from rust")?;
88+
print.call::<()>("hello from rust")?;
8989

9090
// This API generally handles variadic using tuples. This is one way to call a function with
9191
// multiple parameters:
9292

93-
print.call::<_, ()>(("hello", "again", "from", "rust"))?;
93+
print.call::<()>(("hello", "again", "from", "rust"))?;
9494

9595
// But, you can also pass variadic arguments with the `Variadic` type.
9696

97-
print.call::<_, ()>(Variadic::from_iter(
97+
print.call::<()>(Variadic::from_iter(
9898
["hello", "yet", "again", "from", "rust"].iter().cloned(),
9999
))?;
100100

src/chunk.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<'a> Chunk<'a> {
308308
/// All global variables (including the standard library!) are looked up in `_ENV`, so it may be
309309
/// necessary to populate the environment in order for scripts using custom environments to be
310310
/// useful.
311-
pub fn set_environment<V: IntoLua>(mut self, env: V) -> Self {
311+
pub fn set_environment(mut self, env: impl IntoLua) -> Self {
312312
let lua = self.lua.lock();
313313
let lua = lua.lua();
314314
self.env = env
@@ -343,7 +343,7 @@ impl<'a> Chunk<'a> {
343343
///
344344
/// This is equivalent to calling the chunk function with no arguments and no return values.
345345
pub fn exec(self) -> Result<()> {
346-
self.call::<_, ()>(())?;
346+
self.call::<()>(())?;
347347
Ok(())
348348
}
349349

@@ -404,7 +404,7 @@ impl<'a> Chunk<'a> {
404404
/// Load the chunk function and call it with the given arguments.
405405
///
406406
/// This is equivalent to `into_function` and calling the resulting function.
407-
pub fn call<A: IntoLuaMulti, R: FromLuaMulti>(self, args: A) -> Result<R> {
407+
pub fn call<R: FromLuaMulti>(self, args: impl IntoLuaMulti) -> Result<R> {
408408
self.into_function()?.call(args)
409409
}
410410

@@ -417,9 +417,8 @@ impl<'a> Chunk<'a> {
417417
/// [`call`]: #method.call
418418
#[cfg(feature = "async")]
419419
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
420-
pub async fn call_async<A, R>(self, args: A) -> Result<R>
420+
pub async fn call_async<R>(self, args: impl IntoLuaMulti) -> Result<R>
421421
where
422-
A: IntoLuaMulti,
423422
R: FromLuaMulti,
424423
{
425424
self.into_function()?.call_async(args).await

src/function.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Function {
7575
///
7676
/// let tostring: Function = globals.get("tostring")?;
7777
///
78-
/// assert_eq!(tostring.call::<_, String>(123)?, "123");
78+
/// assert_eq!(tostring.call::<String>(123)?, "123");
7979
///
8080
/// # Ok(())
8181
/// # }
@@ -94,12 +94,12 @@ impl Function {
9494
/// end
9595
/// "#).eval()?;
9696
///
97-
/// assert_eq!(sum.call::<_, u32>((3, 4))?, 3 + 4);
97+
/// assert_eq!(sum.call::<u32>((3, 4))?, 3 + 4);
9898
///
9999
/// # Ok(())
100100
/// # }
101101
/// ```
102-
pub fn call<A: IntoLuaMulti, R: FromLuaMulti>(&self, args: A) -> Result<R> {
102+
pub fn call<R: FromLuaMulti>(&self, args: impl IntoLuaMulti) -> Result<R> {
103103
let lua = self.0.lua.lock();
104104
let state = lua.state();
105105
unsafe {
@@ -153,9 +153,8 @@ impl Function {
153153
/// [`AsyncThread`]: crate::AsyncThread
154154
#[cfg(feature = "async")]
155155
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
156-
pub fn call_async<A, R>(&self, args: A) -> impl Future<Output = Result<R>>
156+
pub fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>>
157157
where
158-
A: IntoLuaMulti,
159158
R: FromLuaMulti,
160159
{
161160
let lua = self.0.lua.lock();
@@ -188,15 +187,15 @@ impl Function {
188187
/// "#).eval()?;
189188
///
190189
/// let bound_a = sum.bind(1)?;
191-
/// assert_eq!(bound_a.call::<_, u32>(2)?, 1 + 2);
190+
/// assert_eq!(bound_a.call::<u32>(2)?, 1 + 2);
192191
///
193192
/// let bound_a_and_b = sum.bind(13)?.bind(57)?;
194-
/// assert_eq!(bound_a_and_b.call::<_, u32>(())?, 13 + 57);
193+
/// assert_eq!(bound_a_and_b.call::<u32>(())?, 13 + 57);
195194
///
196195
/// # Ok(())
197196
/// # }
198197
/// ```
199-
pub fn bind<A: IntoLuaMulti>(&self, args: A) -> Result<Function> {
198+
pub fn bind(&self, args: impl IntoLuaMulti) -> Result<Function> {
200199
unsafe extern "C-unwind" fn args_wrapper_impl(state: *mut ffi::lua_State) -> c_int {
201200
let nargs = ffi::lua_gettop(state);
202201
let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(1)) as c_int;

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
7474
// Deny warnings inside doc tests / examples. When this isn't present, rustdoc doesn't show *any*
7575
// warnings at all.
76-
#![doc(test(attr(warn(warnings))))] // FIXME: Remove this when rust-lang/rust#123748 is fixed
7776
#![cfg_attr(docsrs, feature(doc_cfg))]
7877

7978
#[macro_use]

src/luau/package.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn lua_loader(lua: &Lua, modname: StdString) -> Result<Value> {
194194
let key = lua.app_data_ref::<PackageKey>().unwrap();
195195
lua.registry_value::<Table>(&key.0)
196196
}?;
197-
let search_path = package.get::<_, StdString>("path").unwrap_or_default();
197+
let search_path = package.get::<StdString>("path").unwrap_or_default();
198198

199199
if let Some(file_path) = package_searchpath(&modname, &search_path, false) {
200200
match fs::read(&file_path) {
@@ -222,7 +222,7 @@ fn dylib_loader(lua: &Lua, modname: StdString) -> Result<Value> {
222222
let key = lua.app_data_ref::<PackageKey>().unwrap();
223223
lua.registry_value::<Table>(&key.0)
224224
}?;
225-
let search_cpath = package.get::<_, StdString>("cpath").unwrap_or_default();
225+
let search_cpath = package.get::<StdString>("cpath").unwrap_or_default();
226226

227227
let find_symbol = |lib: &Library| unsafe {
228228
if let Ok(entry) = lib.get::<ffi::lua_CFunction>(format!("luaopen_{modname}\0").as_bytes()) {

src/state.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ impl Lua {
436436
///
437437
/// lua.sandbox(true)?;
438438
/// lua.load("var = 123").exec()?;
439-
/// assert_eq!(lua.globals().get::<_, u32>("var")?, 123);
439+
/// assert_eq!(lua.globals().get::<u32>("var")?, 123);
440440
///
441441
/// // Restore the global environment (clear changes made in sandbox)
442442
/// lua.sandbox(false)?;
443-
/// assert_eq!(lua.globals().get::<_, Option<u32>>("var")?, None);
443+
/// assert_eq!(lua.globals().get::<Option<u32>>("var")?, None);
444444
/// # Ok(())
445445
/// # }
446446
/// ```
@@ -1497,7 +1497,7 @@ impl Lua {
14971497

14981498
/// Converts a value that implements `IntoLua` into a `Value` instance.
14991499
#[inline]
1500-
pub fn pack<T: IntoLua>(&self, t: T) -> Result<Value> {
1500+
pub fn pack(&self, t: impl IntoLua) -> Result<Value> {
15011501
t.into_lua(self)
15021502
}
15031503

@@ -1509,7 +1509,7 @@ impl Lua {
15091509

15101510
/// Converts a value that implements `IntoLuaMulti` into a `MultiValue` instance.
15111511
#[inline]
1512-
pub fn pack_multi<T: IntoLuaMulti>(&self, t: T) -> Result<MultiValue> {
1512+
pub fn pack_multi(&self, t: impl IntoLuaMulti) -> Result<MultiValue> {
15131513
t.into_lua_multi(self)
15141514
}
15151515

@@ -1523,10 +1523,7 @@ impl Lua {
15231523
///
15241524
/// This value will be available to rust from all `Lua` instances which share the same main
15251525
/// state.
1526-
pub fn set_named_registry_value<T>(&self, name: &str, t: T) -> Result<()>
1527-
where
1528-
T: IntoLua,
1529-
{
1526+
pub fn set_named_registry_value(&self, name: &str, t: impl IntoLua) -> Result<()> {
15301527
let lua = self.lock();
15311528
let state = lua.state();
15321529
unsafe {
@@ -1575,7 +1572,7 @@ impl Lua {
15751572
/// Be warned, garbage collection of values held inside the registry is not automatic, see
15761573
/// [`RegistryKey`] for more details.
15771574
/// However, dropped [`RegistryKey`]s automatically reused to store new values.
1578-
pub fn create_registry_value<T: IntoLua>(&self, t: T) -> Result<RegistryKey> {
1575+
pub fn create_registry_value(&self, t: impl IntoLua) -> Result<RegistryKey> {
15791576
let lua = self.lock();
15801577
let state = lua.state();
15811578
unsafe {
@@ -1657,7 +1654,7 @@ impl Lua {
16571654
/// An identifier used in [`RegistryKey`] may possibly be changed to a new value.
16581655
///
16591656
/// See [`Lua::create_registry_value`] for more details.
1660-
pub fn replace_registry_value<T: IntoLua>(&self, key: &mut RegistryKey, t: T) -> Result<()> {
1657+
pub fn replace_registry_value(&self, key: &mut RegistryKey, t: impl IntoLua) -> Result<()> {
16611658
let lua = self.lock();
16621659
if !lua.owns_registry_value(key) {
16631660
return Err(Error::MismatchedRegistryKey);

src/state/raw.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cell::{Cell, UnsafeCell};
33
use std::ffi::{CStr, CString};
44
use std::os::raw::{c_char, c_int, c_void};
55
use std::panic::resume_unwind;
6-
use std::rc::Rc;
76
use std::result::Result as StdResult;
87
use std::sync::Arc;
98
use std::{mem, ptr};
@@ -362,7 +361,7 @@ impl RawLua {
362361
callback_error_ext(state, extra, move |_| {
363362
let hook_cb = (*extra).hook_callback.clone();
364363
let hook_cb = mlua_expect!(hook_cb, "no hook callback set in hook_proc");
365-
if Rc::strong_count(&hook_cb) > 2 {
364+
if std::rc::Rc::strong_count(&hook_cb) > 2 {
366365
return Ok(()); // Don't allow recursion
367366
}
368367
let rawlua = (*extra).raw_lua();
@@ -372,7 +371,7 @@ impl RawLua {
372371
})
373372
}
374373

375-
(*self.extra.get()).hook_callback = Some(Rc::new(callback));
374+
(*self.extra.get()).hook_callback = Some(std::rc::Rc::new(callback));
376375
(*self.extra.get()).hook_thread = state; // Mark for what thread the hook is set
377376
ffi::lua_sethook(state, Some(hook_proc), triggers.mask(), triggers.count());
378377
}
@@ -1198,12 +1197,12 @@ impl RawLua {
11981197
}
11991198

12001199
let lua = self.lua();
1201-
let coroutine = lua.globals().get::<_, Table>("coroutine")?;
1200+
let coroutine = lua.globals().get::<Table>("coroutine")?;
12021201

12031202
let env = lua.create_table_with_capacity(0, 3)?;
12041203
env.set("get_poll", get_poll)?;
12051204
// Cache `yield` function
1206-
env.set("yield", coroutine.get::<_, Function>("yield")?)?;
1205+
env.set("yield", coroutine.get::<Function>("yield")?)?;
12071206
unsafe {
12081207
env.set("unpack", lua.create_c_function(unpack)?)?;
12091208
}

0 commit comments

Comments
 (0)