Skip to main content

Table

Struct Table 

Source
pub struct Table(/* private fields */);
Expand description

Handle to an internal Lua table.

Implementations§

Source§

impl Table

Source

pub fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<(), Error>

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()?;
Source

pub fn get<V>(&self, key: impl IntoLua) -> Result<V, Error>
where V: FromLua,

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);
Source

pub fn contains_key(&self, key: impl IntoLua) -> Result<bool, Error>

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

This might invoke the __index metamethod.

Source

pub fn push(&self, value: impl IntoLua) -> Result<(), Error>

Appends a value to the back of the table.

This might invoke the __len and __newindex metamethods.

Source

pub fn pop<V>(&self) -> Result<V, Error>
where V: FromLua,

Removes the last element from the table and returns it.

This might invoke the __len and __newindex metamethods.

Source

pub fn remove(&self, key: impl IntoLua) -> Result<(), Error>

Removes a key from the table.

If key is an integer, mlua shifts down the elements from table[key+1], and erases element table[key]. The complexity is O(n) in the worst case, where n is the table length.

For other key types this is equivalent to setting table[key] = nil.

This might invoke the __len, __index and __newindex metamethods. Use the raw_remove method if that is not desired.

Source

pub fn equals(&self, other: &Table) -> Result<bool, Error>

Compares two tables for equality.

Uses the same rules as Lua’s == operator, including __eq metamethod dispatch. Lua <5.3 and Luau require both tables to have the same __eq metamethod. Lua 5.3+ check self first, then other if no metamethod is found.

§Examples

Compare two tables using __eq metamethod:

let table1 = lua.create_table()?;
table1.set(1, "value")?;

let table2 = lua.create_table()?;
table2.set(2, "value")?;

let always_equals_mt = lua.create_table()?;
always_equals_mt.set("__eq", lua.create_function(|_, (_t1, _t2): (Table, Table)| Ok(true))?)?;
table1.set_metatable(Some(always_equals_mt.clone()))?;
table2.set_metatable(Some(always_equals_mt))?;

assert!(table1.equals(&table1.clone())?);
assert!(table1.equals(&table2)?);
Source

pub fn raw_set( &self, key: impl IntoLua, value: impl IntoLua, ) -> Result<(), Error>

Sets a key-value pair without invoking metamethods.

Source

pub fn raw_get<V>(&self, key: impl IntoLua) -> Result<V, Error>
where V: FromLua,

Gets the value associated to key without invoking metamethods.

Source

pub fn raw_insert(&self, idx: i64, value: impl IntoLua) -> Result<(), Error>

Inserts element value at position idx to the table, shifting up the elements from table[idx].

The worst case complexity is O(n), where n is the table length.

Source

pub fn raw_push(&self, value: impl IntoLua) -> Result<(), Error>

Appends a value to the back of the table without invoking metamethods.

Source

pub fn raw_pop<V>(&self) -> Result<V, Error>
where V: FromLua,

Removes the last element from the table and returns it, without invoking metamethods.

Source

pub fn raw_remove(&self, key: impl IntoLua) -> Result<(), Error>

Removes a key from the table.

If key is an integer, mlua shifts down the elements from table[key+1], and erases element table[key]. The complexity is O(n) in the worst case, where n is the table length.

For other key types this is equivalent to setting table[key] = nil.

Source

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

Clears the table, removing all keys and values from array and hash parts, without invoking metamethods.

This method is useful to clear the table while keeping its capacity.

Source

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

Returns the result of the Lua # operator.

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

Source

pub fn raw_len(&self) -> usize

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

Source

pub fn is_empty(&self) -> bool

Returns true if the table is empty, without invoking metamethods.

It checks both the array part and the hash part.

Source

pub fn metatable(&self) -> Option<Table>

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>) -> Result<(), Error>

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 to_pointer(&self) -> *const c_void

Converts this table to a generic C pointer.

