mlua-rs / rlua

High level Lua bindings to Rust
Other
1.72k stars 115 forks source link

Missing mutable borrows for Table<'lua> #266

Closed CXWorks closed 1 year ago

CXWorks commented 1 year ago

Hi,

There may be a Rust violation on changing the value of immutable reference in Table<'lua>.

Its setter methods change the state of self as an immutbale reference.

Could you please help me check if this is a bug or design decision? Thx

https://github.com/amethyst/rlua/blob/262180ff21acf01cdb5c4cb5605feb985bf4d284/src/table.rs#L51-L69

https://github.com/amethyst/rlua/blob/262180ff21acf01cdb5c4cb5605feb985bf4d284/src/table.rs#L228-L241

CXWorks commented 1 year ago

Duplicated by https://github.com/amethyst/rlua/issues/48

jugglerchris commented 1 year ago

Good job finding the earlier issue!

In case it helps, it sometimes makes more sense thinking of & and &mut as shared/exclusive references, respectively rather than immutable/mutable. It just happens that with simple types it's safe to mutate things you have an exclusive reference to, but Lua types inherently can have multiple references to each other so an exclusive reference doesn't make much sense.