pub fn scope<T>(f: impl FnOnce() -> T) -> (ScopeHandle, T)Expand description
Runs f inside a new ownership scope and returns its result along with a handle to the
scope.
Effects (and nested scopes) created while f executes are owned by the scope: they stay
alive without their handles being held, and they are disposed together when the scope is
disposed.
A scope created inside another owner (an effect’s run, or an enclosing scope) is kept alive
by that owner — even if every ScopeHandle clone is dropped — and is disposed with it. An
unowned scope is disposed when the last clone of its handle is dropped; call
leak to keep it alive for the remainder of the program.
§Examples
use std::cell::RefCell;
use std::rc::Rc;
use adaptite::{on_cleanup, scope};
let torn_down = Rc::new(RefCell::new(false));
let (handle, greeting) = scope({
let torn_down = Rc::clone(&torn_down);
move || {
// Effects created here would be owned by the scope and disposed with it.
on_cleanup(move || *torn_down.borrow_mut() = true);
"hello"
}
});
assert_eq!(greeting, "hello");
assert!(!handle.is_disposed());
handle.dispose();
assert!(*torn_down.borrow());