From d5400f370f65194dbd57ada30120db9c02270e3e Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 16 Aug 2023 11:17:43 -0500 Subject: [PATCH] Remove static lua lifetimes where possible --- src/lune/builtins/fs.rs | 31 ++++++++++------------------ src/lune/builtins/luau.rs | 8 ++++---- src/lune/builtins/net.rs | 39 +++++++++++++++++++----------------- src/lune/builtins/process.rs | 20 +++++++++--------- src/lune/builtins/serde.rs | 32 ++++++++++++++--------------- 5 files changed, 62 insertions(+), 68 deletions(-) diff --git a/src/lune/builtins/fs.rs b/src/lune/builtins/fs.rs index 71fc131..fd2db01 100644 --- a/src/lune/builtins/fs.rs +++ b/src/lune/builtins/fs.rs @@ -25,12 +25,12 @@ pub fn create(lua: &'static Lua) -> LuaResult { .build_readonly() } -async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult { +async fn fs_read_file(lua: &Lua, path: String) -> LuaResult { let bytes = fs::read(&path).await.into_lua_err()?; lua.create_string(bytes) } -async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { +async fn fs_read_dir(_: &Lua, path: String) -> LuaResult> { let mut dir_strings = Vec::new(); let mut dir = fs::read_dir(&path).await.into_lua_err()?; while let Some(dir_entry) = dir.next_entry().await.into_lua_err()? { @@ -59,26 +59,23 @@ async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { Ok(dir_strings_no_prefix) } -async fn fs_write_file( - _: &'static Lua, - (path, contents): (String, LuaString<'_>), -) -> LuaResult<()> { +async fn fs_write_file(_: &Lua, (path, contents): (String, LuaString<'_>)) -> LuaResult<()> { fs::write(&path, &contents.as_bytes()).await.into_lua_err() } -async fn fs_write_dir(_: &'static Lua, path: String) -> LuaResult<()> { +async fn fs_write_dir(_: &Lua, path: String) -> LuaResult<()> { fs::create_dir_all(&path).await.into_lua_err() } -async fn fs_remove_file(_: &'static Lua, path: String) -> LuaResult<()> { +async fn fs_remove_file(_: &Lua, path: String) -> LuaResult<()> { fs::remove_file(&path).await.into_lua_err() } -async fn fs_remove_dir(_: &'static Lua, path: String) -> LuaResult<()> { +async fn fs_remove_dir(_: &Lua, path: String) -> LuaResult<()> { fs::remove_dir_all(&path).await.into_lua_err() } -async fn fs_metadata(_: &'static Lua, path: String) -> LuaResult { +async fn fs_metadata(_: &Lua, path: String) -> LuaResult { match fs::metadata(path).await { Err(e) if e.kind() == IoErrorKind::NotFound => Ok(FsMetadata::not_found()), Ok(meta) => Ok(FsMetadata::from(meta)), @@ -86,7 +83,7 @@ async fn fs_metadata(_: &'static Lua, path: String) -> LuaResult { } } -async fn fs_is_file(_: &'static Lua, path: String) -> LuaResult { +async fn fs_is_file(_: &Lua, path: String) -> LuaResult { match fs::metadata(path).await { Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false), Ok(meta) => Ok(meta.is_file()), @@ -94,7 +91,7 @@ async fn fs_is_file(_: &'static Lua, path: String) -> LuaResult { } } -async fn fs_is_dir(_: &'static Lua, path: String) -> LuaResult { +async fn fs_is_dir(_: &Lua, path: String) -> LuaResult { match fs::metadata(path).await { Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false), Ok(meta) => Ok(meta.is_dir()), @@ -102,10 +99,7 @@ async fn fs_is_dir(_: &'static Lua, path: String) -> LuaResult { } } -async fn fs_move( - _: &'static Lua, - (from, to, options): (String, String, FsWriteOptions), -) -> LuaResult<()> { +async fn fs_move(_: &Lua, (from, to, options): (String, String, FsWriteOptions)) -> LuaResult<()> { let path_from = PathBuf::from(from); if !path_from.exists() { return Err(LuaError::RuntimeError(format!( @@ -124,9 +118,6 @@ async fn fs_move( Ok(()) } -async fn fs_copy( - _: &'static Lua, - (from, to, options): (String, String, FsWriteOptions), -) -> LuaResult<()> { +async fn fs_copy(_: &Lua, (from, to, options): (String, String, FsWriteOptions)) -> LuaResult<()> { copy(from, to, options).await } diff --git a/src/lune/builtins/luau.rs b/src/lune/builtins/luau.rs index c80a630..6d0c7e4 100644 --- a/src/lune/builtins/luau.rs +++ b/src/lune/builtins/luau.rs @@ -29,10 +29,10 @@ fn compile_source<'lua>( } } -fn load_source<'a>( - lua: &'static Lua, - (source, options): (LuaString<'a>, LuauLoadOptions), -) -> LuaResult> { +fn load_source<'lua>( + lua: &'lua Lua, + (source, options): (LuaString<'lua>, LuauLoadOptions), +) -> LuaResult> { lua.load(source.as_bytes()) .set_name(options.debug_name) .into_function() diff --git a/src/lune/builtins/net.rs b/src/lune/builtins/net.rs index 8bb7707..0be55b5 100644 --- a/src/lune/builtins/net.rs +++ b/src/lune/builtins/net.rs @@ -47,19 +47,22 @@ fn create_user_agent_header() -> String { format!("{github_owner}-{github_repo}-cli") } -fn net_json_encode<'a>( - lua: &'static Lua, - (val, pretty): (LuaValue<'a>, Option), -) -> LuaResult> { +fn net_json_encode<'lua>( + lua: &'lua Lua, + (val, pretty): (LuaValue<'lua>, Option), +) -> LuaResult> { EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default())) .serialize_to_string(lua, val) } -fn net_json_decode<'a>(lua: &'static Lua, json: LuaString<'a>) -> LuaResult> { +fn net_json_decode<'lua>(lua: &'lua Lua, json: LuaString<'lua>) -> LuaResult> { EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json) } -async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaResult> { +async fn net_request<'lua>( + lua: &'static Lua, + config: RequestConfig<'lua>, +) -> LuaResult> { // Create and send the request let client: LuaUserDataRef = lua.named_registry_value("net.client")?; let mut request = client.request(config.method, &config.url); @@ -119,15 +122,15 @@ async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaRes .build_readonly() } -async fn net_socket<'a>(lua: &'static Lua, url: String) -> LuaResult { +async fn net_socket<'lua>(lua: &'static Lua, url: String) -> LuaResult { let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?; NetWebSocket::new(ws).into_lua_table(lua) } -async fn net_serve<'a>( +async fn net_serve<'lua>( lua: &'static Lua, - (port, config): (u16, ServeConfig<'a>), -) -> LuaResult> { + (port, config): (u16, ServeConfig<'lua>), +) -> LuaResult> { // Note that we need to use a mpsc here and not // a oneshot channel since we move the sender // into our table with the stop function @@ -188,10 +191,10 @@ async fn net_serve<'a>( .build_readonly() } -fn net_url_encode<'a>( - lua: &'static Lua, - (lua_string, as_binary): (LuaString<'a>, Option), -) -> LuaResult> { +fn net_url_encode<'lua>( + lua: &'lua Lua, + (lua_string, as_binary): (LuaString<'lua>, Option), +) -> LuaResult> { if matches!(as_binary, Some(true)) { urlencoding::encode_binary(lua_string.as_bytes()).into_lua(lua) } else { @@ -199,10 +202,10 @@ fn net_url_encode<'a>( } } -fn net_url_decode<'a>( - lua: &'static Lua, - (lua_string, as_binary): (LuaString<'a>, Option), -) -> LuaResult> { +fn net_url_decode<'lua>( + lua: &'lua Lua, + (lua_string, as_binary): (LuaString<'lua>, Option), +) -> LuaResult> { if matches!(as_binary, Some(true)) { urlencoding::decode_binary(lua_string.as_bytes()).into_lua(lua) } else { diff --git a/src/lune/builtins/process.rs b/src/lune/builtins/process.rs index b83948c..51e7c6f 100644 --- a/src/lune/builtins/process.rs +++ b/src/lune/builtins/process.rs @@ -81,10 +81,10 @@ pub fn create(lua: &'static Lua, args_vec: Vec) -> LuaResult { .build_readonly() } -fn process_env_get<'a>( - lua: &'static Lua, - (_, key): (LuaValue<'a>, String), -) -> LuaResult> { +fn process_env_get<'lua>( + lua: &'lua Lua, + (_, key): (LuaValue<'lua>, String), +) -> LuaResult> { match env::var_os(key) { Some(value) => { let raw_value = RawOsString::new(value); @@ -96,9 +96,9 @@ fn process_env_get<'a>( } } -fn process_env_set( - _: &'static Lua, - (_, key, value): (LuaValue, String, Option), +fn process_env_set<'lua>( + _: &'lua Lua, + (_, key, value): (LuaValue<'lua>, String, Option), ) -> LuaResult<()> { // Make sure key is valid, otherwise set_var will panic if key.is_empty() { @@ -150,10 +150,10 @@ fn process_env_iter<'lua>( }) } -async fn process_spawn<'a>( +async fn process_spawn<'lua>( lua: &'static Lua, - (mut program, args, options): (String, Option>, Option>), -) -> LuaResult> { + (mut program, args, options): (String, Option>, Option>), +) -> LuaResult> { // Parse any given options or create defaults let (child_cwd, child_envs, child_shell, child_stdio_inherit) = match options { Some(options) => { diff --git a/src/lune/builtins/serde.rs b/src/lune/builtins/serde.rs index e2bc6e7..5069337 100644 --- a/src/lune/builtins/serde.rs +++ b/src/lune/builtins/serde.rs @@ -16,34 +16,34 @@ pub fn create(lua: &'static Lua) -> LuaResult { .build_readonly() } -fn serde_encode<'a>( - lua: &'static Lua, - (format, val, pretty): (EncodeDecodeFormat, LuaValue<'a>, Option), -) -> LuaResult> { +fn serde_encode<'lua>( + lua: &'lua Lua, + (format, val, pretty): (EncodeDecodeFormat, LuaValue<'lua>, Option), +) -> LuaResult> { let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default())); config.serialize_to_string(lua, val) } -fn serde_decode<'a>( - lua: &'static Lua, - (format, str): (EncodeDecodeFormat, LuaString<'a>), -) -> LuaResult> { +fn serde_decode<'lua>( + lua: &'lua Lua, + (format, str): (EncodeDecodeFormat, LuaString<'lua>), +) -> LuaResult> { let config = EncodeDecodeConfig::from(format); config.deserialize_from_string(lua, str) } -async fn serde_compress<'a>( - lua: &'static Lua, - (format, str): (CompressDecompressFormat, LuaString<'a>), -) -> LuaResult> { +async fn serde_compress<'lua>( + lua: &'lua Lua, + (format, str): (CompressDecompressFormat, LuaString<'lua>), +) -> LuaResult> { let bytes = compress(format, str).await?; lua.create_string(bytes) } -async fn serde_decompress<'a>( - lua: &'static Lua, - (format, str): (CompressDecompressFormat, LuaString<'a>), -) -> LuaResult> { +async fn serde_decompress<'lua>( + lua: &'lua Lua, + (format, str): (CompressDecompressFormat, LuaString<'lua>), +) -> LuaResult> { let bytes = decompress(format, str).await?; lua.create_string(bytes) }