From 4625ac9d52e35cf06373774eed4d17547842bbb2 Mon Sep 17 00:00:00 2001 From: kyren Date: Mon, 1 Oct 2018 05:24:11 -0400 Subject: Some more minor guided tour updates --- examples/guided_tour.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/examples/guided_tour.rs b/examples/guided_tour.rs index dfe6801..03a1f8b 100644 --- a/examples/guided_tour.rs +++ b/examples/guided_tour.rs @@ -153,26 +153,29 @@ fn main() -> Result<()> { // create userdata and callbacks types that only live for as long as the call to scope, but do // not have to be `Send` OR `'static`. - let mut rust_val = 0; - - lua.scope(|scope| { - // We create a 'sketchy' lua callback that modifies the variable `rust_val`. Outside of a - // `Lua::scope` call, this would not be allowed. - - lua.globals().set( - "sketchy", - scope.create_function_mut(|_, ()| { - rust_val = 42; - Ok(()) - })?, - )?; - - lua.eval::<()>("sketchy()", None) - })?; + { + let mut rust_val = 0; + + lua.scope(|scope| { + // We create a 'sketchy' lua callback that modifies the variable `rust_val`. Outside of a + // `Lua::scope` call, this would not be allowed because it could be unsafe. + + lua.globals().set( + "sketchy", + scope.create_function_mut(|_, ()| { + rust_val = 42; + Ok(()) + })?, + )?; + + lua.eval::<()>("sketchy()", None) + })?; + } // We were able to run our 'sketchy' function inside the scope just fine. However, if we try to // run our 'sketchy' function outside of the scope, the function we created will have been - // destroyed and we will generate an error. + // invalidated and we will generate an error. If our function wasn't invalidated, we might be + // able to improperly access the destroyed `rust_val` which would be unsafe. assert_eq!(rust_val, 42); assert!(lua.eval::<()>("sketchy()", None).is_err()); -- cgit v1.2.3