Merge pull request #1702 from UnHumbleBen/patch-2

Fixes description of Closure lifetime in comments.
This commit is contained in:
Alex Crichton 2019-08-05 10:15:23 -05:00 committed by GitHub
commit 0dbf4294c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -72,14 +72,14 @@ fn setup_clock(window: &Window, document: &Document) -> Result<(), JsValue> {
));
}
// The instances of `Closure` that we created will invalidate their
// corresponding JS callback whenever they're dropped, so if we were to
// normally return from `run` then both of our registered closures will
// raise exceptions when invoked.
// The instance of `Closure` that we created will invalidate its
// corresponding JS callback whenever it is dropped, so if we were to
// normally return from `setup_clock` then our registered closure will
// raise an exception when invoked.
//
// Normally we'd store these handles to later get dropped at an appropriate
// time but for now we want these to be global handlers so we use the
// `forget` method to drop them without invalidating the closure. Note that
// Normally we'd store the handle to later get dropped at an appropriate
// time but for now we want it to be a global handler so we use the
// `forget` method to drop it without invalidating the closure. Note that
// this is leaking memory in Rust, so this should be done judiciously!
a.forget();
@ -106,5 +106,7 @@ fn setup_clicker(document: &Document) {
.dyn_ref::<HtmlElement>()
.expect("#green-square be an `HtmlElement`")
.set_onclick(Some(a.as_ref().unchecked_ref()));
// See comments in `setup_clock` above for why we use `a.forget()`.
a.forget();
}