Struct rlua::Table [] [src]

pub struct Table<'lua>(_);

Handle to an internal Lua table.

Methods

impl<'lua> Table<'lua>
[src]

[src]

Sets a key-value pair in the table.

If the value is nil, this will effectively remove the pair.

This might invoke the __newindex metamethod. Use the raw_set method if that is not desired.

Examples

Export a value as a global to make it usable from Lua:

let lua = Lua::new();
let globals = lua.globals();

globals.set("assertions", cfg!(debug_assertions))?;

lua.exec::<()>(r#"
    if assertions == true then
        -- ...
    elseif assertions == false then
        -- ...
    else
        error("assertions neither on nor off?")
    end
"#, None)?;

[src]

Gets the value associated to key from the table.

If no value is associated to key, returns the nil value.

This might invoke the __index metamethod. Use the raw_get method if that is not desired.

Examples

Query the version of the Lua interpreter:

let lua = Lua::new();
let globals = lua.globals();

let version: String = globals.get("_VERSION")?;
println!("Lua version: {}", version);

[src]

Checks whether the table contains a non-nil value for key.

[src]

Sets a key-value pair without invoking metamethods.

[src]

Gets the value associated to key without invoking metamethods.

[src]

Returns the result of the Lua # operator.

This might invoke the __len metamethod. Use the raw_len method if that is not desired.

[src]

Returns the result of the Lua # operator, without invoking the __len metamethod.

[src]

Returns a reference to the metatable of this table, or None if no metatable is set.

Unlike the getmetatable Lua function, this method ignores the __metatable field.

[src]

Sets or removes the metatable of this table.

If metatable is None, the metatable is removed (if no metatable is set, this does nothing).

[src]

Consume this table and return an iterator over the pairs of the table.

This works like the Lua pairs function, but does not invoke the __pairs metamethod.

The pairs are wrapped in a Result, since they are lazily converted to K and V types.

Note

While this method consumes the Table object, it can not prevent code from mutating the table while the iteration is in progress. Refer to the Lua manual for information about the consequences of such mutation.

Examples

Iterate over all globals:

let lua = Lua::new();
let globals = lua.globals();

for pair in globals.pairs::<Value, Value>() {
    let (key, value) = pair?;
    // ...
}

[src]

Consume this table and return an iterator over all values in the sequence part of the table.

The iterator will yield all values t[1], t[2], and so on, until a nil value is encountered. This mirrors the behaviour of Lua's ipairs function and will invoke the __index metamethod according to the usual rules. However, the deprecated __ipairs metatable will not be called.

Just like pairs, the values are wrapped in a Result.

Note

While this method consumes the Table object, it can not prevent code from mutating the table while the iteration is in progress. Refer to the Lua manual for information about the consequences of such mutation.

Examples

let lua = Lua::new();
let my_table: Table = lua.eval("{ [1] = 4, [2] = 5, [4] = 7, key = 2 }", None)?;

let expected = [4, 5];
for (&expected, got) in expected.iter().zip(my_table.sequence_values::<u32>()) {
    assert_eq!(expected, got?);
}

Trait Implementations

impl<'lua> ToLua<'lua> for Table<'lua>
[src]

[src]

Performs the conversion.

impl<'lua> FromLua<'lua> for Table<'lua>
[src]

[src]

Performs the conversion.

impl<'lua> Clone for Table<'lua>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'lua> Debug for Table<'lua>
[src]

[src]

Formats the value using the given formatter.