pub fn with_cancellation_token<'a, R>(
    token: &'a dyn CancellationToken,
    func: impl FnOnce() -> R,
) -> R {
    // Note that `'static` is a hack here to avoid having to specify the outer (unknown) lifetimes.
    // Since we are guaranteed to reset the token before the lifetime ends and
    // don't copy it anywhere else, this is ok.
    struct RevertToOldTokenGuard<'a> {
        prev: Option<&'static dyn CancellationToken>,
        storage: &'a std::cell::RefCell<Option<&'static dyn CancellationToken>>,
    }

    impl<'a> Drop for RevertToOldTokenGuard<'a> {
        fn drop(&mut self) {
            let mut guard = self.storage.borrow_mut();
            *guard = self.prev;
        }
    }

    CURRENT_CANCELLATION_TOKEN.with(|storage| {
        let mut guard = storage.borrow_mut();
        let static_token: &'static dyn CancellationToken = unsafe { std::mem::transmute(token) };

        let prev = std::mem::replace(&mut *guard, Some(static_token));
        drop(guard);

        // Revert to the last token once the function ends
        // This guard makes sure we are adhering to to the non static lifetime
        let _revert_guard = RevertToOldTokenGuard { prev, storage };

        func()
    })
}
/// Replaces the currently active (thread-local) cancellation token with the provided one,
/// and executes the given function.
/// Once the scope ends, the current cancellation token will be reset to the previous one.
Replaces the currently active (thread-local) cancellation token with the provided one,
and executes the given function.
Once the scope ends, the current cancellation token will be reset to the previous one.
Replaces the currently active ( thread - local ) cancellation token with the provided one and executes the given function . Once the scope ends the current cancellation token will be reset to the previous one .
pub fn with_cancellation_token<'a, R>(
    token: &'a dyn CancellationToken,
    func: impl FnOnce() -> R,
) -> R {
    // Note that `'static` is a hack here to avoid having to specify the outer (unknown) lifetimes.
    // Since we are guaranteed to reset the token before the lifetime ends and
    // don't copy it anywhere else, this is ok.
    struct RevertToOldTokenGuard<'a> {
        prev: Option<&'static dyn CancellationToken>,
        storage: &'a std::cell::RefCell<Option<&'static dyn CancellationToken>>,
    }
    impl<'a> Drop for RevertToOldTokenGuard<'a> {
        fn drop(&mut self) {
            let mut guard = self.storage.borrow_mut();
            *guard = self.prev;
        }
    }
    CURRENT_CANCELLATION_TOKEN.with(|storage| {
        let mut guard = storage.borrow_mut();
        let static_token: &'static dyn CancellationToken = unsafe { std::mem::transmute(token) };
        let prev = std::mem::replace(&mut *guard, Some(static_token));
        drop(guard);
        // Revert to the last token once the function ends
        // This guard makes sure we are adhering to to the non static lifetime
        let _revert_guard = RevertToOldTokenGuard { prev, storage };
        func()
    })
}
pub fn with_cancellation_token < ' a , R > ( token : & ' a dyn CancellationToken , func : impl FnOnce ( ) -> R , ) -> R { // Note that `'static` is a hack here to avoid having to specify the outer (unknown) lifetimes. // Since we are guaranteed to reset the token before the lifetime ends and // don't copy it anywhere else, this is ok. struct RevertToOldTokenGuard < ' a > { prev : Option < & ' static dyn CancellationToken > , storage : & ' a std :: cell :: RefCell < Option < & ' static dyn CancellationToken > > , } impl < ' a > Drop for RevertToOldTokenGuard < ' a > { fn drop ( & mut self ) { let mut guard = self . storage . borrow_mut ( ) ; * guard = self . prev ; } } CURRENT_CANCELLATION_TOKEN . with ( | storage | { let mut guard = storage . borrow_mut ( ) ; let static_token : & ' static dyn CancellationToken = unsafe { std :: mem :: transmute ( token ) } ; let prev = std :: mem :: replace ( & mut * guard , Some ( static_token ) ) ; drop ( guard ) ; // Revert to the last token once the function ends // This guard makes sure we are adhering to to the non static lifetime let _revert_guard = RevertToOldTokenGuard { prev , storage } ; func ( ) } ) }
Replaces the currently active (thread-local) cancellation token with the provided one,
and executes the given function.
Replaces the currently active ( thread - local ) cancellation token with the provided one and executes the given function .
token
&'a dyn CancellationToken
func
impl FnOnce() -> R
token
&'a dyn CancellationToken
func
impl FnOnce() -> R