Repository: tokio-rs/tokio
File: tokio/src/task/task_local.rs (Rust)

Code before:
        // move `self.future` from now on.
        let this = unsafe { Pin::into_inner_unchecked(self) };
        let mut future_opt = unsafe { Pin::new_unchecked(&mut this.future) };

        let res =
            this.local
                .scope_inner(&mut this.slot, || match future_opt.as_mut().as_pin_mut() {
                    Some(fut) => {
                        let res = fut.poll(cx);
                        if res.is_ready() {
                            future_opt.set(None);
                        }
                        Some(res)
                    }
                    None => None,
                });

        match res {
            Ok(Some(res)) => res,
            Ok(None) => panic!("TaskLocalFuture polled after completion"),
            Err(ScopeInnerErr::BorrowError) => {
                panic!("TaskLocalFuture::poll called while task local is borrowed")
            }
            Err(ScopeInnerErr::AccessError) => {
                panic!("cannot access a Task Local Storage value during or after destruction")
            }
        }
    }
}

impl<T: 'static, F> Drop for TaskLocalFuture<T, F> {
    fn drop(&mut self) {
        if std::mem::needs_drop::<F>() && self.future.is_some() {
            // Drop the future while the task-local is set, if possible. Otherwise
            // the future is dropped normally when the `Option<F>` field drops.
            let future = &mut self.future;
            let _ = self.local.scope_inner(&mut self.slot, || {
                *future = None;
            });
        }
    }
}

impl<T: 'static, F> fmt::Debug for TaskLocalFuture<T, F>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        /// Format the Option without Some.
        struct TransparentOption<'a, T> {
            value: &'a Option<T>,

Review comment (suggestion):
nits:
```suggestion
            Ok(None) => panic!("`TaskLocalFuture` polled after completion"),
            Err(ScopeInnerErr::BorrowError) => {
                panic!("`TaskLocalFuture::poll` called while task-local storage is borrowed")
            }
            Err(ScopeInnerErr::AccessError) => {
                panic!("cannot access a task-local storage value during or after destruction")
            }
```

Code after:
            this.local
                .scope_inner(&mut this.slot, || match future_opt.as_mut().as_pin_mut() {
                    Some(fut) => {
                        let res = fut.poll(cx);
                        if res.is_ready() {
                            future_opt.set(None);
                        }
                        Some(res)
                    }
                    None => None,
                });

        match res {
            Ok(Some(res)) => res,
            Ok(None) => panic!("`TaskLocalFuture` polled after completion"),
            Err(err) => err.panic(),
        }
    }
}

impl<T: 'static, F> Drop for TaskLocalFuture<T, F> {
    fn drop(&mut self) {
        if mem::needs_drop::<F>() && self.future.is_some() {
            // Drop the future while the task-local is set, if possible. Otherwise
            // the future is dropped normally when the `Option<F>` field drops.
            let future = &mut self.future;
            let _ = self.local.scope_inner(&mut self.slot, || {
                *future = None;
            });
        }
    }
}

impl<T: 'static, F> fmt::Debug for TaskLocalFuture<T, F>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        /// Format the Option without Some.
        struct TransparentOption<'a, T> {
            value: &'a Option<T>,
        }
        impl<'a, T: fmt::Debug> fmt::Debug for TransparentOption<'a, T> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self.value.as_ref() {
                    Some(value) => value.fmt(f),
                    // Hitting the None branch should not be possible.
                    None => f.pad("<missing>"),
                }
            }
        }