The source code I am referring to is located here: https://github.com/tokio-rs/tokio/blob/master/tokio/src/runtime/context.rs
thread_local! {
static CONTEXT: RefCell<Option<Handle>> = RefCell::new(None)
}
/// Set this [`Handle`] as the current active [`Handle`].
///
/// [`Handle`]: Handle
pub(crate) fn enter<F, R>(new: Handle, f: F) -> R
where
F: FnOnce() -> R,
{
struct DropGuard(Option<Handle>);
impl Drop for DropGuard {
fn drop(&mut self) {
CONTEXT.with(|ctx| {
*ctx.borrow_mut() = self.0.take();
})
}
}
let _guard = CONTEXT.with(|ctx| {
let old = ctx.borrow_mut().replace(new);
DropGuard(old)
});
f()
} // <------------ fn drop() method gets called here?
I am not able to understand why we immediately replace back the wrapped Option with the value old, as soon as we return from this function.
I might be lacking understanding of how the drop method works, but it seems that it gets invoked when '_guard' variable goes out of scope. It means that the handle is only active during the execution of function 'f'.
I apologize if this answer to this question is straightforward.