Skip to content

Commit

Permalink
Drop futures-timer dev-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jul 10, 2023
1 parent 08ab685 commit 6b8b792
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ futures = "0.3.5"
hyper = { version = "0.14", features = ["client", "server"] }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
futures-timer = "3.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
maplit = "1.0"
Expand Down
3 changes: 1 addition & 2 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,13 @@ impl<'lua> Function<'lua> {
///
/// ```
/// use std::time::Duration;
/// use futures_timer::Delay;
/// # use mlua::{Lua, Result};
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let lua = Lua::new();
///
/// let sleep = lua.create_async_function(move |_lua, n: u64| async move {
/// Delay::new(Duration::from_millis(n)).await;
/// tokio::time::sleep(Duration::from_millis(n)).await;
/// Ok(())
/// })?;
///
Expand Down
3 changes: 1 addition & 2 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,11 +1601,10 @@ impl Lua {
///
/// ```
/// use std::time::Duration;
/// use futures_timer::Delay;
/// use mlua::{Lua, Result};
///
/// async fn sleep(_lua: &Lua, n: u64) -> Result<&'static str> {
/// Delay::new(Duration::from_millis(n)).await;
/// tokio::time::sleep(Duration::from_millis(n)).await;
/// Ok("done")
/// }
///
Expand Down
43 changes: 23 additions & 20 deletions tests/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
use std::sync::{Arc, Mutex};
use std::time::Duration;

use futures_timer::Delay;
use futures_util::stream::TryStreamExt;

use mlua::{
AnyUserDataExt, Error, Function, Lua, LuaOptions, Result, StdLib, Table, TableExt, UserData,
UserDataMethods, Value,
};

async fn sleep_ms(ms: u64) {
tokio::time::sleep(Duration::from_millis(ms)).await;
}

#[tokio::test]
async fn test_async_function() -> Result<()> {
let lua = Lua::new();
Expand Down Expand Up @@ -43,7 +46,7 @@ async fn test_async_sleep() -> Result<()> {
let lua = Lua::new();

let sleep = lua.create_async_function(move |_lua, n: u64| async move {
Delay::new(Duration::from_millis(n)).await;
sleep_ms(n).await;
Ok(format!("elapsed:{}ms", n))
})?;
lua.globals().set("sleep", sleep)?;
Expand All @@ -59,7 +62,7 @@ async fn test_async_call() -> Result<()> {
let lua = Lua::new();

let hello = lua.create_async_function(|_lua, name: String| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
Ok(format!("hello, {}!", name))
})?;

Expand Down Expand Up @@ -102,7 +105,7 @@ async fn test_async_handle_yield() -> Result<()> {
let lua = Lua::new();

let sum = lua.create_async_function(|_lua, (a, b): (i64, i64)| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
Ok(a + b)
})?;

Expand Down Expand Up @@ -160,10 +163,10 @@ async fn test_async_return_async_closure() -> Result<()> {
let lua = Lua::new();

let f = lua.create_async_function(|lua, a: i64| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;

let g = lua.create_async_function(move |_, b: i64| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
return Ok(a + b);
})?;

Expand Down Expand Up @@ -253,7 +256,7 @@ async fn test_async_thread() -> Result<()> {
let f = lua.create_async_function(move |_lua, ()| {
let cnt3 = cnt2.clone();
async move {
Delay::new(Duration::from_millis(*cnt3.as_ref())).await;
sleep_ms(*cnt3.as_ref()).await;
Ok("done")
}
})?;
Expand Down Expand Up @@ -296,19 +299,19 @@ async fn test_async_table() -> Result<()> {
table.set("val", 10)?;

let get_value = lua.create_async_function(|_, table: Table| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
table.get::<_, i64>("val")
})?;
table.set("get_value", get_value)?;

let set_value = lua.create_async_function(|_, (table, n): (Table, i64)| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
table.set("val", n)
})?;
table.set("set_value", set_value)?;

let sleep = lua.create_async_function(|_, n| async move {
Delay::new(Duration::from_millis(n)).await;
sleep_ms(n).await;
Ok(format!("elapsed:{}ms", n))
})?;
table.set("sleep", sleep)?;
Expand Down Expand Up @@ -336,12 +339,12 @@ async fn test_async_thread_pool() -> Result<()> {
let lua = Lua::new_with(StdLib::ALL_SAFE, options)?;

let error_f = lua.create_async_function(|_, ()| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
Err::<(), _>(Error::RuntimeError("test".to_string()))
})?;

let sleep = lua.create_async_function(|_, n| async move {
Delay::new(Duration::from_millis(n)).await;
sleep_ms(n).await;
Ok(format!("elapsed:{}ms", n))
})?;

Expand All @@ -359,33 +362,33 @@ async fn test_async_userdata() -> Result<()> {
impl UserData for MyUserData {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method("get_value", |_, data, ()| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
Ok(data.0)
});

methods.add_async_method_mut("set_value", |_, data, n| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
data.0 = n;
Ok(())
});

methods.add_async_function("sleep", |_, n| async move {
Delay::new(Duration::from_millis(n)).await;
sleep_ms(n).await;
Ok(format!("elapsed:{}ms", n))
});

#[cfg(not(any(feature = "lua51", feature = "luau")))]
methods.add_async_meta_method(mlua::MetaMethod::Call, |_, data, ()| async move {
let n = data.0;
Delay::new(Duration::from_millis(n)).await;
sleep_ms(n).await;
Ok(format!("elapsed:{}ms", n))
});

#[cfg(not(any(feature = "lua51", feature = "luau")))]
methods.add_async_meta_method(
mlua::MetaMethod::Index,
|_, data, key: String| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
match key.as_str() {
"ms" => Ok(Some(data.0 as f64)),
"s" => Ok(Some((data.0 as f64) / 1000.0)),
Expand All @@ -398,7 +401,7 @@ async fn test_async_userdata() -> Result<()> {
methods.add_async_meta_method_mut(
mlua::MetaMethod::NewIndex,
|_, data, (key, value): (String, f64)| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
match key.as_str() {
"ms" => data.0 = value as u64,
"s" => data.0 = (value * 1000.0) as u64,
Expand Down Expand Up @@ -485,7 +488,7 @@ async fn test_owned_async_call() -> Result<()> {

let hello = lua
.create_async_function(|_, name: String| async move {
Delay::new(Duration::from_millis(10)).await;
sleep_ms(10).await;
Ok(format!("hello, {}!", name))
})?
.into_owned();
Expand All @@ -506,7 +509,7 @@ async fn test_async_terminate() -> Result<()> {
let mutex = mutex2.clone();
async move {
let _guard = mutex.lock();
Delay::new(Duration::from_millis(100)).await;
sleep_ms(100).await;
Ok(())
}
})?;
Expand Down

0 comments on commit 6b8b792

Please sign in to comment.