[][src]Struct mlua::prelude::LuaTable

pub struct LuaTable<'lua>(_);

Handle to an internal Lua table.

Methods

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

pub fn set<K: ToLua<'lua>, V: ToLua<'lua>>(
    &self,
    key: K,
    value: V
) -> Result<()>
[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 globals = lua.globals();

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

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

pub fn get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V>[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 globals = lua.globals();

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

pub fn contains_key<K: ToLua<'lua>>(&self, key: K) -> Result<bool>[src]

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

pub fn call<K, A, R>(&self, key: K, args: A) -> Result<R> where
    K: ToLua<'lua>,
    A: ToLuaMulti<'lua>,
    R: FromLuaMulti<'lua>, 
[src]

Gets the function associated to key from the table and executes it, passing the table itself as the first argument.

Examples

Execute the table method with name "concat":

// simiar to: object:concat("param1", "param2")
object.call("concat", ("param1", "param2"))?;

This might invoke the __index metamethod.

pub fn raw_remove<K: ToLua<'lua>>(&self, key: K) -> Result<()>[src]

Removes a key from the table, returning the value at the key if the key was previously in the table.

pub fn raw_set<K: ToLua<'lua>, V: ToLua<'lua>>(
    &self,
    key: K,
    value: V
) -> Result<()>
[src]

Sets a key-value pair without invoking metamethods.

pub fn raw_get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V>[src]

Gets the value associated to key without invoking metamethods.

pub fn len(&self) -> Result<Integer>[src]

Returns the result of the Lua # operator.

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

pub fn raw_len(&self) -> Integer[src]

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

pub fn get_metatable(&self) -> Option<Table<'lua>>[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.

pub fn set_metatable(&self, metatable: Option<Table<'lua>>)[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).

Important traits for TablePairs<'lua, K, V>
pub fn pairs<K: FromLua<'lua>, V: FromLua<'lua>>(self) -> TablePairs<'lua, K, V>[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 globals = lua.globals();

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

Important traits for TableSequence<'lua, V>
pub fn sequence_values<V: FromLua<'lua>>(self) -> TableSequence<'lua, V>[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 my_table: Table = lua.load(r#"
    {
        [1] = 4,
        [2] = 5,
        [4] = 7,
        key = 2
    }
"#).eval()?;

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]

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

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

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

Auto Trait Implementations

impl<'lua> !Send for Table<'lua>

impl<'lua> !Sync for Table<'lua>

impl<'lua> Unpin for Table<'lua>

impl<'lua> !UnwindSafe for Table<'lua>

impl<'lua> !RefUnwindSafe for Table<'lua>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]