Remove static lua lifetimes where possible

This commit is contained in:
Filip Tibell 2023-08-16 11:17:43 -05:00
parent 60026b1c85
commit d5400f370f
No known key found for this signature in database
5 changed files with 62 additions and 68 deletions

View file

@ -25,12 +25,12 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.build_readonly() .build_readonly()
} }
async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult<LuaString> { async fn fs_read_file(lua: &Lua, path: String) -> LuaResult<LuaString> {
let bytes = fs::read(&path).await.into_lua_err()?; let bytes = fs::read(&path).await.into_lua_err()?;
lua.create_string(bytes) lua.create_string(bytes)
} }
async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult<Vec<String>> { async fn fs_read_dir(_: &Lua, path: String) -> LuaResult<Vec<String>> {
let mut dir_strings = Vec::new(); let mut dir_strings = Vec::new();
let mut dir = fs::read_dir(&path).await.into_lua_err()?; let mut dir = fs::read_dir(&path).await.into_lua_err()?;
while let Some(dir_entry) = dir.next_entry().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<Vec<String>> {
Ok(dir_strings_no_prefix) Ok(dir_strings_no_prefix)
} }
async fn fs_write_file( async fn fs_write_file(_: &Lua, (path, contents): (String, LuaString<'_>)) -> LuaResult<()> {
_: &'static Lua,
(path, contents): (String, LuaString<'_>),
) -> LuaResult<()> {
fs::write(&path, &contents.as_bytes()).await.into_lua_err() 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() 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() 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() fs::remove_dir_all(&path).await.into_lua_err()
} }
async fn fs_metadata(_: &'static Lua, path: String) -> LuaResult<FsMetadata> { async fn fs_metadata(_: &Lua, path: String) -> LuaResult<FsMetadata> {
match fs::metadata(path).await { match fs::metadata(path).await {
Err(e) if e.kind() == IoErrorKind::NotFound => Ok(FsMetadata::not_found()), Err(e) if e.kind() == IoErrorKind::NotFound => Ok(FsMetadata::not_found()),
Ok(meta) => Ok(FsMetadata::from(meta)), Ok(meta) => Ok(FsMetadata::from(meta)),
@ -86,7 +83,7 @@ async fn fs_metadata(_: &'static Lua, path: String) -> LuaResult<FsMetadata> {
} }
} }
async fn fs_is_file(_: &'static Lua, path: String) -> LuaResult<bool> { async fn fs_is_file(_: &Lua, path: String) -> LuaResult<bool> {
match fs::metadata(path).await { match fs::metadata(path).await {
Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false), Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false),
Ok(meta) => Ok(meta.is_file()), Ok(meta) => Ok(meta.is_file()),
@ -94,7 +91,7 @@ async fn fs_is_file(_: &'static Lua, path: String) -> LuaResult<bool> {
} }
} }
async fn fs_is_dir(_: &'static Lua, path: String) -> LuaResult<bool> { async fn fs_is_dir(_: &Lua, path: String) -> LuaResult<bool> {
match fs::metadata(path).await { match fs::metadata(path).await {
Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false), Err(e) if e.kind() == IoErrorKind::NotFound => Ok(false),
Ok(meta) => Ok(meta.is_dir()), Ok(meta) => Ok(meta.is_dir()),
@ -102,10 +99,7 @@ async fn fs_is_dir(_: &'static Lua, path: String) -> LuaResult<bool> {
} }
} }
async fn fs_move( async fn fs_move(_: &Lua, (from, to, options): (String, String, FsWriteOptions)) -> LuaResult<()> {
_: &'static Lua,
(from, to, options): (String, String, FsWriteOptions),
) -> LuaResult<()> {
let path_from = PathBuf::from(from); let path_from = PathBuf::from(from);
if !path_from.exists() { if !path_from.exists() {
return Err(LuaError::RuntimeError(format!( return Err(LuaError::RuntimeError(format!(
@ -124,9 +118,6 @@ async fn fs_move(
Ok(()) Ok(())
} }
async fn fs_copy( async fn fs_copy(_: &Lua, (from, to, options): (String, String, FsWriteOptions)) -> LuaResult<()> {
_: &'static Lua,
(from, to, options): (String, String, FsWriteOptions),
) -> LuaResult<()> {
copy(from, to, options).await copy(from, to, options).await
} }

View file

@ -29,10 +29,10 @@ fn compile_source<'lua>(
} }
} }
fn load_source<'a>( fn load_source<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(source, options): (LuaString<'a>, LuauLoadOptions), (source, options): (LuaString<'lua>, LuauLoadOptions),
) -> LuaResult<LuaFunction<'a>> { ) -> LuaResult<LuaFunction<'lua>> {
lua.load(source.as_bytes()) lua.load(source.as_bytes())
.set_name(options.debug_name) .set_name(options.debug_name)
.into_function() .into_function()

View file

@ -47,19 +47,22 @@ fn create_user_agent_header() -> String {
format!("{github_owner}-{github_repo}-cli") format!("{github_owner}-{github_repo}-cli")
} }
fn net_json_encode<'a>( fn net_json_encode<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(val, pretty): (LuaValue<'a>, Option<bool>), (val, pretty): (LuaValue<'lua>, Option<bool>),
) -> LuaResult<LuaString<'a>> { ) -> LuaResult<LuaString<'lua>> {
EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default())) EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default()))
.serialize_to_string(lua, val) .serialize_to_string(lua, val)
} }
fn net_json_decode<'a>(lua: &'static Lua, json: LuaString<'a>) -> LuaResult<LuaValue<'a>> { fn net_json_decode<'lua>(lua: &'lua Lua, json: LuaString<'lua>) -> LuaResult<LuaValue<'lua>> {
EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json) EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json)
} }
async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaResult<LuaTable<'a>> { async fn net_request<'lua>(
lua: &'static Lua,
config: RequestConfig<'lua>,
) -> LuaResult<LuaTable<'lua>> {
// Create and send the request // Create and send the request
let client: LuaUserDataRef<NetClient> = lua.named_registry_value("net.client")?; let client: LuaUserDataRef<NetClient> = lua.named_registry_value("net.client")?;
let mut request = client.request(config.method, &config.url); 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() .build_readonly()
} }
async fn net_socket<'a>(lua: &'static Lua, url: String) -> LuaResult<LuaTable> { async fn net_socket<'lua>(lua: &'static Lua, url: String) -> LuaResult<LuaTable> {
let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?; let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?;
NetWebSocket::new(ws).into_lua_table(lua) NetWebSocket::new(ws).into_lua_table(lua)
} }
async fn net_serve<'a>( async fn net_serve<'lua>(
lua: &'static Lua, lua: &'static Lua,
(port, config): (u16, ServeConfig<'a>), (port, config): (u16, ServeConfig<'lua>),
) -> LuaResult<LuaTable<'a>> { ) -> LuaResult<LuaTable<'lua>> {
// Note that we need to use a mpsc here and not // Note that we need to use a mpsc here and not
// a oneshot channel since we move the sender // a oneshot channel since we move the sender
// into our table with the stop function // into our table with the stop function
@ -188,10 +191,10 @@ async fn net_serve<'a>(
.build_readonly() .build_readonly()
} }
fn net_url_encode<'a>( fn net_url_encode<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(lua_string, as_binary): (LuaString<'a>, Option<bool>), (lua_string, as_binary): (LuaString<'lua>, Option<bool>),
) -> LuaResult<LuaValue<'a>> { ) -> LuaResult<LuaValue<'lua>> {
if matches!(as_binary, Some(true)) { if matches!(as_binary, Some(true)) {
urlencoding::encode_binary(lua_string.as_bytes()).into_lua(lua) urlencoding::encode_binary(lua_string.as_bytes()).into_lua(lua)
} else { } else {
@ -199,10 +202,10 @@ fn net_url_encode<'a>(
} }
} }
fn net_url_decode<'a>( fn net_url_decode<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(lua_string, as_binary): (LuaString<'a>, Option<bool>), (lua_string, as_binary): (LuaString<'lua>, Option<bool>),
) -> LuaResult<LuaValue<'a>> { ) -> LuaResult<LuaValue<'lua>> {
if matches!(as_binary, Some(true)) { if matches!(as_binary, Some(true)) {
urlencoding::decode_binary(lua_string.as_bytes()).into_lua(lua) urlencoding::decode_binary(lua_string.as_bytes()).into_lua(lua)
} else { } else {

View file

@ -81,10 +81,10 @@ pub fn create(lua: &'static Lua, args_vec: Vec<String>) -> LuaResult<LuaTable> {
.build_readonly() .build_readonly()
} }
fn process_env_get<'a>( fn process_env_get<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(_, key): (LuaValue<'a>, String), (_, key): (LuaValue<'lua>, String),
) -> LuaResult<LuaValue<'a>> { ) -> LuaResult<LuaValue<'lua>> {
match env::var_os(key) { match env::var_os(key) {
Some(value) => { Some(value) => {
let raw_value = RawOsString::new(value); let raw_value = RawOsString::new(value);
@ -96,9 +96,9 @@ fn process_env_get<'a>(
} }
} }
fn process_env_set( fn process_env_set<'lua>(
_: &'static Lua, _: &'lua Lua,
(_, key, value): (LuaValue, String, Option<String>), (_, key, value): (LuaValue<'lua>, String, Option<String>),
) -> LuaResult<()> { ) -> LuaResult<()> {
// Make sure key is valid, otherwise set_var will panic // Make sure key is valid, otherwise set_var will panic
if key.is_empty() { 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, lua: &'static Lua,
(mut program, args, options): (String, Option<Vec<String>>, Option<LuaTable<'a>>), (mut program, args, options): (String, Option<Vec<String>>, Option<LuaTable<'lua>>),
) -> LuaResult<LuaTable<'a>> { ) -> LuaResult<LuaTable<'lua>> {
// Parse any given options or create defaults // Parse any given options or create defaults
let (child_cwd, child_envs, child_shell, child_stdio_inherit) = match options { let (child_cwd, child_envs, child_shell, child_stdio_inherit) = match options {
Some(options) => { Some(options) => {

View file

@ -16,34 +16,34 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.build_readonly() .build_readonly()
} }
fn serde_encode<'a>( fn serde_encode<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(format, val, pretty): (EncodeDecodeFormat, LuaValue<'a>, Option<bool>), (format, val, pretty): (EncodeDecodeFormat, LuaValue<'lua>, Option<bool>),
) -> LuaResult<LuaString<'a>> { ) -> LuaResult<LuaString<'lua>> {
let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default())); let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default()));
config.serialize_to_string(lua, val) config.serialize_to_string(lua, val)
} }
fn serde_decode<'a>( fn serde_decode<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(format, str): (EncodeDecodeFormat, LuaString<'a>), (format, str): (EncodeDecodeFormat, LuaString<'lua>),
) -> LuaResult<LuaValue<'a>> { ) -> LuaResult<LuaValue<'lua>> {
let config = EncodeDecodeConfig::from(format); let config = EncodeDecodeConfig::from(format);
config.deserialize_from_string(lua, str) config.deserialize_from_string(lua, str)
} }
async fn serde_compress<'a>( async fn serde_compress<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(format, str): (CompressDecompressFormat, LuaString<'a>), (format, str): (CompressDecompressFormat, LuaString<'lua>),
) -> LuaResult<LuaString<'a>> { ) -> LuaResult<LuaString<'lua>> {
let bytes = compress(format, str).await?; let bytes = compress(format, str).await?;
lua.create_string(bytes) lua.create_string(bytes)
} }
async fn serde_decompress<'a>( async fn serde_decompress<'lua>(
lua: &'static Lua, lua: &'lua Lua,
(format, str): (CompressDecompressFormat, LuaString<'a>), (format, str): (CompressDecompressFormat, LuaString<'lua>),
) -> LuaResult<LuaString<'a>> { ) -> LuaResult<LuaString<'lua>> {
let bytes = decompress(format, str).await?; let bytes = decompress(format, str).await?;
lua.create_string(bytes) lua.create_string(bytes)
} }