Skip to content

Commit a653d08

Browse files
committed
Simplify Compiler::add_library_constant (combine lib and member)
1 parent 2b6b014 commit a653d08

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/chunk.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,19 @@ impl Compiler {
349349
/// The constants are used by the compiler to optimize the generated bytecode.
350350
/// Optimization level must be at least 2 for this to have any effect.
351351
///
352-
/// The first element of the tuple is the library name,the second is the member name, and the
353-
/// third is the constant value.
352+
/// The `name` is a string in the format `lib.member`, where `lib` is the library name
353+
/// and `member` is the member (constant) name.
354354
#[must_use]
355355
pub fn add_library_constant(
356356
mut self,
357-
lib: impl Into<StdString>,
358-
member: impl Into<StdString>,
357+
name: impl AsRef<str>,
359358
r#const: impl Into<CompileConstant>,
360359
) -> Self {
361-
let (lib, member) = (lib.into(), member.into());
360+
let Some((lib, member)) = name.as_ref().split_once('.') else {
361+
return self;
362+
};
363+
let (lib, member) = (lib.to_owned(), member.to_owned());
364+
362365
if !self.libraries_with_known_members.contains(&lib) {
363366
self.libraries_with_known_members.push(lib.clone());
364367
}

tests/chunk.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ fn test_compiler_library_constants() {
145145

146146
let compiler = Compiler::new()
147147
.set_optimization_level(2)
148-
.add_library_constant("mylib", "const_bool", true)
149-
.add_library_constant("mylib", "const_num", 123.0)
150-
.add_library_constant("mylib", "const_vec", Vector::zero())
151-
.add_library_constant("mylib", "const_str", "value1");
148+
.add_library_constant("mylib.const_bool", true)
149+
.add_library_constant("mylib.const_num", 123.0)
150+
.add_library_constant("mylib.const_vec", Vector::zero())
151+
.add_library_constant("mylib.const_str", "value1");
152152

153153
let lua = Lua::new();
154154
lua.set_compiler(compiler);

0 commit comments

Comments
 (0)