Struct Table

Source
pub struct Table<'lua>(/* private fields */);
Expand description

Handle to an internal Lua table.

Implementations§

Source§

impl<'lua> Table<'lua>

Source

pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
where K: ToLua<'lua>, V: ToLua<'lua>,

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_context.globals();

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

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

pub fn get<K, V>(&self, key: K) -> Result<V, Error>
where K: ToLua<'lua>, V: FromLua<'lua>,

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_context.globals();

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

pub fn contains_key<K>(&self, key: K) -> Result<bool, Error>
where K: ToLua<'lua>,

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

Source

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

Sets a key-value pair without invoking metamethods.

Source

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

Gets the value associated to key without invoking metamethods.

Source

pub fn len(&self) -> Result<i64, Error>

Returns the result of the Lua # operator.

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

Source

pub fn raw_len(&self) -> i64

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

Source

pub fn get_metatable(&self) -> Option<Table<'lua>>

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.

Source

pub fn set_metatable(&self, metatable: Option<Table<'lua>>)

Sets or removes the metatable of this table.

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

Source

pub fn pairs<K, V>(self) -> TablePairs<'lua, K, V>
where K: FromLua<'lua>, V: FromLua<'lua>,

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_context.globals();

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

pub fn sequence_values<V>(self) -> TableSequence<'lua, V>
where V: FromLua<'lua>,

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_context.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§

Source§

impl<'lua> Clone for Table<'lua>

Source§

fn clone(&self) -> Table<'lua>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'lua> Debug for Table<'lua>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'lua> FromLua<'lua> for Table<'lua>

Source§

fn from_lua(value: Value<'lua>, _: Context<'lua>) -> Result<Table<'lua>, Error>

Performs the conversion.
Source§

impl<'lua> ToLua<'lua> for Table<'lua>

Source§

fn to_lua(self, _: Context<'lua>) -> Result<Value<'lua>, Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'lua> Freeze for Table<'lua>

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'lua, T> FromLuaMulti<'lua> for T
where T: FromLua<'lua>,

Source§

fn from_lua_multi( values: MultiValue<'lua>, lua: Context<'lua>, ) -> Result<T, Error>

Performs the conversion. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'lua, T> ToLuaMulti<'lua> for T
where T: ToLua<'lua>,

Source§

fn to_lua_multi(self, lua: Context<'lua>) -> Result<MultiValue<'lua>, Error>

Performs the conversion.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Erased for T