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()
}
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()?;
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 = 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<Vec<String>> {
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<FsMetadata> {
async fn fs_metadata(_: &Lua, path: String) -> LuaResult<FsMetadata> {
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<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 {
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<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 {
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<bool> {
}
}
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
}

View file

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

View file

@ -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<bool>),
) -> LuaResult<LuaString<'a>> {
fn net_json_encode<'lua>(
lua: &'lua Lua,
(val, pretty): (LuaValue<'lua>, Option<bool>),
) -> LuaResult<LuaString<'lua>> {
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<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)
}
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
let client: LuaUserDataRef<NetClient> = 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<LuaTable> {
async fn net_socket<'lua>(lua: &'static Lua, url: String) -> LuaResult<LuaTable> {
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<LuaTable<'a>> {
(port, config): (u16, ServeConfig<'lua>),
) -> LuaResult<LuaTable<'lua>> {
// 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<bool>),
) -> LuaResult<LuaValue<'a>> {
fn net_url_encode<'lua>(
lua: &'lua Lua,
(lua_string, as_binary): (LuaString<'lua>, Option<bool>),
) -> LuaResult<LuaValue<'lua>> {
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<bool>),
) -> LuaResult<LuaValue<'a>> {
fn net_url_decode<'lua>(
lua: &'lua Lua,
(lua_string, as_binary): (LuaString<'lua>, Option<bool>),
) -> LuaResult<LuaValue<'lua>> {
if matches!(as_binary, Some(true)) {
urlencoding::decode_binary(lua_string.as_bytes()).into_lua(lua)
} else {

View file

@ -81,10 +81,10 @@ pub fn create(lua: &'static Lua, args_vec: Vec<String>) -> LuaResult<LuaTable> {
.build_readonly()
}
fn process_env_get<'a>(
lua: &'static Lua,
(_, key): (LuaValue<'a>, String),
) -> LuaResult<LuaValue<'a>> {
fn process_env_get<'lua>(
lua: &'lua Lua,
(_, key): (LuaValue<'lua>, String),
) -> LuaResult<LuaValue<'lua>> {
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<String>),
fn process_env_set<'lua>(
_: &'lua Lua,
(_, key, value): (LuaValue<'lua>, String, Option<String>),
) -> 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<Vec<String>>, Option<LuaTable<'a>>),
) -> LuaResult<LuaTable<'a>> {
(mut program, args, options): (String, Option<Vec<String>>, Option<LuaTable<'lua>>),
) -> LuaResult<LuaTable<'lua>> {
// Parse any given options or create defaults
let (child_cwd, child_envs, child_shell, child_stdio_inherit) = match options {
Some(options) => {

View file

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