diff --git a/src/error.rs b/src/error.rs index a5c03a5..5ff1b94 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,7 @@ //! - `LUA_ERRMEM` (4) — memory allocation error //! - `LUA_ERRERR` (5) — error in error handler -use std::fmt; +use std::{borrow::Cow, fmt}; /// Result type alias used throughout rilua. pub type LuaResult = Result; @@ -176,31 +176,31 @@ const LUA_IDSIZE: usize = 60; /// - `"=name"` -> `"name"` (literal, truncated to `LUA_IDSIZE`) /// - `"@filename"` -> `"filename"` (file, with `"..."` prefix if too long) /// - other -> `[string "first_line..."]` -pub fn chunkid(source: &str) -> String { +pub fn chunkid<'a>(source: &'a str) -> Cow<'a, str> { if let Some(rest) = source.strip_prefix('=') { // Literal name -- strip the '=' prefix. if rest.len() < LUA_IDSIZE { - rest.to_string() + rest.into() } else { - rest[..LUA_IDSIZE - 1].to_string() + rest[..LUA_IDSIZE - 1].into() } } else if let Some(rest) = source.strip_prefix('@') { // File name. if rest.len() < LUA_IDSIZE { - rest.to_string() + rest.into() } else { let skip = rest.len() - (LUA_IDSIZE - 4); - format!("...{}", &rest[skip..]) + format!("...{}", &rest[skip..]).into() } } else { // String source. let first_line = source.split('\n').next().unwrap_or(source); let max_len = LUA_IDSIZE - "[string \"...\"]".len(); if first_line.len() <= max_len && !source.contains('\n') { - format!("[string \"{first_line}\"]") + format!("[string \"{first_line}\"]").into() } else { let truncated = &first_line[..first_line.len().min(max_len)]; - format!("[string \"{truncated}...\"]") + format!("[string \"{truncated}...\"]").into() } } } diff --git a/src/stdlib/debug.rs b/src/stdlib/debug.rs index 1fdb25b..68e50e8 100644 --- a/src/stdlib/debug.rs +++ b/src/stdlib/debug.rs @@ -573,7 +573,7 @@ fn extract_closure_info( Closure::Lua(lcl) => ClosureInfo { is_lua: true, source: lcl.proto.source.clone(), - short_src: chunkid(&lcl.proto.source), + short_src: chunkid(&lcl.proto.source).into(), line_defined: i64::from(lcl.proto.line_defined), last_line_defined: i64::from(lcl.proto.last_line_defined), what: if lcl.proto.line_defined == 0 {