use std::cell::{RefCell, RefMut}; pub trait RefCellExt { fn try_borrow_mut_or_yield( &self, ) -> std::result::Result, std::cell::BorrowMutError>; fn borrow_mut_or_yield(&self) -> RefMut<'_, T> { self.try_borrow_mut_or_yield().unwrap() } } impl RefCellExt for RefCell { fn try_borrow_mut_or_yield( &self, ) -> std::result::Result, std::cell::BorrowMutError> { loop { match self.try_borrow_mut() { Ok(borrow) => break Ok(borrow), Err(error) => { tracing::warn!("dynamic borrow failed, yielding to local tasks"); match rayon::yield_local() { Some(rayon::Yield::Executed) => continue, _ => return Err(error), } } } } } }