Function sycamore_reactive::create_child_scope
source · [−]pub fn create_child_scope<'a, F>(cx: Scope<'a>, f: F) -> ScopeDisposer<'a>where
F: for<'child_lifetime> FnOnce(BoundedScope<'child_lifetime, 'a>),
Expand description
Create a child scope.
Returns a disposer function which will release the memory owned by the Scope
. If the
disposer function is never called, the child scope will be disposed automatically when the
parent scope is disposed.
Child scope lifetime
The lifetime of the child scope is strictly a subset of the lifetime of the parent scope.
[------------'a-------------]
[---------'b--------]
'a: lifetime of parent
'b: lifetime of child
If the disposer is never called, the lifetime 'b
lasts as long as 'a
.
As such, it is impossible for anything allocated in the child scope to escape into the
parent scope.
ⓘ
let mut outer = None;
let disposer = create_child_scope(cx, |cx| {
outer = Some(cx);
// ^^
});
disposer();
let _ = outer.unwrap();
However, the closure itself only needs to live as long as the call to this method because it is called immediately. For example, the following compiles and is perfectly safe:
let mut outer = String::new();
let disposer = create_child_scope(cx, |cx| {
// outer is accessible inside the closure.
outer = "Hello World!".to_string();
});
unsafe { disposer.dispose(); }
drop(outer);
// ^^^^^ -> and remains accessible outside the closure.