pub struct Chunk { /* private fields */ }Implementations§
Source§impl Chunk
impl Chunk
pub fn set_name(self, name: impl AsRef<[u8]>) -> Self
pub fn exec(self) -> Result<()>
Sourcepub fn eval<T: FromLuaMulti>(self) -> Result<T>
pub fn eval<T: FromLuaMulti>(self) -> Result<T>
Examples found in repository?
examples/scope_world.rs (line 91)
65fn main() -> Result<()> {
66 let lua = Lua::new();
67 let mut world = World::default();
68
69 let count: i64 = lua.scope(|s| {
70 let world_ud = s.create_userdata_ref_mut(&lua, &mut world)?;
71 lua.globals().set("world", &world_ud)?;
72
73 lua.load(
74 r#"
75 local a = world:spawn()
76 local b = world:spawn()
77
78 -- Direct mutation through a live sub-reference.
79 local pa = world:position(a)
80 pa.x, pa.y = 10, 20
81
82 local pb = world:position(b)
83 pb.x = pa.x + 5 -- reading pa here re-borrows; both are short-lived
84
85 -- Stash one to prove it dies with the scope.
86 escaped = world:position(a)
87
88 return world:count()
89 "#,
90 )
91 .eval()
92 })?;
93
94 println!("spawned {count} entities");
95 // The mutations are visible to Rust after the scope returns.
96 for (i, e) in world.entities.iter().enumerate() {
97 println!("entity {i} = ({}, {})", e.x, e.y);
98 }
99
100 // The handle the script stashed on `escaped` is now invalid. Reading a
101 // field raises a Lua error rather than touching the released `&mut World`.
102 // We surface the message through Lua's own `pcall` + `tostring`, since the
103 // error payload is a Lua value.
104 let (ok, msg): (bool, String) = lua
105 .load("local ok, e = pcall(function() return escaped.x end); return ok, tostring(e)")
106 .eval()?;
107 assert!(!ok, "stashed handle should be unusable after the scope");
108 println!("post-scope use of a stashed handle -> {msg}");
109
110 Ok(())
111}Auto Trait Implementations§
impl Freeze for Chunk
impl !RefUnwindSafe for Chunk
impl !Send for Chunk
impl !Sync for Chunk
impl Unpin for Chunk
impl UnsafeUnpin for Chunk
impl !UnwindSafe for Chunk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more