Skip to content

Commit 88063e7

Browse files
committed
Make table module public
1 parent c8436e2 commit 88063e7

File tree

3 files changed

+167
-8
lines changed

3 files changed

+167
-8
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ mod scope;
8484
mod state;
8585
mod stdlib;
8686
mod string;
87-
mod table;
8887
mod thread;
8988
mod traits;
9089
mod types;
@@ -95,6 +94,7 @@ mod vector;
9594

9695
pub mod function;
9796
pub mod prelude;
97+
pub mod table;
9898

9999
pub use bstr::BString;
100100
pub use ffi::{self, lua_CFunction, lua_State};
@@ -108,7 +108,7 @@ pub use crate::scope::Scope;
108108
pub use crate::state::{GCMode, Lua, LuaOptions, WeakLua};
109109
pub use crate::stdlib::StdLib;
110110
pub use crate::string::{BorrowedBytes, BorrowedStr, LuaString, LuaString as String};
111-
pub use crate::table::{Table, TablePairs, TableSequence};
111+
pub use crate::table::Table;
112112
pub use crate::thread::{Thread, ThreadStatus};
113113
pub use crate::traits::{
114114
FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, LuaNativeFn, LuaNativeFnMut, ObjectLike,

src/prelude.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ pub use crate::{
99
LightUserData as LuaLightUserData, Lua, LuaNativeFn, LuaNativeFnMut, LuaOptions, LuaString,
1010
MetaMethod as LuaMetaMethod, MultiValue as LuaMultiValue, Nil as LuaNil, Number as LuaNumber,
1111
ObjectLike as LuaObjectLike, RegistryKey as LuaRegistryKey, Result as LuaResult, StdLib as LuaStdLib,
12-
Table as LuaTable, TablePairs as LuaTablePairs, TableSequence as LuaTableSequence, Thread as LuaThread,
13-
ThreadStatus as LuaThreadStatus, UserData as LuaUserData, UserDataFields as LuaUserDataFields,
14-
UserDataMetatable as LuaUserDataMetatable, UserDataMethods as LuaUserDataMethods,
15-
UserDataRef as LuaUserDataRef, UserDataRefMut as LuaUserDataRefMut,
16-
UserDataRegistry as LuaUserDataRegistry, Value as LuaValue, Variadic as LuaVariadic,
17-
VmState as LuaVmState, WeakLua, function::FunctionInfo as LuaFunctionInfo,
12+
Table as LuaTable, Thread as LuaThread, ThreadStatus as LuaThreadStatus, UserData as LuaUserData,
13+
UserDataFields as LuaUserDataFields, UserDataMetatable as LuaUserDataMetatable,
14+
UserDataMethods as LuaUserDataMethods, UserDataRef as LuaUserDataRef,
15+
UserDataRefMut as LuaUserDataRefMut, UserDataRegistry as LuaUserDataRegistry, Value as LuaValue,
16+
Variadic as LuaVariadic, VmState as LuaVmState, WeakLua, function::FunctionInfo as LuaFunctionInfo,
17+
table::TablePairs as LuaTablePairs, table::TableSequence as LuaTableSequence,
1818
};
1919

2020
#[cfg(not(feature = "luau"))]

src/table.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,162 @@
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+
1160
use std::collections::HashSet;
2161
use std::fmt;
3162
use std::marker::PhantomData;

0 commit comments

Comments
 (0)