|
| 1 | +//! Lua table handling. |
| 2 | +//! |
| 3 | +//! Tables are Lua's primary data structure, used for arrays, dictionaries, objects, modules, |
| 4 | +//! and more. This module provides types for creating and manipulating Lua tables from Rust. |
| 5 | +//! |
| 6 | +//! # Main Types |
| 7 | +//! |
| 8 | +//! - [`Table`] - A handle to a Lua table. |
| 9 | +//! - [`TablePairs`] - An iterator over key-value pairs in a table. |
| 10 | +//! - [`TableSequence`] - An iterator over the array (sequence) portion of a table. |
| 11 | +//! |
| 12 | +//! # Basic Operations |
| 13 | +//! |
| 14 | +//! Tables support key-value access similar to Rust's `HashMap`: |
| 15 | +//! |
| 16 | +//! ``` |
| 17 | +//! # use mlua::{Lua, Result}; |
| 18 | +//! # fn main() -> Result<()> { |
| 19 | +//! let lua = Lua::new(); |
| 20 | +//! let table = lua.create_table()?; |
| 21 | +//! |
| 22 | +//! // Set and get values |
| 23 | +//! table.set("key", "value")?; |
| 24 | +//! let value: String = table.get("key")?; |
| 25 | +//! assert_eq!(value, "value"); |
| 26 | +//! |
| 27 | +//! // Keys and values can be any Lua-compatible type |
| 28 | +//! table.set(1, "first")?; |
| 29 | +//! table.set("nested", lua.create_table()?)?; |
| 30 | +//! # Ok(()) |
| 31 | +//! # } |
| 32 | +//! ``` |
| 33 | +//! |
| 34 | +//! # Array Operations |
| 35 | +//! |
| 36 | +//! Tables can be used as arrays with 1-based indexing: |
| 37 | +//! |
| 38 | +//! ``` |
| 39 | +//! # use mlua::{Lua, Result}; |
| 40 | +//! # fn main() -> Result<()> { |
| 41 | +//! let lua = Lua::new(); |
| 42 | +//! let array = lua.create_table()?; |
| 43 | +//! |
| 44 | +//! // Push values to the end (like Vec::push) |
| 45 | +//! array.push("first")?; |
| 46 | +//! array.push("second")?; |
| 47 | +//! array.push("third")?; |
| 48 | +//! |
| 49 | +//! // Pop from the end |
| 50 | +//! let last: String = array.pop()?; |
| 51 | +//! assert_eq!(last, "third"); |
| 52 | +//! |
| 53 | +//! // Get length |
| 54 | +//! assert_eq!(array.raw_len(), 2); |
| 55 | +//! # Ok(()) |
| 56 | +//! # } |
| 57 | +//! ``` |
| 58 | +//! |
| 59 | +//! # Iteration |
| 60 | +//! |
| 61 | +//! Iterate over all key-value pairs with [`Table::pairs`]: |
| 62 | +//! |
| 63 | +//! ``` |
| 64 | +//! # use mlua::{Lua, Result, Value}; |
| 65 | +//! # fn main() -> Result<()> { |
| 66 | +//! let lua = Lua::new(); |
| 67 | +//! let table = lua.create_table()?; |
| 68 | +//! table.set("a", 1)?; |
| 69 | +//! table.set("b", 2)?; |
| 70 | +//! |
| 71 | +//! for pair in table.pairs::<String, i32>() { |
| 72 | +//! let (key, value) = pair?; |
| 73 | +//! println!("{key} = {value}"); |
| 74 | +//! } |
| 75 | +//! # Ok(()) |
| 76 | +//! # } |
| 77 | +//! ``` |
| 78 | +//! |
| 79 | +//! For array portions, use [`Table::sequence_values`]: |
| 80 | +//! |
| 81 | +//! ``` |
| 82 | +//! # use mlua::{Lua, Result}; |
| 83 | +//! # fn main() -> Result<()> { |
| 84 | +//! let lua = Lua::new(); |
| 85 | +//! let array = lua.create_sequence_from(["a", "b", "c"])?; |
| 86 | +//! |
| 87 | +//! for value in array.sequence_values::<String>() { |
| 88 | +//! println!("{}", value?); |
| 89 | +//! } |
| 90 | +//! # Ok(()) |
| 91 | +//! # } |
| 92 | +//! ``` |
| 93 | +//! |
| 94 | +//! # Raw vs Normal Access |
| 95 | +//! |
| 96 | +//! Methods prefixed with `raw_` (like [`Table::raw_get`], [`Table::raw_set`]) bypass |
| 97 | +//! metamethods, directly accessing the table's contents. Normal methods may trigger |
| 98 | +//! `__index`, `__newindex`, and other metamethods: |
| 99 | +//! |
| 100 | +//! ``` |
| 101 | +//! # use mlua::{Lua, Result}; |
| 102 | +//! # fn main() -> Result<()> { |
| 103 | +//! let lua = Lua::new(); |
| 104 | +//! |
| 105 | +//! // raw_set bypasses __newindex metamethod |
| 106 | +//! let t = lua.create_table()?; |
| 107 | +//! t.raw_set("key", "value")?; |
| 108 | +//! |
| 109 | +//! // raw_get bypasses __index metamethod |
| 110 | +//! let v: String = t.raw_get("key")?; |
| 111 | +//! # Ok(()) |
| 112 | +//! # } |
| 113 | +//! ``` |
| 114 | +//! |
| 115 | +//! # Metatables |
| 116 | +//! |
| 117 | +//! Tables can have metatables that customize their behavior: |
| 118 | +//! |
| 119 | +//! ``` |
| 120 | +//! # use mlua::{Lua, Result}; |
| 121 | +//! # fn main() -> Result<()> { |
| 122 | +//! let lua = Lua::new(); |
| 123 | +//! |
| 124 | +//! let table = lua.create_table()?; |
| 125 | +//! let metatable = lua.create_table()?; |
| 126 | +//! |
| 127 | +//! // Set a default value via __index |
| 128 | +//! metatable.set("__index", lua.create_function(|_, _: ()| Ok("default"))?)?; |
| 129 | +//! table.set_metatable(Some(metatable))?; |
| 130 | +//! |
| 131 | +//! // Accessing missing keys returns "default" |
| 132 | +//! let value: String = table.get("missing")?; |
| 133 | +//! assert_eq!(value, "default"); |
| 134 | +//! # Ok(()) |
| 135 | +//! # } |
| 136 | +//! ``` |
| 137 | +//! |
| 138 | +//! # Global Table |
| 139 | +//! |
| 140 | +//! The Lua global environment is itself a table, accessible via [`Lua::globals`]: |
| 141 | +//! |
| 142 | +//! ``` |
| 143 | +//! # use mlua::{Lua, Result}; |
| 144 | +//! # fn main() -> Result<()> { |
| 145 | +//! let lua = Lua::new(); |
| 146 | +//! let globals = lua.globals(); |
| 147 | +//! |
| 148 | +//! // Set a global variable |
| 149 | +//! globals.set("my_var", 42)?; |
| 150 | +//! |
| 151 | +//! // Now accessible from Lua code |
| 152 | +//! let result: i32 = lua.load("my_var + 8").eval()?; |
| 153 | +//! assert_eq!(result, 50); |
| 154 | +//! # Ok(()) |
| 155 | +//! # } |
| 156 | +//! ``` |
| 157 | +//! |
| 158 | +//! [`Lua::globals`]: crate::Lua::globals |
| 159 | +
|
1 | 160 | use std::collections::HashSet; |
2 | 161 | use std::fmt; |
3 | 162 | use std::marker::PhantomData; |
|
0 commit comments