Different tables will give different pointers. There is no way to convert the pointer back to its original value.

Typically this function is used only for hashing and debug information.

Source

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

Returns 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.

§Examples

Iterate over all globals:

let globals = lua.globals();

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

pub fn for_each<K, V>( &self, f: impl FnMut(K, V) -> Result<(), Error>, ) -> Result<(), Error>
where K: FromLua, V: FromLua,

Iterates over the pairs of the table, invoking the given closure on each pair.

This method is similar to Table::pairs, but optimized for performance. It does not invoke the __pairs metamethod.

Source

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

Returns 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 behavior of Lua’s ipairs function but does not invoke any metamethods.

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

Source§

impl Clone for Table

Source§

fn clone(&self) -> Table

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Table

Source§

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

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

impl FromLua for Table

Source§

fn from_lua(value: Value, _: &Lua) -> Result<Table, Error>

Performs the conversion.
Source§

impl IntoLua for Table

Source§

fn into_lua(self, _: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl IntoLua for &Table

Source§

fn into_lua(self, _: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl ObjectLike for Table

Source§

fn get<V>(&self, key: impl IntoLua) -> Result<V, Error>
where V: FromLua,

Gets the value associated to key from the object, assuming it has __index metamethod.
Source§

fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<(), Error>

Sets the value associated to key in the object, assuming it has __newindex metamethod.
Source§

fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R, Error>
where R: FromLuaMulti,

Calls the object as a function assuming it has __call metamethod. Read more
Source§

fn call_method<R>( &self, name: &str, args: impl IntoLuaMulti, ) -> Result<R, Error>
where R: FromLuaMulti,

Gets the function associated to key name from the object and calls it, passing the object itself along with args as function arguments.
Source§

fn call_function<R>( &self, name: &str, args: impl IntoLuaMulti, ) -> Result<R, Error>
where R: FromLuaMulti,

Gets the function associated to key name from the object and calls it, passing args as function arguments. Read more
Source§

fn to_string(&self) -> Result<String, Error>

Converts the object to a string in a human-readable format. Read more
Source§

fn to_value(&self) -> Value

Converts the object to a Lua value.
Source§

fn get_path<V>(&self, path: &str) -> Result<V, Error>
where V: FromLua,

Look up a value by a path of keys. Read more
Source§

impl PartialEq for Table

Source§

fn eq(&self, other: &Table) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T> PartialEq<&[T]> for Table
where T: IntoLua + Clone,

Source§

fn eq(&self, other: &&[T]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T, const N: usize> PartialEq<[T; N]> for Table
where T: IntoLua + Clone,

Source§

fn eq(&self, other: &[T; N]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T> PartialEq<[T]> for Table
where T: IntoLua + Clone,

Source§

fn eq(&self, other: &[T]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Table

Auto Trait Implementations§

§

impl !Freeze for Table

§

impl !RefUnwindSafe for Table

§

impl !UnwindSafe for Table

§

impl Forget for Table

§

impl Move for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnsafeUnpin for Table

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> ConditionalSend for T
where T: Send,

Source§

impl<T> Context for T
where T: 'static + Send + Any,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<T, Error>

Performs the conversion. Read more
Source§

fn from_lua_args( args: MultiValue, i: usize, to: Option<&str>, lua: &Lua, ) -> Result<T, Error>

Source§

unsafe fn from_stack_multi(nvals: i32, lua: &RawLua) -> Result<T, Error>

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> Result<T, Error>

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue, Error>

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<i32, Error>

Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> Template for T
where T: Clone + Unpin,

Source§

type Output = T

The type of value produced by this Template.
Source§

fn build_template( &self, _context: &mut TemplateContext<'_, '_>, ) -> Result<<T as Template>::Output, BevyError>

Uses this template and the given entity context to produce a Template::Output.
Source§

fn clone_template(&self) -> T

Clones this template. See Clone.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of self.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more